Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29084 Accepted: 8633
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
Source
POJ Monthly–2007.08.05, Huang, Jinsong
题目大意:
给你一颗苹果树,树的主干设为1,每一个分支设为一个数,一直到N,代表这颗苹果树。每个分支上面只能最多有一个苹果,也就是一个枝子上面不可能有两个苹果,另外注意一点,不要把苹果树想象成二叉树,苹果树每个节点可以分出很多叉,应该是多叉树。
输入是叉之间的关系,
1 2
1 3
就是主干上面两个叉分别是2 和3.
下面是两种操作,Q 和C
C j 的意思是如果 j 这个枝子上面有苹果就摘下来,如果没有,那么就会长出新的一个
Q j 就是问 j 这个叉上面的苹果总数。
思路 邻接表建树
然后遍历此树 两个数组 in和out 记录这个点进入时间和出去的时间
在这个时间之间 访问的就是这个节点为根的子树
这样左区间in[i] 右区间out[i]
因为有单点更新 这题变成一个区间查询和维护的题目
这样就可以使用树状数组
思路可见此网站
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<malloc.h>
#define N 200010
using namespace std;
int n;
struct node {
int data;
struct node *next;
};
struct node *a[N],*p;
bool flag2[N];
int inn[N],outt[N],time=0,flag[N],c[N];
void init(){//邻接表
scanf("%d",&n);
for(int i=1;i<=n-1;i++){
int x,y;
scanf("%d%d",&x,&y);
p=(struct node*)malloc(sizeof(struct node));
p->data=y;
p->next=a[x];
a[x]=p;
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=a[y];
a[y]=p;
}
}
int lowbit(int x)
{
return x&-x;
}
void update(int i,int x){
while(i<=n){
c[i]+=x;
i+=lowbit(i);
}
}
int getsum(int i){
int sum=0;
while(i){
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
void dfs(int x){
flag[x]=1;
time++;
inn[x]=time;
struct node *q;
q=a[x];
while(q!=NULL){
int t=q->data;
if(!flag[t]) dfs(t);
q=q->next;
}
outt[x]=time;
}
void deal(){
for(int i=1;i<=n;i++)
update(inn[i],1);
memset(flag2,true,sizeof(flag2));
int m;
scanf("%d",&m);
char ch;
int nn;
for(int i=1;i<=m;i++){
getchar();
scanf("%c",&ch);
scanf("%d",&nn);
if(ch=='Q')cout<<getsum(outt[nn])-getsum(inn[nn]-1)<<endl;
if(ch=='C'){
if(flag2[inn[nn]]) update(inn[nn],-1);//是inn[nn]不是nn
else update(inn[nn],1);
flag2[inn[nn]]=!flag2[inn[nn]];
}
}
}
int main(){
init();
dfs(1);
deal();
return 0;
}
当然 线段树做法 一个意思 就是代码长
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<malloc.h>
#define N 200010
using namespace std;
int n;
int inn[N],outt[N],time=0,flag[N],c[N],b[N],p[N],nt[N],num;
struct node{
int left;
int right;
int sum;
}tree[4*N];
void init(){
scanf("%d",&n);
for(int i=1;i<=n-1;i++){
int x,y;
scanf("%d%d",&x,&y);
num++;
b[num]=y;
nt[num]=p[x];
p[x]=num;
num++;
b[num]=x;
nt[num]=p[y];
p[y]=num;
}
}
void dfs(int x){
flag[x]=1;
time++;
inn[x]=time;
int e=p[x];
while(e>0){
int k=b[e];
if(!flag[k]) dfs(k);
e=nt[e];
}
outt[x]=time;
}
void build(int l,int r,int p){
tree[p].left=l;tree[p].right=r;
if(l==r) {tree[p].sum=1;return;}
if(l<r){
int mid=(l+r)/2;
build(l,mid,2*p);
build(mid+1,r,2*p+1);
tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
}
void update(int l,int r,int p,int k){
if(tree[p].left==l&&tree[p].right==r){
tree[p].sum+=k;return;
}
int mid=(tree[p].left+tree[p].right)/2;
if(r<=mid)update(l,r,2*p,k);
else if(l>mid) update(l,r,2*p+1,k);
else {
update(l,mid,2*p,k);
update(r,mid+1,2*p+1,k);
}
tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
}
int query(int l,int r,int p){
if(tree[p].left==l&&tree[p].right==r) return tree[p].sum;
int mid=(tree[p].left+tree[p].right)/2;
if(r<=mid) return query(l,r,2*p);
else if(l>mid)return query(l,r,2*p+1);
else return query(l,mid,2*p)+query(mid+1,r,2*p+1);
}
void deal(){
build(1,n,1);
memset(flag,true,sizeof(flag));
int m;
scanf("%d",&m);
char ch;
int nn;
for(int i=1;i<=m;i++){
getchar();
scanf("%c",&ch);
scanf("%d",&nn);
if(ch=='Q')cout<<query(inn[nn],outt[nn],1)<<endl;
if(ch=='C'){
if(flag[inn[nn]]) update(inn[nn],inn[nn],1,-1);
else update(inn[nn],inn[nn],1,1);
flag[inn[nn]]=!flag[inn[nn]];
}
}
}
int main(){
init();
dfs(1);
deal();
return 0;
}