Description
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
32
此题我是用线段树做的,深搜确定每个叉所对应的苹果区间。然后就是普通的点修改区间求和问题了。
此题可以用树状数组做。晚些更新树状数组的做法。
/*
n就是点数
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000008
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
int from[maxn],to[maxn];
int first[maxn];
int vv[maxn],nxt[maxn];
bool vis[maxn];
int e,cnt;
struct ST
{
int l,r,key;
}st[maxn<<2];
void buildtree(int id,int l,int r)
{
st[id].l = l,st[id].r = r;
if(l == r)
{
st[id].key = 1;
return;
}
int mid = (l+r) >> 1;
buildtree(lson);
buildtree(rson);
st[id].key = r - l + 1;
}
void PushUp(int id)
{
st[id].key = st[id<<1].key + st[id<<1|1].key;
}
void update(int id,int x)//传递过来的这个x必须是真实的x
{
if(st[id].l == x && st[id].r == x)
{
st[id].key ^= 1;
return;
}
if(st[id<<1].r >= x)
{
update(id<<1,x);
PushUp(id);
return;
}
if(st[id<<1|1].l <= x)
{
update(id<<1|1,x);
PushUp(id);
return;
}
}
int query(int id,int l,int r)
{
if(st[id].l == l && st[id].r == r)
{
return st[id].key;
}
if(st[id<<1].r >= r)
{
return query(id<<1,l,r);
}
if(st[id<<1|1].l <= l)
{
return query(id<<1|1,l,r);
}
return query(id<<1,l,st[id<<1].r) + query(id<<1|1,st[id<<1|1].l,r);
}
void addEdge(int u,int v)
{
vv[e] = v; nxt[e] = first[u]; first[u] = e++;
vv[e] = u; nxt[e] = first[v]; first[v] = e++;
}
void dfs(int x)
{
vis[x] = 1;
from[x] = cnt++;
for(int i=first[x];i!=-1;i=nxt[i])
{
int v = vv[i];
if(!vis[v])
{
dfs(v);
}
}
to[x] = cnt-1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(first,-1,sizeof(first));
memset(vis,0,sizeof(vis));
e = 0;cnt = 1;
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
}
dfs(1);
buildtree(1,1,n);
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
char c;int num;
cin>>c>>num;
if(c == 'C')
{
update(1,from[num]);
}
else printf("%d\n",query(1,from[num],to[num]));
}
}
return 0;
}