Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23212 | Accepted: 7050 |
Descriptionios
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.app
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?ide
Inputui
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 beginningspa
Outputcode
Sample Inputblog
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #define N 100010 using namespace std; int n,a[N],tot,lef[N],rig[N],vis[N]; vector<vector<int> >v(N); ///最开始用了vector,可是一直不过,百度了某大牛的代码,才发现和本身有一样的问题,改为这样就过了 int lowbit(int k) { return k&(-k); } int getSum(int i) { int res=0; while(i>0) { res+=a[i]; i-=lowbit(i); } return res; } void add(int i) { while(i<=n) { a[i]++; i+=lowbit(i); } } void eat(int i) { while(i<=n) { a[i]--; i+=lowbit(i); } } void dfs(int i) ///深搜对于每个节点进行编号 { lef[i]=tot; for(int j=0;j<v[i].size();j++) { tot++; dfs(v[i][j]); } rig[i]=tot; } int main() { int m; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); memset(lef,0,sizeof(lef)); memset(rig,0,sizeof(rig)); memset(a,0,sizeof(a)); for(int i=0; i<N; i++) v[i].clear(); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); } tot=1; dfs(1); for(int i=1;i<=n;i++) { vis[i]=1; add(i); } for(int i=1;i<=n;i++) { cout<<lef[i]<<" "<<rig[i]<<endl; } scanf("%d",&m); while(m--) { char op[5]; int q; scanf("%s%d",op,&q); if(!strcmp(op,"Q")) printf("%d\n",getSum(rig[q])-getSum(lef[q]-1)); if(!strcmp(op,"C")) { if(vis[q]) eat(lef[q]); else add(lef[q]); vis[q]=!vis[q]; } } } }
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Outputip
3 2
题目大意:一棵苹果树上有n个苹果,每一个叉上都最多只能有1个苹果,1为根节点。如今有2种操做,Q为求以某一节点为根节点的子树上全部苹果的数量和。C是更新某一点上的苹果数量,若是原先为0则长出来一个苹果,不然则被吃掉。get
思路:咱们发现本题中的树为一个多叉树,而树的先序遍历即dfs则能够知道以某一节点为根节点的子树所包含的全部节点编号。因此咱们用一个left和right数组记录i号苹果在先序遍历下的子树中的最小编号以及最大编号,即它的祖先和本身子树中的全部编号的最大值,而后求和时候用getSum(right[i])-getSum(left[i]-1)便可。
代码: