BZOJ4756 [USACO17JAN]Promotion Counting晉升者計數
阿新 • • 發佈:2019-01-14
Once top spl count root sin describe tar def
問對於每個奶牛來說,它的子樹中有幾個能力值比它大的。
接下來n行為1-n號奶牛的能力值pi
接下來n-1行為2-n號奶牛的經理(樹中的父親)
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大
804289384
846930887
681692778
714636916
957747794
1
1
2
3
0
1
0
0
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號奶牛的經理(樹中的父親)
Output
Sample Input
5804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
20
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\n",ans[i]); 60 }View Code
BZOJ4756 [USACO17JAN]Promotion Counting晉升者計數