POJ 题目3321 Apple Tree(线段树)
2015-08-11 08:46
337 查看
Apple Tree
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 toN 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 forku 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 forkx, 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
Sample Output
Source
POJ Monthly--2007.08.05, Huang, Jinsong
题目大意给你一棵树,每个节点开始的时候有一个苹果,下边m个操作,Q a,查询以a和a的子树的总共的苹果数,c b,修改操作,改变b节点,,开始在有变没有,没有变成有
ac代码
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21566 | Accepted: 6548 |
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 toN 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 forku 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 forkx, 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
题目大意给你一棵树,每个节点开始的时候有一个苹果,下边m个操作,Q a,查询以a和a的子树的总共的苹果数,c b,修改操作,改变b节点,,开始在有变没有,没有变成有
ac代码
#include<stdio.h> #include<string.h> struct node { int u,v,next; }edge[100010<<1]; int head[100010],cnt,num[100010],son[100010],cc,vis[100100]; void add(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void dfs(int u) { num[u]=++cc; //son[u]=1; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!vis[v]) dfs(v); // son[u]+=son[v]; } son[u]=cc; } struct s { int sum,cover; }node[100010<<2]; void pushdown(int tr,int m) { if(node.cover) { node[tr<<1].cover=node[tr<<1|1].cover=1; node[tr<<1].sum=m-(m>>1); node[tr<<1|1].sum=(m>>1); node .cover=0; } } void build(int l,int r,int tr) { node .cover=1; node .sum=r-l+1; if(l==r) { return; } int mid=(l+r)>>1; build(l,mid,tr<<1); build(mid+1,r,tr<<1|1); } void update(int pos,int l,int r,int tr) { if(l==pos&&r==pos) { if(node .cover) { node .cover=0; node .sum=0; } else { node .cover=1; node .sum=1; } return; } pushdown(tr,r-l+1); int mid=(l+r)>>1; if(pos<=mid) update(pos,l,mid,tr<<1); else update(pos,mid+1,r,tr<<1|1); node .sum=node[tr<<1].sum+node[tr<<1|1].sum; if(node[tr<<1].cover&&node[tr<<1|1].cover) { node .cover=1; } } int query(int L,int R,int l,int r,int tr) { if(L<=l&&R>=r) { return node .sum; } int mid=(l+r)>>1; pushdown(tr,r-l+1); int ans=0; if(L<=mid) ans+=query(L,R,l,mid,tr<<1); if(R>mid) ans+=query(L,R,mid+1,r,tr<<1|1); return ans; /*if(R<=mid) return query(L,R,l,mid,tr<<1); else if(L>mid) return query(L,R,mid+1,r,tr<<1|1); else return query(L,mid,l,mid,tr<<1)+query(mid+1,R,mid+1,r,tr<<1|1);*/ } int main() { int n; while(scanf("%d",&n)!=EOF) { cnt=0; cc=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); int i; for(i=0;i<n-1;i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); } dfs(1); int m; scanf("%d",&m); build(1,n,1); while(m--) { char s[2]; scanf("%s",s); if(s[0]=='Q') { int a; scanf("%d",&a); int ans=query(num[a],son[a],1,n,1); printf("%d\n",ans); } else { int a; scanf("%d",&a); update(num[a],1,n,1); } } } } 相关文章推荐
- swift_将UIDatePicker到达的传播之间的时间差在数小时出现页面的事
- android ContentProvider的使用
- A006-AndroidManifest.xml解析
- cordova ios升级插件
- Android研究之手PullToRefresh(ListView GridView 下拉刷新)使用详解
- Android Api Demos登顶之路(二十九)Share Action Provider
- CodingTipsAndTricks android ksoap2
- Android调用相册或相机拍照选取照片并裁剪
- Android Device Policy Administration Tutorial
- oschina iOS代码库
- oschina iOS代码库
- nagios插件之登陆SBC监控电话数
- Android数据库 之 SQLite数据库
- Android中proc/meminfo的详解(原)
- Android中显示和隐式Intent的使用
- 关于Android程序真机不兼容性问题总结及手机ROOT方法
- iOS-CocoaPods第三方库管理工具的使用,(很多公司都用这个)
- iOS-NSThread多线程
- 微信商城无法使用支付宝支付已经成为过去式
- iOS CLLocationManager定位,IOS8注意