This problem can be said to be a tree template problem ... But I have been WA10 to find the wrong ... Later found that is the hand shake hit less a two characters ...
In fact, the topic is to give you a tree and a traversal order, and then follow the traversal order to update the value of the path, and finally query the values of all nodes.
In fact, this problem with the tree differential will be more ideal, but in order to practice the tree is also written in the tree section.
The AC code is as follows:
1097ms 56696kb
#include <bits/stdc++.h>using namespacestd;namespaceStandardio {Template<typename T>inlinevoidRead (T &x) {x=0; T f=1;CharC=GetChar (); for(; c<'0'|| C>'9'; C=getchar ())if(c=='-') f=-1; for(; c>='0'&&c<='9'; C=getchar ()) x=x*Ten+c-'0'; X*=F; } template<typename T>inlinevoidwrite (T x) {if(x<0) Putchar ('-'), x*=-1; if(x>=Ten) Write (x/Ten); Putchar (x%Ten+'0'); }}using namespaceStandardio;namespaceSolve {Const intn=300300; intN; intA[n]; Vector<int>Graph[n]; intindex; intFa[n],dep[n],size[n],son[n],dfn[n],top[n]; structNode {intL,r,val,tag; }tree[n<<2]; voidDFS1 (intNowintfather) {Fa[now]=father,dep[now]=dep[father]+1, size[now]=1; for(RegisterintI=0; I<graph[now].size (); ++i) {intto=Graph[now][i]; if(To==father)Continue; DFS1 (To,now); Size[now]+=Size[to]; if(Size[to]>size[son[now]]) son[now]=to ; } } voidDFS2 (intNowintTopf) {Dfn[now]=++index,top[now]=Topf; if(!son[now])return; DFS2 (Son[now],topf); for(RegisterintI=0; I<graph[now].size (); ++i) {intto=Graph[now][i]; if(to==fa[now]| | To==son[now])Continue; DFS2 (to,to); }} inlinevoidPushup (intPOS) {Tree[pos].val=tree[pos<<1].val+tree[pos<<1|1].val; } InlinevoidPushdown (intPOS) { if(Tree[pos].tag) {Tree[pos<<1].tag+=tree[pos].tag,tree[pos<<1|1].tag+=Tree[pos].tag; Tree[pos<<1].val+= (tree[pos<<1].r-tree[pos<<1].l+1)*Tree[pos].tag; Tree[pos<<1|1].val+= (tree[pos<<1|1].r-tree[pos<<1|1].l+1)*Tree[pos].tag; Tree[pos].tag=0; } } voidBuild (intLintRintPOS) {TREE[POS].L=l,tree[pos].r=r,tree[pos].val=tree[pos].tag=0; if(L==R)return; intMid= (l+r) >>1; Build (L,mid,pos<<1), Build (mid+1,r,pos<<1|1); Pushup (POS); } voidUpdate (intLintRintVintPOS) { if(l<=tree[pos].l&&tree[pos].r<=r) {Tree[pos].val+=v,tree[pos].tag+=v; return; } pushdown (POS); intMid= (TREE[POS].L+TREE[POS].R) >>1; if(l<=mid) Update (l,r,v,pos<<1); if(mid<r) Update (l,r,v,pos<<1|1); Pushup (POS); } intQuery (intLintRintPOS) { if(l<=tree[pos].l&&tree[pos].r<=r) {returnTree[pos].val; } pushdown (POS); intMid= (TREE[POS].L+TREE[POS].R) >>1, ans=0; if(L<=mid) Ans+=query (l,r,pos<<1); if(mid<r) Ans+=query (l,r,pos<<1|1); returnans; } InlinevoidUpdatepath (intXintYintv) { while(top[x]!=Top[y]) { if(dep[top[x]]<Dep[top[y]]) swap (x, y); Update (DFN[TOP[X]],DFN[X],V,1); X=Fa[top[x]]; } if(dep[x]>Dep[y]) swap (x, y); Update (DFN[X],DFN[Y],V,1); } InlineintQuerynode (intx) {returnQuery (Dfn[x],dfn[x],1); } Inlinevoidsolve () {read (n); for(RegisterintI=1; i<=n; ++i) {read (a[i]); } for(RegisterintI=1; i<=n-1; ++i) {intx, y; Read (x), read (y); Graph[x].push_back (y); Graph[y].push_back (x); } DFS1 (1,0); DFS2 (1,1); Build (1N1); for(RegisterintI=1; i<=n-1; ++i) {Updatepath (a[i],a[i+1],1); } for(RegisterintI=1; i<=n; ++i) {write ((I==a[1])? Querynode (i): Querynode (i)-1), Putchar ('\ n'); } }}using namespaceSolve;intMain () {//freopen (". In", "R", stdin);//freopen (". Out", "w", stdout);solve ();}
The P3258 "[JLOI2014] Squirrel's new home"