POJ 3321 树状数组(+dfs+重新建树)

时间:2023-12-27 13:25:13
Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27092   Accepted: 8033

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 树状数组(+dfs+重新建树)

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong
题意:
一棵苹果树,每个节点最多结一个苹果,最初每个节点都有一个苹果,非二叉树,有两种操作,Q X,表示X节点的子树共有几个苹果,C X,表示X节点摘掉一个苹果或长出一个苹果(有就摘,没有就长)
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADHAagDASIAAhEBAxEB/8QA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TUNeutRszp7zXEyN9vLo4kChVihIAhYAfO4J38elevjUbJcf6bbH6SrXNH4LeDCCP7Chwev72T/4qk/4Up4LOM6DAccD95Jx/wCPUXHdGb8NdUsk8R+Owby3B/tgnHmr/wA809671tWsv+f23H1lWuPi+BXgSCaaaPw3axyzNvkZGcFz6n5uTU//AApjwZ/0A4h9JZP/AIqkTocJ4s+Hl9q3xhsNa0zVrWw8PSS2+o6ntul3XF1CjRKCm3IHl+UMhh9zkGvZV1axAP8Aplvx/wBNV/xrlf8AhTHg3PGhxe/72T/4qm/8KV8FndnQYTu6/vJP/iqA0MX9oPUrSX4X3CpdQsf7U0ngSA/8xK2969RBzXEf8KU8E5Unw/bNtdJAHZ2G5GDKcE4OGUEe4rtwMCkIWiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA//9k=" alt="" />
代码:
 /*
挺好的一道题,本题要重新建立树状数组,用dfs从树根开始搜,每搜到一个点他的左值记录该点的新序号,他的
右值记录改点的子树中的最大深度,这样每个点就变成了树状数组中的C数组参考上图,然后套树状数组区间求和的模板就行了。
注意本题如果用vector会很费时,vector<int>a[MAX]会超时,vector<vector<int> > a(MAX) 985ms过了(后面这个什么鬼!)。
不如自己建树用时会更少。
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int MAX=;
int c[MAX];
int lef[MAX],rig[MAX];
int n,m;
int rot;
int head[MAX];
int lne;
//vector<int>a[MAX];
//vector<vector<int> > a(MAX);
struct node
{
int to,next;
}a[MAX];
void tadd(int u,int v)
{
a[lne].to=v;
a[lne].next=head[u];
head[u]=lne++;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int id,int val)
{
while(id<=n)
{
c[id]+=val;
id+=lowbit(id);
}
}
int sum(int id)
{
int sum=;
while(id>)
{
sum+=c[id];
id-=lowbit(id);
}
return sum;
}
void dfs(int x)
{
lef[x]=rot;
//for(int i=0;i<a[x].size();i++)
for(int i=head[x];i!=-;i=a[i].next)
{
rot++;
//dfs(a[x][i]);
dfs(a[i].to);
}
rig[x]=rot;
}
int main()
{
int x,y;
char ch[];
int flag[MAX];
scanf("%d",&n);
for(int i=;i<=n;i++)
{
flag[i]=;
add(i,);
}
lne=;
memset(head,-,sizeof(head));
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
//a[x].push_back(y);
tadd(x,y);
}
rot=;
dfs();
scanf("%d",&m);
while(m--)
{
scanf("%s%d",ch,&x);
if(ch[]=='C')
{
if(flag[lef[x]])
add(lef[x],-);
else add(lef[x],);
flag[lef[x]]=!flag[lef[x]];
}
else
{
printf("%d\n",sum(rig[x])-sum(lef[x]-));
}
}
return ;
}