Long time no blog, but also for a long time did not brush CV, casually write a bad article warm hands ...
This question is a topic in a tree ... General we would think of a tree array or line segment tree (Blue Goose Violence I also porcelain, just think this time seems to explode
At first I didn't look at it carefully--I thought the root node was not 1 and then began to worry, thought to seek a wave of gravity (fog
The center of gravity of the method to listen to Czl, and then Zzs said to use what points to divide the treatment (did not learn, do not understand, difficult to fight, abandoned pits
Because qzz over this problem, so start hugging thigh--root is 1 ah ... (on the topic of not reading ...)
So I knew the root of the tree and I was just thinking about using the DFS sequence to turn around and then the bare trees array.
Probably like this ... It's a pretty quick run >_<
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <vector > #include <map> #include <algorithm>using namespace std; #define MAXN 100010#define ll Long Long#define Rep (i,l,r,step) for (int i=l;i<=r;i+=step) #define DOWN (i,l,r,step) for (int i=l;i>=r;i-=step) int N,Q,T[MAXN],L[MAXN ],r[maxn],cnt; BOOL F[maxn];//tree_array#define lowbit (x) x&-xint Add (int x,int c) {Rep (i,x,n,lowbit (i)) t[i]+=c;} int sum (int x) {int Ans=0;down (i,x,1,lowbit (i)) Ans+=t[i];return ans;} int edge,to[maxn*2],next[maxn*2],head[maxn];//adjacency table void Insert (int u,int v) {to[++edge]=v;next[edge]=head[u];head[u]= Edge;} Dfs sequence void dfs (int rt,int fa) {l[rt]=++cnt;add (l[rt],1); for (int i=head[rt];i;i=next[i]) {if (TO[I]!=FA) DFS (TO[I],RT);} r[rt]=cnt;} int main () {memset (f,1,sizeof (f)), scanf ("%d", &n), Rep (i,1,n-1,1) {int x,y;scanf ("%d%d", &x,&y); Insert (x, y); insert (y,x);} DFS (1,0); scanf ("%d", &q), while (q--) {char op[10];int x;scanf ("%s%d", op,&x), if (op[0]== ' Q ') printf ("%d\n", SUM (R[x])-sum (l[x]-1)), Else{if (F[x]) {f[x]=0;add (l[x],-1);} Else{f[x]=1;add (l[x],1);}}} return 0;}
codevs-Apple Tree