您的位置:首页 > 其它

POJ 3321 线段树or树状数组

2016-02-15 13:08 302 查看

题目

Apple Tree

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 23013 Accepted: 6989

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

题意

有个苹果树,开始每个叶子结点上都有一个苹果。然后有两种操作,一种是把节点上的苹果处理(有就摘掉,没有就长出来)。另一种操作是问一个节点的所有子树上一共有多少个苹果。

题解

这种节点上所有子树的题目,先dfs一下给每个节点表上记号。那么每个节点的子树是一个连续区间。就可以用线段树或者树状数组求区间和了。

线段树:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <cstdlib>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)

using namespace std;
#define SIZE 100000

struct Po{
int v;
Po* next;
};

Po* G[SIZE+10] = {NULL};
int L[SIZE+10];
int R[SIZE+10];
int cnt;
int NN;
int T[SIZE*8+100];

void Edge_add(int a,int k){
Po* tem = (Po*)malloc(sizeof(Po));
tem->v = k;
tem->next = G[a];
G[a] = tem;
}

void dfs(int cur){
//    vector<int>::iterator it = G[cur].begin();
Po* it = G[cur];
for(;it!=NULL;it = it->next){
int k = it->v;
if(L[k]==-1){
L[k] = cnt++;
dfs(k);
}
}
R[cur] = cnt-1;
}

int query(int LL,int RR){
int sum = 0;
for(LL = LL-1+NN,RR = RR+1+NN;LL^RR^1;LL>>=1,RR>>=1){
if(~LL&1)
sum+=T[LL^1];
if(RR&1)
sum+=T[RR^1];
}
return sum;
}

void T_add(int t){
for(T[t+=NN] ^= 1,t>>=1;t;t>>=1)
T[t] = T[t<<1] + T[t<<1|1];
}

void init(int n){
NN = 1;
while(NN<=n+1)NN<<=1;
m0(T);
int i;
f(i,1,n)
T_add(i);
}

int main()
{
//  ios_base::sync_with_stdio(false); cin.tie(0);

int n;

scanf("%d",&n);
if(!n)return 0;
int i;
f(i,1,n-1){
int a,b;
scanf("%d%d",&a,&b);
Edge_add(a,b);
Edge_add(b,a);
}
cnt = 2;

m_1(L);
m_1(R);
L[1] = 1;
dfs(1);

init(n);
int m;
scanf("%d",&m);
char c[10];
int a;
f(i,1,m){
scanf("%s%d",c,&a);
if(c[0]=='Q'){
printf("%d\n",query(L[a],R[a]));
}else{
T_add(L[a]);
}
}
return 0;
}


树状数组:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <cstdlib>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)

using namespace std;
#define SIZE 100000

struct Po{
int v;
Po* next;
};

Po* G[SIZE+10] = {NULL};
int L[SIZE+10];
int R[SIZE+10];
int cnt;
int p[SIZE+100];
int T[SIZE+100];

void Edge_add(int a,int k){
Po* tem = (Po*)malloc(sizeof(Po));
tem->v = k;
tem->next = G[a];
G[a] = tem;
}

int T_sum(int k){
int sum = 0;
for(;k;k-=lowbit(k))
sum+=T[k];
return sum;
}

void dfs(int cur){
Po* it = G[cur];
for(;it!=NULL;it = it->next){
int k = it->v;
if(L[k]==-1){
L[k] = cnt++;
dfs(k);
}
}
R[cur] = cnt-1;
}

int query(int LL,int RR){
return T_sum(RR)-T_sum(LL-1);
}

void T_add(int t,int n){
int i;
if(p[t]==0){
T[t]++;
for(i=t+lowbit(t);i<=n;i+=lowbit(i))
T[i]++;
}else{
T[t]--;
for(i=t+lowbit(t);i<=n;i+=lowbit(i))
T[i]--;
}
p[t]^=1;
}

void init(int n){
int i;
f(i,1,n){
T[i] = lowbit(i);
p[i] = 1;
}
}

int main()
{
//  ios_base::sync_with_stdio(false); cin.tie(0);

int n;

scanf("%d",&n);
if(!n)return 0;
int i;
f(i,1,n-1){
int a,b;
scanf("%d%d",&a,&b);
Edge_add(a,b);
Edge_add(b,a);
}
cnt = 2;

m_1(L);
m_1(R);
L[1] = 1;
dfs(1);

init(n);

int m;
scanf("%d",&m);
char c[10];
int a;
f(i,1,m){
scanf("%s%d",c,&a);
if(c[0]=='Q'){
printf("%d\n",query(L[a],R[a]));
}else{
T_add(L[a],n);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj