POJ 3321 Apple Tree(树状数组)

                                                          Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27470   Accepted: 8140

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?

POJ 3321 Apple Tree(树状数组)

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
【分析】题意很简单,这里就不多说了。做法是先dfs编号,找出每个节点所包含的节点编号区间,然后就是树状数组的事了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 2e5+;
const int M = 4e5+;
int n,m,tot=,cnt=;
int head[N],x[N],y[N],r[N];
int tree[N];
struct EDG{
int to,next;
}edg[M];
inline void addedg(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void add(int k,int num){
while(k<=n){
tree[k]+=num;
//printf("####%lld\n",tree[k]);
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
void dfs(int u){
int v;
x[u]=++cnt;
for(int i=head[u];i!=-;i=edg[i].next){
v=edg[i].to;
if(!x[v])dfs(v);
}
y[u]=cnt;
return;
}
int main() {
met(tree,);met(head,-);met(x,);
int apple[N];
for(int i=;i<N;i++)apple[i]=;
int u,v;
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedg(u,v);addedg(v,u);
}
dfs();
for(int i=;i<=n;i++)add(i,);
scanf("%d",&m);
while(m--){
scanf("%s",str);
scanf("%d",&u);
if(str[]=='C'){
int l=x[u],rr=y[u];
if(apple[l]){
apple[l]=;
add(l,-);
}else {
apple[l]=;
add(l,);
}
}else {
int ans=Sum(y[u])-Sum(x[u]-);
printf("%d\n",ans);
}
}
return ;
}
上一篇:【转】Android官方下拉刷新控件 SwipeRefreshLayout


下一篇:Android——谷歌官方下拉刷新控件SwipeRefreshLayout(转)