POJ 3321 Apple Tree, poj3321appletree
| Time Limit:2000 MS |
|
Memory Limit:65536 K |
| Total Submissions:31499 |
|
Accepted:9477 |
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 hasNForks which are connected by branches. Kaka numbers the forks by 1NAnd 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 integerN(N≤ 100,000), which is the number of the forks in the tree. The followingN-1 lines each contain two integersUAndV, Which means forkUAnd forkVAre connected by a branch. The next line contains an integerM(M≤ 100,000 ). The followingMLines each contain a message which is either "CX"Which means the existence of the apple on forkXHas 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 "QX"Which means an inquiry for the number of apples in the sub-tree above the forkX, 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 31 21 33Q 1C 2Q 1 Sample Output 32 Source POJ Monthly -- 2007.08.05, Huang, Jinsong |
One tree for you. Two operations are supported.
1. Modify the weight of a node
2. query the weight of the subtree (number of nodes in the subtree)
Obviously, the tree array can be used for maintenance.
Why can't I align my font left...
1 # include <cstdio> 2 # include <cstring> 3 # include <cmath> 4 # include <algorithm> 5 # include <queue> 6 # define lb (x) x & (-x) 7 using namespace std; 8 const int MAXN = 2*1e6 + 10; 9 const int INF = 0x7fffff; 10 inline int read () 11 {12 char c = getchar (); int flag = 1, x = 0; 13 while (c <'0' | c> '9 ') {if (c = '-') flag =-1; c = getchar ();} 14 while (c> = '0' & c <= '9 ') x = x * 10 + c-48, c = getchar (); return x * flag; 15} 16 struct node17 {18 int U, v, nxt; 19} edge [MAXN]; 20 int head [MAXN]; 21 int num = 1; 22 inline void add_edge (int x, int y) 23 {24 edge [num]. u = x; 25 edge [num]. v = y; 26 edge [num]. nxt = head [x]; 27 head [x] = num ++; 28} 29 int n; 30 int in [MAXN], out [MAXN], cnt = 0; 31 int tree [MAXN], have [MAXN]; 32 inline void pre () 33 {34 memset (head,-1, sizeof (head); 35 memset (have, 1, sizeof (have); 36 num = 1; cnt = 0; 37} 38 int deep [MAXN]; 39 void dfs (int now) 40 {41 in [n Ow] = ++ cnt; 42 for (int I = head [now]; I! =-1; I = edge [I]. nxt) 43 dfs (edge [I]. v); 44 out [now] = cnt; 45} 46 int how [MAXN]; // whether 47 inline void Interval_change (int pos, int val) on the tree) 48 {49 while (pos <= cnt) 50 {51 tree [pos] + = val; 52 pos + = lb (pos ); 53} 54} 55 inline int Interval_sum (int pos) 56 {57 int ans = 0; 58 while (pos) 59 {60 ans + = tree [pos]; 61 pos-= lb (pos); 62} 63 return ans; 64} 65 int main () 66 {67 while (scanf ("% d", & n) = 1) 68 {69 pre (); 70 for (int I = 1; I <= n-1; I ++) 71 {72 int x = read (), y = read (); 73 add_edge (x, y); 74} 75 dfs (1 ); 76 for (int I = 1; I <= n; I ++) 77 Interval_change (in [I], 1); 78 int q = read (); 79 for (int I = 1; I <= q; I ++) 80 {81 char c [4]; scanf ("% s", c ); 82 if (c [0] = 'q') // query 83 {84 int pos = read (); 85 printf ("% d \ n ", interval_sum (out [pos])-Interval_sum (in [pos]-1); 86} 87 else88 {89 int pos = read (); 90 if (have [pos]) 91 Interval_change (in [pos],-1); 92 else Interval_change (in [Pos], 1); 93 have [pos] =! Have [pos]; 94} 95} 96} 97 return 0; 98}