【poj 3321】 Apple Tree 树状数组+dfs序
来源:互联网 发布:mac如何查看qq群相册 编辑:程序博客网 时间:2024/05/16 12:31
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23568 Accepted: 7118
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
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
题目链接:http://poj.org/problem?id=3321
题意:
给出一个苹果树,每个节点一开始都有苹果
C X,如果X点有苹果,则拿掉,如果没有,则新长出一个
Q X,查询X点与它的所有后代分支一共有几个苹果
思路:1为根节点,子树问题用dfs序,然后区间查询,单点修改–》树状数组
*注意!!
typedef vector<int> ve_int;vector<ve_int> lin(100005);!vector定义 ,不然TLE!
代码
#include<iostream>#include<vector>#include<stdio.h>#include<string.h>using namespace std;int in[100005],out[100005];int C[100005];int a[100005];int n,m;int tot=0;typedef vector<int> ve_int;//---!!!vector<ve_int> lin(100005);void dfs(int x,int fa){ in[x]=++tot; int len=lin[x].size(); for(int i=0;i<len;i++) { int y=lin[x][i]; if(y==fa) continue; dfs(y,x); } out[x]=tot;}void add(int x,int y){ while(x<=n) { C[x]+=y; x+=x&(-x); }}int _get(int x){ int ret=0; while(x>0) { ret+=C[x]; x-=x&(-x); } return ret;}int main(){ int aa,bb; scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d%d",&aa,&bb); lin[aa].push_back(bb); lin[bb].push_back(aa); } dfs(1,0); char s[3]; scanf("%d",&m);// for(int i=1;i<=n;i++)// cout<<i<<" "<<in[i]<<" "<<out[i]<<endl; for(int i=1;i<=m;i++) { scanf("%s%d",s,&aa); if(s[0]=='Q') printf("%d\n",out[aa]-in[aa]+1-(_get(out[aa])-_get(in[aa]-1))); else { if(a[aa]==0) { add(in[aa],1); a[aa]=1; } else { add(in[aa],-1); a[aa]=0; } } }}
- 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 Apple Tree (3321)(树状数组+dfs)
- poj 3321 Apple Tree (dfs+树状数组)
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321 Apple Tree(dfs序+树状数组)
- 【poj 3321】 Apple Tree 树状数组+dfs序
- POJ 3321Apple Tree(dfs序 树状数组)
- poj 3321 Apple Tree(dfs序+树状数组求和模型)
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- 【poj】 3321 Apple Tree dfs序+树状数组
- POJ 3321-Apple Tree(DFS序+树状数组)
- DPDK和DNS服务器关系
- Twitter 工程师谈 JVM 调优
- Android Log升级版
- R——Matrix
- C# Dictionary
- 【poj 3321】 Apple Tree 树状数组+dfs序
- mysql中drop、truncat、delete的用法区别
- 虚拟机调用函数是栈堆之间的变化
- 【新人笔记16.04.05】JS--事件绑定
- CSS
- NDK-JNI实战教程(三) 从比Hello World稍复杂点儿的NDK例子说说模板
- shell脚本中for循环后面冒号的问题
- 使用ab进行页面的压力测试
- Tomcat学习总结