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
For every inquiry, output the correspond answer per line.
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
题意:Q:求以它为节点的子节点的数目(包括自己);C:该节点要是没有苹果就生成一个,有苹果就吃掉……
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define ls rt<<1
#define rs rt<<1|1
#define Lson ls, l, mid
#define Rson rs, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, head[maxN], cnt, root[maxN], deep[maxN], siz[maxN], W_son[maxN], top[maxN], id[maxN], num, W[maxN];
struct Eddge
{
int nex, to;
Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<2];
inline void addEddge(int u, int v)
{
edge[cnt] = Eddge(head[u], v);
head[u] = cnt++;
}
inline void dfs1(int u, int fa, int depth)
{
root[u] = fa;
deep[u] = depth;
siz[u] = 1;
int minn = -1;
for(int i=head[u]; i!=-1; i=edge[i].nex)
{
int v = edge[i].to;
if(v == fa) continue;
dfs1(v, u, depth + 1);
siz[u] += siz[v];
if(siz[v] > minn)
{
minn = siz[v];
W_son[u] = v;
}
}
}
inline void dfs2(int u, int topf)
{
top[u] = topf;
id[u] = ++num;
W[num] = 1;
if(W_son[u] == -1) return;
dfs2(W_son[u], topf);
for(int i=head[u]; i!=-1; i=edge[i].nex)
{
int v = edge[i].to;
if(v == W_son[u] || v == root[u]) continue;
dfs2(v, v);
}
}
int tree[maxN<<2];
inline void pushup(int rt)
{
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
inline void build(int rt, int l, int r)
{
if(l == r)
{
tree[rt] = W[num];
return;
}
int mid = HalF;
build(Lson);
build(Rson);
pushup(rt);
}
inline void update(int rt, int l, int r, int qx)
{
if(l == r)
{
tree[rt] ^= 1;
return;
}
int mid = HalF;
if(qx <= mid) update(Lson, qx);
else update(Rson, qx);
pushup(rt);
}
inline int query(int rt, int l, int r, int ql, int qr)
{
if(ql <= l && qr >= r) return tree[rt];
int mid = HalF;
if(ql > mid) return query(QR);
else if(qr <= mid) return query(QL);
else return query(QL) + query(QR);
}
inline void init()
{
cnt = num = 0;
memset(head, -1, sizeof(head));
memset(W_son, -1, sizeof(W_son));
}
char op[5];
int main()
{
while(scanf("%d", &N)!=EOF)
{
init();
for(int i=1; i<N; i++)
{
int e1, e2; scanf("%d%d", &e1, &e2);
addEddge(e1, e2);
addEddge(e2, e1);
}
dfs1(1, 1, 0);
dfs2(1, 1);
build(1, 1, N);
scanf("%d", &M);
while(M--)
{
scanf("%s", op);
if(op[0] == 'Q')
{
int x; scanf("%d", &x);
printf("%d\n", query(1, 1, N, id[x], id[x] + siz[x] - 1));
}
else
{
int x; scanf("%d", &x);
update(1, 1, N, id[x]);
}
}
}
return 0;
}