POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
来源:互联网 发布:电驴默认端口 编辑:程序博客网 时间:2024/05/17 01:38
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
题目连接:POJ 3321
很久以前就想做,但不知道如何转换,跟另外一题差不多的意思,主要是进行单点修改的应该怎么写要知道据说树状数组更快,然而并不是是很懂树状数组的原理,只会模版,就直接用线段树做了。
代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<sstream>#include<cstring>#include<cstdio>#include<string>#include<deque>#include<stack>#include<cmath>#include<queue>#include<set>#include<map>using namespace std;#define INF 0x3f3f3f3f#define MM(x,y) memset(x,y,sizeof(x))#define LC(x) (x<<1)#define RC(x) ((x<<1)+1)#define MID(x,y) ((x+y)>>1)typedef pair<int,int> pii;typedef long long LL;const double PI=acos(-1.0);const int N=100010;struct seg{int l,mid,r;int sum;};seg T[N<<2];struct edge{int to;int pre;}E[N];int head[N],cnt,in[N],out[N],total;void add(int s,int t){E[cnt].to=t;E[cnt].pre=head[s];head[s]=cnt++;}void init(){MM(head,-1);cnt=0;MM(in,0);MM(out,0);total=0;}void dfs(int now,int fa){in[now]=++total;for (int i=head[now]; ~i; i=E[i].pre){int v=E[i].to;if(v!=fa){dfs(v,now);}}out[now]=total;}void pushup(int k){T[k].sum=T[LC(k)].sum+T[RC(k)].sum;}void build(int k,int l,int r){T[k].l=l;T[k].r=r;T[k].mid=MID(l,r);if(l==r)T[k].sum=1;else{build(LC(k),l,T[k].mid);build(RC(k),T[k].mid+1,r);pushup(k);}}void update(int k,int x){if(T[k].l==T[k].r&&T[k].l==x)T[k].sum^=1;else{if(x<=T[k].mid)update(LC(k),x);elseupdate(RC(k),x);pushup(k);}}int query(int k,int l,int r){if(l<=T[k].l&&T[k].r<=r)return T[k].sum;else{if(r<=T[k].mid)return query(LC(k),l,r);else if(l>T[k].mid)return query(RC(k),l,r);elsereturn query(LC(k),l,T[k].mid)+query(RC(k),T[k].mid+1,r);}}int main(void){int n,m,q,i,x,y,c;char ops[3];while (~scanf("%d",&n)){init();for (i=0; i<n-1; ++i){scanf("%d%d",&x,&y);add(x,y);}dfs(1,-1);build(1,1,n);scanf("%d",&m);while (m--){scanf("%s",ops);if(ops[0]=='C'){scanf("%d",&c);update(1,in[c]);}else if(ops[0]=='Q'){scanf("%d",&q);printf("%d\n",query(1,in[q],out[q]));}}}return 0;}
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
- 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序+线段树
- 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 2155 Matrix 二维线段树 区间修改 单点查询
- [poj 3321]Apple Tree[模拟DFS][时间戳][线段树]
- poj 3264 Balanced Lineup(线段树单点修改区间查询)
- 【数据结构】【线段树】单点修改区间查询
- 黑马程序员杭州校区,喜迎PHP 学科入驻
- Java容器二:LinkedList
- IIS部署到服务器的流程及可能遇到的问题及解决方法
- C#中如何获取数据库中表的信息和列的信息
- mysql 主从原理
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- iOS 开发者文档翻译 URL Session的生命周期
- 链表基本操作(删除,插入,查找)
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
- mapreduce生命周期的解释及其优化
- Leach协议在ns2下函数调用视图
- Redis 集群规范(中文稿)
- java设计模式——工厂方法模式(Factory Method Pattern)