Main topic:
Modify the nodes on the tree, and then seek the and of the subtree.
Thinking Analysis:
DFS re-numbering, Bad street ...
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define MAXN 100005#define Lson num<<1,s,mid#define Rson num<<1|1,mid+1,eusing namespace Std;int num[maxn];int ED[MAXN] ; int tot[maxn<<2];void pushup (int num) {tot[num]=tot[num<<1]+tot[num<<1|1];} void build (int num,int s,int e) {if (s==e) {tot[num]=1; Return } int mid= (s+e) >>1; Build (Lson); Build (Rson); Pushup (num);} void update (int num,int s,int e,int pos) {if (s==e) {tot[num]^=1; Return } int mid= (s+e) >>1; if (pos<=mid) update (LSON,POS); else update (RSON,POS); Pushup (num);} int query (int num,int s,int e,int l,int R) {if (l<=s && r>=e) {return tot[num]; } int mid= (s+e) >>1; if (r<=mid) return query (LSON,L,R); else if (l>mid) return query (RSON,L,R); else return query (lson,l,mid) +query (rson,mid+1,r);} int Id;int Next[maxn<<1];inT head[maxn];int to[maxn<<1];void addedge (int u,int v) {id++; Next[id]=head[u]; To[id]=v; Head[u]=id;} int Cnt=1;bool vis[maxn];int dfs (int cur) {num[cur]=cnt++; Ed[num[cur]]=num[cur]; for (int p=head[cur]; p;p =next[p]) {if (vis[to[p])) continue; Vis[to[p]]=true; Ed[num[cur]] = max (Ed[num[cur]],dfs (to[p)); } return Ed[num[cur]];} int main () {int n; while (scanf ("%d", &n)!=eof) {memset (vis,false,sizeof vis); memset (head,0,sizeof head); memset (ed,0,sizeof ed); Id=0,cnt=1; for (int i=1;i<=n-1;i++) {int u,v; scanf ("%d%d", &u,&v); Addedge (U,V); Addedge (V,u); } vis[1]=true; DFS (1); int m; scanf ("%d", &m); Char str[2]; Build (1,1,cnt-1); while (m--) {int op; scanf ("%s%d", Str,&op); if (str[0]== ' Q ') {printf ("%d\n ", query (1,1,cnt-1,num[op],ed[num[op])); } else update (1,1,cnt-1,num[op]); }} return 0;} /*41 21 32 42 5*/