Description
The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating,
p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a man
ager of a manager) of cow jj, then we say jj is a subordinate of ii.
Unfortunately, the cows find that it is often the case that a manager has less proficiency than seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. Your task is to help the cows figure out when this is happening. For each cow ii in the company,
please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N)
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
Please print N lines of output. The ith line of output should tell the number of
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大
Sample Input
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
2
0
1
0
0
0
1
0
0
一道水题,本来可以用dfs+树状数组两次查询的方式水过的,这里为了练习线段树合并又自己yy了一个,结果还是不错的
因为RE了一回,本地又卡系统栈特意又去查了下空间复杂度,原来每次合并最多会新开logN个节点,所以就开到NlogN就好了
不过要是实在蒙圈的话我认为开到内存上限也不失为良策,只不过要千万小心的计算内存啊。。。
1 #include<cstdio> 2 #include<algorithm> 3 #define N 100005 4 using namespace std; 5 int n,m,p[N],t[N],ans[N]; 6 int h[N],to[N],nxt[N],etop; 7 void add(int u,int v){to[++etop]=v;nxt[etop]=h[u];h[u]=etop;} 8 int data[N*100],ls[N*100],rs[N*100],tot; 9 int newtree(int l,int r,int x){ 10 data[++tot]=1; 11 if(l==r)return tot; 12 int node=tot,mid=(l+r)>>1; 13 if(x<=mid)ls[node]=newtree(l,mid,x); 14 else rs[node]=newtree(mid+1,r,x); 15 return node; 16 } 17 int merge(int l,int r,int u,int v){ 18 if(!u||!v)return u+v; 19 if(l==r){ 20 data[++tot]=data[u]+data[v]; 21 return tot; 22 } 23 int mid=(l+r)>>1,node=++tot; 24 ls[node]=merge(l,mid,ls[u],ls[v]); 25 rs[node]=merge(mid+1,r,rs[u],rs[v]); 26 data[node]=data[ls[node]]+data[rs[node]]; 27 return node; 28 } 29 int query(int l,int r,int L,int R,int u){ 30 if(!u)return 0; 31 if(L>R)return 0; 32 if(l==L&&r==R)return data[u]; 33 int mid=(l+r)>>1; 34 if(R<=mid)return query(l,mid,L,R,ls[u]); 35 else if(L>mid)return query(mid+1,r,L,R,rs[u]); 36 else return query(l,mid,L,mid,ls[u])+query(mid+1,r,mid+1,R,rs[u]); 37 } 38 int dfs(int u){ 39 int node=newtree(1,n,p[u]),v; 40 for(int k=h[u];k;k=nxt[k]){ 41 v=dfs(to[k]); 42 node=merge(1,n,node,v); 43 } 44 ans[u]=query(1,n,p[u]+1,n,node); 45 return node; 46 } 47 int main(){ 48 scanf("%d",&n); 49 for(int i=1;i<=n;i++)scanf("%d",&p[i]),t[i]=p[i]; 50 sort(t+1,t+1+n); 51 m=unique(t+1,t+1+n)-t-1; 52 for(int i=1;i<=n;i++)p[i]=lower_bound(t+1,t+1+m,p[i])-t; 53 for(int i=2,fff;i<=n;i++){ 54 scanf("%d",&fff); 55 add(fff,i); 56 } 57 dfs(1); 58 for(int i=1;i<=n;i++) 59 printf("%d ",ans[i]); 60 }