First, DFS evaluates each node as the root of the subtree in the tree array of the starting and ending values, and then the tree array to do interval query and single point update.
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <utility>
usingnamespaceStd
typedefLongLongLL;
ConstintN =100008, INF =0x3f3f3f3f;
#defineMS (A, num) memset (A, num, sizeof (a))
intLeftid[n], rightid[n];
structnode{
intTo,next;
}edge[2* N];
intHead[n],tot;
voidInit () {
Memset (Head,-1,sizeof(head));
tot =0;
}
InlinevoidAddedge (intUintTo) {
Edge[tot].to=to;
Edge[tot].next=head[u];
head[u]=tot++;
}
intN, M;
BOOLSta[n];
intC[n];
InlineintLowbit (intx) {
returnx&-x;
}
InlinevoidAddintXintVal) {
for(intI=x;i<=n;i+=lowbit (i)) {
C[i] + = val;
}
}
InlineintSumintx) {
intRET =0;
for(intI=x;i>0; I-=lowbit (i)) {
Ret+=c[i];
}
returnRet
}
intCNT =0;
voidDfsintUintFA) {
Leftid[u] = cnt +1;
for(inti = Head[u]; ~i; i = edge[i].next) {
intv = edge[i].to;
if(v! = FA) {
DFS (v, u);
}
}
Rightid[u] = ++cnt;
}
intMain () {
while(~SCANF ("%d", &n) && N) {
MS (C,0);
Init ();
for(inti =1; I <= N; i++) {
Add (I,1);
Sta[i] =1;
}
intU, v;
for(inti =0; I < n-1; i++) {
scanf"%d%d", &u, &v);
Addedge (U, v);
Addedge (V, u);
}
CNT =0;
Dfs1, -1);
cin>>m;
CharOpe
while(m--) {
scanf"%c%d", &ope, &u);
intL = Leftid[u], r = Rightid[u];
if(Ope = ='C'){
if(Sta[r]) {
STA[R] =0;
Add (R,-1);
}Else{
STA[R] =1;
Add (R,1);
}
}Else{
intTP = SUM (r)-SUM (L-1);
printf"%d\n", TP);
}
}
}
return0;
}
POJ3321 Apple Tree (array of trees)