POJ-3321Apple Tree
| Time Limit: 2000MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status Description There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would 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 is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree. The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka? Input The first line contains a integer n ( n ≤100,000), which is the number of the forks In the tree. the following N -1 lines each contain the 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 be either " c x strong> "which means the existence of the apple on fork x has been changed. i.e. if there is a apple on the fork and 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 F ork x , including the apple (if exists) on the fork x Note The tree was full of apples at the beginning Output For every inquiry, the output of the correspond answer per line.Sample Input 33Q 1C 2Q 1
Sample Output 32
/*author:2486memory:10004 kbtime:766 mslanguage:g++result:accepted*/#include <cstdio> #include <algorithm& GT, #include <cstring> #include <vector> #include <queue>using namespace std; #define Lson RT << 1, L, Mid#define Rson RT << 1|1, Mid + 1, r#define root 1, 1, nconst int maxn = 2e5 + 5;int sum[maxn << 2], Lu[ma XN], RU[MAXN], N, M, A, B, tot, K;char op[10];/*********** plus template ***************/int head[maxn], NEXT[MAXN], rear;struct Edge {int u,v;} Es[maxn];void Edge_init () {rear = 0; Memset (Head,-1, sizeof (head));} void Edge_add (int u,int v) {es[rear].u = u; ES[REAR].V = v; Next[rear] = Head[u]; Head[u] = rear + +;} void DFS (int to, int from) {Lu[to] = + + tot;//used to mark the sequence of subtrees belonging to it for (int i = head[to]; ~ i; i = next[i]) {int V = ES[I].V; if (v = = from) continue; DFS (V, to); } Ru[to] = tot;} /**********************************/void pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[rt << 1|1];} void build (int rt, int l, int r) {if (L = = r) {Sum[rt] = 1; return; } int mid = (L + r) >> 1; Build (Lson); Build (Rson); Pushup (RT);} void update (int p,int rt,int l, int r) {if (L = = r) {Sum[rt] ^= 1; Return } int mid = (L + r) >> 1; if (P <= mid) Update (P, Lson); else Update (P, Rson); Pushup (RT);} int query (int l, int r,int RT, int l, int R) {if (L <= l && R <= R) {return SUM[RT]; } int mid = (L + r) >> 1; int res = 0; if (L <= mid) Res + = query (l, R, Lson); if (R > Mid) Res + = Query (L, R, Rson); return res;} int main () {//freopen ("D://imput.txt", "R", stdin); while (~ scanf ("%d", &n)) {tot = 0; Build (root); Edge_init (); for (int i = 1; i < N; i + +) {scanf ("%d%d", &a, &b); Edge_add (A, B); Edge_add (B, A); } DFS (1,-1); scanf ("%d", &m);while (M-) {scanf ("%s%d", OP, &k); if (op[0] = = ' C ') {update (lu[k],root); } else {printf ("%d\n", Query (Lu[k],ru[k],root)); }}} return 0;}
|