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
Source
POJ monthly--2007.08.05, Huang, Jinsong
Test instructions: Given an apple tree, each node starts with an Apple C x, assuming that there is an apple at X, then take it off, assuming there is no, then a new Q X. Querying x points with all of its descendants branch a co-owned several apples
Train of thought: the idea is very ingenious, we numbered all apples by ourselves, each node holds two values. The left value is itself, and the right value is the largest number of all descendants included in it. We can do this by searching for numbers, and after we've got the numbers, we can see that for a certain point, we're going to end up with the whole part of his descendant number first. So the right value of this point, including the current point of all descendants and ancestors, the descendants must be all the number is greater than the point of this node, then ancestors. That must be a point number less than this node, so we can get the answer to the query by sum (Rig[x])-sum (lef[x]-1) As for the update, just need to update the current point can be, this translates into a tree array of the beginning of my vector used Vector<int > A[n]. But has been timed out, started I have not found that this is the problem later on the discovery of others no different, the only difference is here, so try to change into vector<vector<int> > A (N), the result is a, pit AH
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack > #include <queue> #include <map> #include <set> #include <vector> #include <math.h># Include <bitset> #include <list> #include <algorithm> #include <climits>using namespace std;# Define Lson 2*i#define Rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define Up (i,x,y) for (i=x;i<=y;i++) # Define down (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __ GCD (A, b) #define LL long long#define N 100005#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit (x) (x&-x) const int MO D = 1e9+7;vector<vector<int> > A (N);//vector<int> a[n];int n,m,lef[n],rig[n],c[n],tot,s[n];int sum ( int x) {int ret = 0; while (x>0) {ret+=c[x]; X-=lowbit (x); } return ret;} void Add (int x,int d) {while (x<=n) {c[x]+=d; X+=lowbit (x); }}void dfs (int x) {lef[x] = tot; for (int i = 0; I<a[x].size (); i++) {tot++; DFS (A[x][i]); } rig[x] = tot;} int main () {int i,j,k,x,y; Char op[5]; while (~SCANF ("%d", &n)) {MEM (lef,0); MEM (rig,0); MEM (s,0); MEM (c,0); for (i=0; i<n; i++) a[i].clear (); for (i = 1; i<n; i++) {scanf ("%d%d", &x,&y); A[x].push_back (y); } tot = 1; DFS (1); for (i = 1; i<=n; i++) {s[i] = 1; Add (i,1); } scanf ("%d", &m); while (m--) {scanf ("%s%d", op,&x); if (op[0]== ' Q ') {printf ("%d\n", SUM (Rig[x])-sum (lef[x]-1)); } else {if (s[x]) Add (lef[x],-1); else Add (lef[x],1); S[X]=!S[X]; }}} return 0;}
Poj3321:apple trees (tree-like array)