POJ 3321 Apple Tree
来源:互联网 发布:二元期权模拟软件 编辑:程序博客网 时间:2024/05/01 02:20
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
Source
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int lim=200011;int sumv[lim<<2],bt[lim],et[lim];struct self{int x,y;}s[lim<<1];int first[lim<<1],nxt[lim<<1];int m,n,a,b,c;char chin;void pushup(int o){sumv[o]=sumv[o<<1]+sumv[o<<1|1];}int x,y;int query(int o,int l,int r){ if(l>=x&&r<=y)return sumv[o]; int m=(l+r)>>1,ret=0; if(x<=m)ret+=query(o<<1,l,m); if(y>m)ret+=query(o<<1|1,m+1,r); return ret;}void update(int o,int l,int r){ if(l==r) { sumv[o]^=1; return; } int m=(l+r)>>1; if(x<=m)update(o<<1,l,m); else update(o<<1|1,m+1,r); pushup(o);}void makeside(int i,int x,int y){ s[i].x=x;s[i].y=y; nxt[i]=first[x]; first[x]=i;}void build(int o,int l,int r){ if(l==r) { sumv[o]=1; return; } int m=(l+r)>>1; build(o<<1,l,m); build(o<<1|1,m+1,r); pushup(o);}bool flag[lim];int t;void maketree(int i){ flag[i]=1; t++; bt[i]=t; for(int e=first[i];e!=-1;e=nxt[e]) if(!flag[s[e].y])maketree(s[e].y); et[i]=t;} int main(){ scanf("%d\n",&m); memset(first,-1,sizeof(first)); memset(nxt,-1,sizeof(nxt)); for(a=1;a<m;a++) { scanf("%d %d\n",&x,&y); makeside(a,x,y); makeside(a+m,y,x); } maketree(1); build(1,1,m); scanf("%d\n",&n); for(a=1;a<=n;a++) { scanf("%c %d\n",&chin,&b); x=bt[b],y=et[b]; if(chin=='Q')printf("%d\n",query(1,1,m)); else update(1,1,m); } return 0;}
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- Poj 3321 Apple Tree
- poj 3321 apple tree
- poj 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ--3321--Apple Tree
- POJ-3321-Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- 一个简单的makefile
- shell中条件判断if中的-z到-d的意思
- 由LG 的G2手机浅析国产旗舰机的方向
- Javascript 对象类型
- 用c语言写cgi程序(3)---实现文件上传
- POJ 3321 Apple Tree
- 不同的数字组成的圆圈中删除第m 个数字
- asp.net网站第一次加载慢的原因
- Project Euler problem 26
- JS滚轮事件onmousewheel
- 用c语言写cgi程序(4)---处理请求post get。文本框,下拉列表框。
- Code Refactor-编程命名(2)
- iOS7初体验(1)——第一个应用程序HelloWorld
- C#高级编程读书笔记 - 第五章泛型