POJ 3321 Apple Tree (线段树 & 树状数组)
2013-04-24 11:49
330 查看
Apple Tree
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 |
思路:
树状数组。这道题重点怎么建立树到树状数组的映射关系:利用dfs遍历树,对每个节点进行两次编号,第一次搜到第i个节点时的深度dep,为这个节点管辖区间的上限low[i](也为这个节点的下标),然后搜这个节点的子节点,最后搜回来后的深度dep,为这个节点管辖区间的下限high[i],如下图所示。接下来就是树状数组部分了。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=100010; struct Edge{ int to,nxt; }edge[N<<1]; int n,m,cnt,head ,array ; int dep,have ,low ,high ,vis ; //low[i]代表左顶点 , high[i]代表右顶点 void addedge(int cu,int cv){ edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++; } void DFS(int u){ low[u]=++dep; //第一次的深度,即左顶点 vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt){ while(!vis[edge[i].to]) DFS(edge[i].to); } high[u]=dep; //返回的深度,即右顶点 } int lowbit(int x){ return x&(-x); } void add(int rt,int val){ while(rt<=n){ array[rt]+=val; rt+=lowbit(rt); } } int sum(int rt){ int ans=0; while(rt>0){ ans+=array[rt]; rt-=lowbit(rt); } return ans; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ cnt=0; memset(head,-1,sizeof(head)); dep=0; memset(have,0,sizeof(have)); memset(vis,0,sizeof(vis)); memset(array,0,sizeof(array)); for(int i=1;i<=n;i++){ have[i]=1; //1表示有苹果 add(i,1); } int u,v; for(int i=1;i<n;i++){ scanf("%d%d",&u,&v); addedge(u,v); } DFS(1); scanf("%d",&m); char op[3]; int id; while(m--){ scanf("%s%d",op,&id); if(op[0]=='Q') printf("%d\n",sum(high[id])-sum(low[id]-1)); else{ if(have[id]){ add(low[id],-1); have[id]=0; }else{ add(low[id],1); have[id]=1; } } } } return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) const int N=100010; struct node{ int l,r; int have,sum; }tree[N<<2]; struct Edge{ int to,nxt; }edge[N<<1]; int n,m,cnt,head ; int dep,vis ,low ,high ; void addedge(int cu,int cv){ edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++; } void PushUp(int rt){ tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum; } void build(int L,int R,int rt){ tree[rt].l=L; tree[rt].r=R; tree[rt].have=1; if(tree[rt].l==tree[rt].r){ tree[rt].sum=1; return ; } int mid=(L+R)>>1; build(L,mid,L(rt)); build(mid+1,R,R(rt)); PushUp(rt); } void PushDown(int rt){ tree[L(rt)].have=1; tree[L(rt)].sum=tree[L(rt)].r-tree[L(rt)].l+1; tree[R(rt)].have=1; tree[R(rt)].sum=tree[R(rt)].r-tree[R(rt)].l+1; tree[rt].have=0; } void update(int id,int rt){ if(tree[rt].l==id && tree[rt].r==id){ if(tree[rt].have==1){ tree[rt].have=0; tree[rt].sum=0; }else{ tree[rt].have=1; tree[rt].sum=1; } return ; } if(tree[rt].have) PushDown(rt); int mid=(tree[rt].l+tree[rt].r)>>1; if(id<=mid) update(id,L(rt)); else update(id,R(rt)); PushUp(rt); if(tree[L(rt)].have && tree[R(rt)].have) tree[rt].have=1; } int query(int L,int R,int rt){ if(tree[rt].l==L && tree[rt].r==R){ return tree[rt].sum; } int mid=(tree[rt].l+tree[rt].r)>>1; if(R<=mid) return query(L,R,L(rt)); else if(L>=mid+1) return query(L,R,R(rt)); else{ int a=query(L,mid,L(rt)); int b=query(mid+1,R,R(rt)); return a+b; } } void DFS(int u){ low[u]=++dep; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].nxt){ while(!vis[edge[i].to]) DFS(edge[i].to); } high[u]=dep; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ cnt=0; memset(head,-1,sizeof(head)); dep=0; memset(vis,0,sizeof(vis)); int u,v; for(int i=1;i<n;i++){ scanf("%d%d",&u,&v); addedge(u,v); } DFS(1); build(1,high[1],1); char op[3]; int id; scanf("%d",&m); while(m--){ scanf("%s%d",op,&id); if(op[0]=='Q') printf("%d\n",query(low[id],high[id],1)); else update(low[id],1); } } return 0; }
相关文章推荐
- POJ 2352_Stars && POJ-2481 Cows (线段树单点更新+树状数组)
- HDU 1166 敌兵布阵 (线段树 & 树状数组)
- POJ2352 Stars(线段树 & 树状数组)
- 线段树 && 树状数组
- codeforces 629D-Babaei and Birthday Cake(dp && 线段树或树状数组离散优化)
- HDU 1166 敌兵布阵 (线段树 & 树状数组)
- HDU 1754 I Hate It (线段树 & 树状数组)
- HDU1166 敌兵布阵 非递归线段树 & 递归线段树 & 树状数组
- HDU 1166 敌兵布阵(区间求和&(线段树|树状数组))
- codevs1228 苹果树(dfs序&&(线段树||树状数组))
- 小结:线段树 & 主席树 & 树状数组
- NYOJ 116 士兵杀敌(二) 树状数组 & 线段树
- [树状数组套权值线段树 || 分块] BZOJ 2120 数颜色 & BZOJ 2453 维护队列
- 【南理工oj】108 - 士兵杀敌(一)(线段树 & 树状数组)
- POJ2481 Cows(线段树 & 树状数组)
- hdu 1166(线段树或者树状数组)
- HDOJ 题目5172 GTY's gay friends(线段树)
- HDU 4122 Alice's mooncake shop // RMQ 线段树
- UVALive - 3938 Ray, Pass methe dishes!"(动态最大连续和子序列,线段树区间合并)
- 【树状数组套线段树】数列