Title Description
Squirrel's new home is a tree, a few days ago just renovated a new home, the new home has n rooms, and there are n-1 root branches connected, each room can reach each other, and the two rooms between the route is unique. God, he actually lived on the tree. Squirrels want to invite Winnie the Pooh to visit, and also designate a visit guide, he hoped Pooh can follow his guide order, first go to A1, then go to A2, ..., finally to an, to visit a new home. But this will cause the Pooh to repeat a lot of rooms, lazy pooh do not listen to refuse. But the squirrel told him, every walk to a room, he can take a piece of candy from the room to eat. Pooh was a greedy fellow, and he agreed immediately. Now squirrels want to know that in order to ensure that Pooh has candy to eat, he needs to put at least how many sweets in each room. Because the squirrel visited the last room in the guide, an is a restaurant, the restaurant he prepared a hearty meal, so when Pooh at the end of the visit to the restaurant will not need to take the candy to eat.
Input
The first line is an integer n, which represents the second row n integers of the number of rooms, in turn describes A1-an
Next n-1 line, two integers per line x, Y, indicates that the two rooms labeled X and Y are connected by a branch.
Output
A total of n rows, the first row of output labeled I of the room need to put at least how many sweets, in order to let Pooh have candy to eat.
Sample input
51 4 5 3 21 22 42 34 5
Sample output
12121
Exercises
a[i] to a[i+1] Point +1,a[i+1] again-1, then the query is a bit OK.
#include <cmath>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#definell Long LongConst intmaxn=300000+ -;Const intmaxm=600000+ -;intN,FA[MAXN],M,CNT,A[MAXN];intfir[maxn],nex[maxm],to[maxm],ecnt;intTOP[MAXN],SON[MAXN],DEP[MAXN],SZ[MAXN],ID[MAXN];structsegmenttree{intV,l,r,add;} st[maxn<<2];voidAddintUintv) {nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;}voidDFS1 (intXintFintDeep ) {Fa[x]=f;dep[x]=Deep ; SZ[X]=1; for(intE=fir[x];e;e=Nex[e]) { intv=To[e]; if(v==f)Continue; DFS1 (V,x,deep+1); SZ[X]+=Sz[v]; if(Sz[v]>sz[son[x]]) son[x]=v; }}voidDFS2 (intXintTopf) {Top[x]=Topf; ID[X]=++CNT; if(!son[x])return ; DFS2 (Son[x],topf); for(intE=fir[x];e;e=Nex[e]) { intv=To[e]; if(v==fa[x]| | V==SON[X])Continue; DFS2 (V,V); }}voidPushup (introot) {ST[ROOT].V=st[root<<1].v+st[root<<1|1].v;}voidBuildintRootintLintR) {ST[ROOT].L=l;st[root].r=R; if(L==R)return ; intM=l+r>>1; Build (Root<<1, l,m); Build (root<<1|1, m+1, R); Pushup (root);}voidPushdown (introot) {St[root<<1].v=st[root<<1].v+st[root].add* (st[root<<1].r-st[root<<1].l+1); St[root<<1|1].v=st[root<<1|1].v+st[root].add* (st[root<<1|1].r-st[root<<1|1].l+1); St[root<<1].add=st[root<<1].add+St[root].add; St[root<<1|1].add=st[root<<1|1].add+St[root].add; St[root].add=0;}voidAddintRootintLintRintval) { if(st[root].l>r| | ST[ROOT].R<L)return ; if(st[root].l>=l&&st[root].r<=R) {ST[ROOT].V=st[root].v+val* (st[root].r-st[root].l+1); St[root].add=st[root].add+Val; } Else{pushdown (root); Add (Root<<1, l,r,val); Add (root<<1|1, L,r,val); Pushup (root); }}intQueryintRootintLintR) { if(st[root].l>r| | ST[ROOT].R<L)return 0; if(ST[ROOT].L>=L&&ST[ROOT].R<=R)returnst[root].v; Pushdown (root); returnQuery (root<<1, L,r) +query (root<<1|1, l,r);}voidChangeintXintYintval) { intF1=top[x],f2=Top[y]; while(f1!=F2) { if(dep[f1]<Dep[f2]) swap (x, y), swap (F1,F2); Add (1, Id[f1],id[x],val); X=fa[f1],f1=Top[x]; } if(dep[x]>Dep[y]) swap (x, y); Add (1, id[x],id[y],val);} Template<typename t>voidRead (t&AA) { Charcc ll Ff;aa=0; Cc=getchar (); ff=1; while((cc<'0'|| Cc>'9') &&cc!='-') cc=GetChar (); if(cc=='-') ff=-1, cc=GetChar (); while(cc>='0'&&cc<='9') aa=aa*Ten+cc-'0', cc=GetChar (); AA*=ff;}intMain () {read (n); for(intI=1; i<=n;i++) read (A[i]); for(intI=1; i<n;i++){ intx, y; Read (x), read (y); Add (x, y); add (y,x); } DFS1 (1,0,1);d FS2 (1,1); Build (1,1, N); for(intI=1; i<n;i++) {Change (A[i],a[i+1],1); Change (A[i+1],a[i+1],-1); } for(intI=1; i<=n;i++) printf ("%d\n", Query (1, Id[i],id[i])); return 0;}
"bzoj3631" "JLOI2014" Squirrel's new home