Topic, is a tree, single-point modification, sub-tree query. A typical DFS sequence entry question.
DFS sequence can represent a tree and a subtree as a continuous interval, and then use a line segment tree to maintain; feeling is a kind of tree chain, and the weight of the chain is different is this is the sub-tree split.
I'm using a non-recursive method to find the DFS sequence.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMAXN 1111116 structedge{7 intv,nxt;8}edge[maxn<<1];9 intN,NE,HEAD[MAXN];Ten voidAddedge (intUintv) { OneEdge[ne].v=v; Edge[ne].nxt=head[u]; head[u]=ne++; A } - - intF[maxn],l[maxn],r[maxn],stack[maxn],odr; the voidDfs () { - inttop=0; -stack[++top]=1; - while(top) { + intu=Stack[top]; - if(L[u]) { +R[U]=++ODR; --top; A Continue; at } -l[u]=++ODR; - for(intI=head[u]; i!=-1; I=edge[i].nxt) { - intv=edge[i].v; - if(F[U]==V)Continue; -F[v]=u; stack[++top]=v; in } - } to } + - intn,tree[maxn<<3],x,y; the voidUpdateintIintJintk) { * if(i==j) { $tree[k]^=1;Panax Notoginseng return; - } the intMid=i+j>>1; + if(x<=mid) Update (i,mid,k<<1); A ElseUpdate (mid+1,j,k<<1|1); thetree[k]=tree[k<<1]+tree[k<<1|1]; + } - intQueryintIintJintk) { $ if(x<=i&&j<=y)returnTree[k]; $ intMid=i+j>>1, res=0; - if(X<=mid) Res=query (i,mid,k<<1); - if(Y>mid) Res+=query (mid+1,j,k<<1|1); the returnRes; - }Wuyi the intMain () { - intm,a,b; Wu Charop[ One]; -memset (head,-1,sizeof(head)); Aboutscanf"%d",&n); $ for(intI=1; i<n; ++i) { -scanf"%d%d",&a,&b); - Addedge (A, b); - Addedge (b,a); A } + dfs (); the for(n=1; n<odr; n<<=1); - for(intI=1; i<=n; ++i) { $x=L[i]; theUpdate1N1); the } thescanf"%d",&m); the while(m--){ -scanf"%s%d",op,&a); in if(op[0]=='Q'){ theX=l[a]; y=R[a]; theprintf"%d\n", Query (1N1)); About}Else{ thex=L[a]; theUpdate1N1); the } + } - return 0; the}
POJ3321 Apple Tree (Dfs order)