POJ 3321 线段树or树状数组
2016-02-15 13:08
302 查看
题目
Apple TreeTime 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; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- 【高手回避】poj3268,一道很水的dijkstra算法题