求 某节点子树内比该节点的点权大的点的个数
值域上维护树状数组,
#include <bits/stdc++.h> using namespace std ; const int N=1e5+2,M=N*2; int bin[N],len; int sum[N] ,n,ans[N],a[N]; int nxt[M],go[M],hd[N],all; void add_(int x,int y){ go[++all]=y,nxt[all]=hd[x]; hd[x]=all; } int lowbit(int x){ return x&-x; } int qq(int x){ int res= 0; for(;x;x-=lowbit(x)) res+=sum[x]; return res; } void add(int x,int v){ for(;x<=n;x+=lowbit(x)) sum[x]+=v ; } void dfs(int x){ ans[x]-= qq(n)-qq(a[x]); for(int i=hd[x];i;i=nxt[i]){ int y=go[i]; dfs(y); } ans[x]+= qq(n)-qq(a[x]); add(a[x],1); } signed main(){ int i,x; cin>>n; for(i=1;i<=n;i++) cin>>a[i],bin[++len]=a[i]; sort(bin+1,bin+1+len); len=unique(bin+1,bin+1+len)-bin-1; for(i=1;i<=n;i++) a[i]=lower_bound(bin+1,bin+1+len,a[i])-bin; for(i=2;i<=n;i++) cin>>x,add_(x,i); dfs(1); for(i=1;i<=n;i++) cout<<ans[i]<<endl; }
标签:bin,int,res,len,USACO17JAN,add,Promotion,Counting,hd From: https://www.cnblogs.com/towboa/p/17218105.html