POJ3321-Apple Tree(dfs序+线段树)
来源:互联网 发布:儿童票卧铺半价算法 编辑:程序博客网 时间:2024/05/24 06:22
poj3321 原题链接:http://poj.org/problem?id=3321
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
题目大意:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <map>#include <cstdlib>#include <cstring>#define ll long long#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define MAXN 100005#define INF 1<<31using namespace std;vector <int> G[MAXN];bool vis[MAXN];int Sum,cnt,n,m,e[MAXN],q[MAXN],head[MAXN];struct node{ int val,next;}edge[MAXN<<1];struct Tree{ int l,r; int sum;}tree[MAXN<<2];void add_edge(int u,int v){ edge[cnt].val=v; edge[cnt].next=head[u]; head[u]=cnt; cnt++;}void dfs(int u){ ++Sum; q[u]=Sum; vis[u]=true; for(int i=head[u];i!=-1;i=edge[i].next){ if(vis[edge[i].val]) continue; dfs(edge[i].val); } e[u]=Sum;}void pushup(int rt){ tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;}void Build(int rt,int l,int r){ tree[rt].l=l;tree[rt].r=r; if(l==r){ tree[rt].sum=1; return; } int mid = (l+r)>>1; Build(lson); Build(rson); pushup(rt);}void Modify(int rt,int x){ int l=tree[rt].l,r=tree[rt].r; if(l==r){ if(tree[rt].sum){ tree[rt].sum=0; return; } else tree[rt].sum=1; return; } int mid=(l+r)>>1; if(x<=mid) Modify(rt<<1,x); else Modify(rt<<1|1,x); pushup(rt);}int Query(int rt,int l,int r){ 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(rt<<1,l,r); else if(l>mid) return Query(rt<<1|1,l,r); else return (Query(lson)+Query(rson));}int main(){ while(scanf("%d",&n)!=EOF){ cnt=0; int u,v; for(int i=1;i<=n;i++){ head[i]=-1; vis[i]=false; } for(int i=0;i<n-1;i++){ scanf("%d%d",&u,&v); add_edge(u,v); } Sum = 0; dfs(1); Build(1,1,n); char s[5]; int x; scanf("%d",&m); while(m--){ scanf("%s%d",s,&x); if(s[0]=='C') Modify(1,q[x]); else printf("q[%d]=%d e[%d]=%d\n",x,q[x],x,e[x]); printf("%d\n",Query(1,q[x],e[x])); } } return 0;}
- POJ3321-Apple Tree(dfs序+线段树)
- POJ3321/Apple tree/(DFS序+线段树)
- 线段树 poj3321 Apple Tree
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- 【POJ3321】Apple Tree 苹果树 线段树
- 【树状数组--dfs序转化】poj3321 Apple Tree
- poj3321 Apple Tree(DFS+树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- poj3321 Apple Tree(树状数组+dfs)
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- poj 3321 Apple Tree(dfs序+线段树)
- POJ3221——Apple Tree(线段树,dfs序)
- POJ 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree dfs序+线段树
- poj 3321 Apple Tree(dfs序+线段树)
- POJ 3321 Apple Tree dfs序+线段树
- 优秀程序员的博客有哪些?
- Java开发常见错误
- dev 禁止单元格编辑
- MFC-Error(1):'/ZI' and '/Gy-' command-line options are incompatible
- 一些寄存器的“小奥秘”
- POJ3321-Apple Tree(dfs序+线段树)
- Flume架构以及应用介绍
- Haproxy 在服务可用情况下返回503
- 比较IOS开发中常用视图的四种切换方式
- 在IDEA中创建Java 9 modules(模块)的Hello World
- 在ubuntu上构建并配置Spark的IDE开发环境
- 王爽 《汇编语言》 读书笔记 十六 直接定址表
- IO复用
- 了解hadoop与分布式系统