POJ 3321 Apple Tree(DFS序 ,修改节点值,子树求和)
来源:互联网 发布:淘宝店铺要交税吗 编辑:程序博客网 时间:2024/06/04 19:57
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
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
题意:两种操作,更新,求和,DFS序的简单应用。
题解:简单的DFS之后就是BIT单点更新和求和问题。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=1e5+100;int c[maxn*2+100];int L[maxn],R[maxn],cnt;struct node{ int u,v; int next;}e[maxn*2+10];int head[maxn];void addedge(int u,int v){ e[cnt].u=u;e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++;}int n,m,dfn;void dfs(int u,int pre){ L[u]=++dfn; for(int i=head[u];i!=-1;i=e[i].next) { int to=e[i].v; if(to==pre) continue; dfs(to,u); } R[u]=++dfn;}int lowbit(int x){ return x&(-x);}void update(int x,int val){ while(x<=dfn) { c[x]+=val; x+=lowbit(x); }}int query(int x){ int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum;}void init(){ CLEAR(head,-1); dfn=cnt=0;}int main(){ int x,y,op;char str[2]; while(~scanf("%d",&n)) { init(); REP(i,n-1) { scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } dfn=0; dfs(1,-1); CLEAR(c,0); for(int i=1;i<=dfn;i++) update(i,1); scanf("%d",&m); while(m--) { scanf("%s%d",str,&op); if(str[0]=='C') { if(query(R[op])-query(R[op]-1)==1) { update(R[op],-1); update(L[op],-1); } else { update(R[op],1); update(L[op],1); } } else { int ans=query(R[op])-query(L[op]-1); printf("%d\n",ans/2); } } } return 0;}/**/
- POJ 3321 Apple Tree(DFS序 ,修改节点值,子树求和)
- poj 3321 Apple Tree(dfs序+树状数组求和模型)
- 【POJ 3321】【dfs序(讲解)+(树状数组或者线段树)】Apple Tree【给你一颗树,最初每个节点上都有一个苹果,有两种操作单点修改和查询子树的苹果个数】
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
- POJ 3321 Apple Tree DFS序+fenwick
- POJ 3321(Apple Tree-dfs序)
- POJ 3321 Apple Tree(dfs序+BIT)
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321-Apple Tree(DFS序+树状数组)
- poj 3321 Apple Tree(dfs序+树状数组)
- 【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)
- POJ 3321 Apple Tree dfs序的应用
- POJ 3321 Apple Tree(dfs序+树状数组)
- 【poj 3321】 Apple Tree 树状数组+dfs序
- poj 3321 Apple Tree(dfs序+线段树)
- KVO的底层实现原理
- android图片处理方法
- Java基础之多线程2
- HALCON11+VS2012配置
- Linux系统下挂载光盘
- POJ 3321 Apple Tree(DFS序 ,修改节点值,子树求和)
- 11.Eclipse安装了genymotion 没有图标显示?
- 分别修改Cube每个面的贴图UV(Unity3D开发之十八)
- [CF 297E]Mystic Carvings解题报告
- 平衡二叉树之一(基本性质、查询、添加)
- HDOJ 2899 Strange fuction(二分,求导。。)
- 黑马程序员——c语言宏定义
- GitLab 官方安装文档中文翻译
- RANSAC算法简介