BZOJ3631[JLOI2014] Squirrel's new home
Test instructions
Give an n-point tree, and then give a node the tour order, each passing a node (including the point of the previous excursion to the point on the next tour) can take a sugar from this node, ask all the nodes at the beginning to put a few sugar. The last node arrived without sugar. n≤300000
Exercises
The chain of the tree chain into a column, and then use the array interval plus (that is, in the array interval left end position increment value, the array interval right end point +1 position to increase the inverse number of this value, and finally sweep over a[i]+=a[i-1]) accumulated candy number.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMAXN 3001005 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=getchar ();} while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); One returnx*F; A } - structe{intT,n;}; E es[maxn*2];intg[maxn],ess; - voidPeintFintt) { theEs[++ess]= (e) {t,g[f]}; g[f]=ess; Es[++ess]= (e) {f,g[t]}; g[t]=ess; - } - voidInit () {ess=0; Memset (G,0,sizeof(g));} - intN,fa[maxn],dep[maxn],go[maxn],pos[maxn],top[maxn],sz[maxn],sm[maxn],tot; + voidDFS1 (intXintf) { -sz[x]=1; + for(intI=G[X];I;I=ES[I].N)if(es[i].t!=f) { Adep[es[i].t]=dep[x]+1; fa[es[i].t]=x; atDFS1 (ES[I].T,X); sz[x]+=sz[es[i].t]; - } - } - voidDFS2 (intXintFintTP) { -Pos[x]=++tot; TOP[X]=TP;intmx=0; - for(intI=G[X];I;I=ES[I].N)if(es[i].t!=f) { in if(!mx| | SZ[MX]<SZ[ES[I].T]) mx=es[i].t; - } to if(!MX)return; DFS2 (MX,X,TP); + for(intI=G[X];I;I=ES[I].N)if(es[i].t!=f&&es[i].t!=MX) { - DFS2 (es[i].t,x,es[i].t); the } * } $ voidLcaintXinty) {Panax Notoginseng for(; top[x]!=top[y];sm[pos[top[x]]]++,sm[pos[x]+1]--, x=Fa[top[x]]) - if(dep[top[x]]<Dep[top[y]]) swap (x, y); the if(Dep[x]>dep[y]) swap (x, y); sm[pos[x]]++,sm[pos[y]+1]--; + } A intMain () { theN=read (); Inc (I,1, N) go[i]=read (); Init (); +Inc (I,1, N-1){intA=read (), b=read (); PE (A, b);} -dep[1]=0; fa[1]=0; tot=0; DFS1 (1,0); DFS2 (1,0,1); $Inc (I,1, N) { $ if(i==1) {sm[pos[go[i]]]++; sm[pos[go[i]]+1]--;Continue;} -sm[pos[go[i-1]]]--; sm[pos[go[i-1]]+1]++; LCA (go[i-1],go[i]); - if(i==n) {sm[pos[go[i]]]--; sm[pos[go[i]]+1]++;} the } -Inc (I,1, tot) sm[i]+=sm[i-1]; Inc (I,1, N) printf ("%d\n", Sm[pos[i]]);Wuyi return 0; the}
20160610
BZOJ3631[JLOI2014] Squirrel's new home