# codevs1228 苹果树

 C x 表示编号为x的分叉点的状态被改变(原来有苹果的话，就被摘掉，原来没有的话，就结出一个苹果) G x 查询编号为x的分叉点所代表的子树中有多少个苹果

3

1 2

1 3

3

Q 1

C 2

Q 1

3

2

/*

*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100500;
struct edge{
int v;
int nxt;
}e[maxn*2];
int n,u,v,m,t,hh,num;
int dfn[maxn],low[maxn];
int c[maxn],a[maxn];
bool vis[maxn];
char cmd;
inline void ins(int u,int v){
cnt++;
e[cnt].v = v;
}
char ch=getchar();
int f=1,x=0;
while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();};
while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();};
return x*f;
}
void dfs(int x){
dfn[x] = low[x] = ++t;
vis[x] = true;
for(int i = head[x];i;i = e[i].nxt){
if(!vis[e[i].v]){
dfs(e[i].v);
low[x] = max(low[x],low[e[i].v]);
}
}
}
inline int lowbit(int x){
return x&-x;
}
inline void change(int x){
int v;
if(a[x]){
v = -1;
a[x] = 0;
}
else{
v = 1;
a[x] = 1;
}
for(int i = x;i <= n;i+=lowbit(i)) c[i] += v;

}
inline int query(int x){
int ans = 0;
for(int i = x;i;i-=lowbit(i)) ans += c[i];
return ans;
}
int main(){
for(int i = 1;i < n;i++){
ins(u,v);
ins(v,u);
change(i);
}
change(n);
dfs(1);
for(int i = 1;i <= m;i++){
cmd = getchar();
while(cmd != 'C'&&cmd != 'Q') cmd = getchar();
if(cmd == 'Q'){
v = low[num];
u = dfn[num];
hh = query(v) - query(u-1);
printf("%d\n",hh);
}else{
change(dfn[num]);
}
}
return 0;
}

posted @ 2016-09-23 19:04  ACforever  阅读(248)  评论(0编辑  收藏  举报