码迷,mamicode.com
首页 > 移动开发 > 详细

Apple Tree(需要预处理的树状数组)

时间:2015-03-21 21:20:31      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20335   Accepted: 6182

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 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


  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <string>
  7 #include <vector>
  8 #include <stack>
  9 #include <queue>
 10 #include <set>
 11 #include <map>
 12 #include <list>
 13 #include <iomanip>
 14 #include <cstdlib>
 15 #include <sstream>
 16 using namespace std;
 17 typedef long long LL;
 18 const int INF=0x5fffffff;
 19 const double EXP=1e-6;
 20 const int MS=200005;
 21 int cnt;
 22 vector<vector<int> > edge(MS/2);
 23 int flag[MS/2];
 24 int L[MS],R[MS];
 25 int c[MS];
 26 
 27 void dfs(int cur)    //    用dfs区间划分
 28 {
 29       L[cur]=++cnt;
 30       for(int i=0;i<edge[cur].size();i++)
 31             dfs(edge[cur][i]);
 32       R[cur]=++cnt;
 33 }
 34 
 35 int lowbit(int x)
 36 {
 37       return x&(-x);
 38 }
 39 
 40 void updata(int x,int d)
 41 {
 42       while(x<=cnt)
 43       {
 44             c[x]+=d;
 45             x+=lowbit(x);
 46       }
 47 }
 48 
 49 int getsum(int x)
 50 {
 51       int ret=0;
 52       while(x>0)
 53       {
 54             ret+=c[x];
 55             x-=lowbit(x);
 56       }
 57       return ret;
 58 }
 59 
 60 int main()
 61 {
 62       int N,M,x,y;
 63       scanf("%d",&N);
 64       for(int i=0;i<N-1;i++)
 65       {
 66             scanf("%d%d",&x,&y);
 67             edge[x].push_back(y);
 68       }
 69       cnt=0;
 70       dfs(1);
 71       memset(c,0,sizeof(c));
 72       for(int i=1;i<=N;i++)
 73       {
 74             updata(L[i],1);
 75             updata(R[i],1);
 76       }
 77       scanf("%d",&M);
 78       char cmd[MS];
 79       for(int i=1;i<=N;i++)
 80             flag[i]=1;
 81       while(M--)
 82       {
 83             scanf("%s%d",cmd,&x);
 84             if(cmd[0]==Q)
 85             {
 86                   int t1=getsum(L[x]-1);
 87                   int t2=getsum(R[x]);
 88                   printf("%d\n",(t2-t1)/2);
 89             }
 90             else
 91             {
 92                   if(flag[x])
 93                   {
 94                         updata(L[x],-1);
 95                         updata(R[x],-1);
 96                         flag[x]=0;
 97                   }
 98                   else
 99                   {
100                         updata(L[x],1);
101                         updata(R[x],1);
102                         flag[x]=1;
103                   }
104             }
105       }
106       return 0;
107 }

 

Apple Tree(需要预处理的树状数组)

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4356161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!