poj_3321 Apple Tree(dfs序+树状数组)
来源:互联网 发布:王者引擎全套源码 编辑:程序博客网 时间:2024/06/14 05:43
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?
![](http://poj.org/images/3321_1.gif)
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
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
树上统计更新问题。
这道题只需要求树上某结点与其子树的信息,所以可以用 dfs序+树状数组/线段树 维护。
这里有张图画得很清楚
而如果是树上两点之间的查询,好像可以用树链剖分维护。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{ int l, r;}interval[maxn];int n, m;//vector<int> vec[maxn]; 这样会超时typedef vector<int>ve; vector<ve>vec(100050);bool vis[maxn];int cot;int c[maxn];int lowbit(int x){ return x&(-x);}void update(int x, int d){ for(int i = x; i <= n; i += lowbit(i)) c[i] += d;}int Sum(int x){ int res = 0; for(int i = x; i >= 1; i -= lowbit(i)) res += c[i]; return res;}void dfs(int u){ cot++, vis[u] = 1; interval[u].l = cot; for(int i = 0; i < vec[u].size(); i++) { if(!vis[vec[u][i]]) dfs(vec[u][i]); } interval[u].r = cot;}int main(){ while(~scanf("%d", &n)) { cot = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) vec[i].clear(); int u, v; for(int i = 1; i < n; i++) { scanf("%d%d", &u, &v); vec[u].push_back(v); vec[v].push_back(u); } dfs(1); for(int i = 1; i <= n; i++) update(interval[i].l, 1); scanf("%d", &m); char s[2]; int x; while(m--) { scanf("%s%d", s, &x); if(s[0] == 'C') { if(vis[x]) update(interval[x].l, -1), vis[x] = 0; else update(interval[x].l, 1), vis[x] = 1; } else if(s[0] == 'Q') printf("%d\n", Sum(interval[x].r) - Sum(interval[x].l-1)); } } return 0;}
- poj_3321 Apple Tree(dfs序+树状数组)
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- POJ 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
- POJ 3321 Apple Tree(dfs序+树状数组)
- 【poj 3321】 Apple Tree 树状数组+dfs序
- POJ 3321Apple Tree(dfs序 树状数组)
- poj 3321 Apple Tree(dfs序+树状数组求和模型)
- 【poj】 3321 Apple Tree dfs序+树状数组
- 【树状数组--dfs序转化】poj3321 Apple Tree
- poj 3321 Apple Tree 树状数组 dfs序
- POJ 3321 Apple Tree dfs序 树状数组
- eclipse.ini配置详情
- React(二)
- 理解互斥量和信号量
- 浅谈浏览器http的缓存机制
- js表格隔行变色
- poj_3321 Apple Tree(dfs序+树状数组)
- 80老翁谈人生(351):北大违纪派存在的客观基础
- 图片加载三方库ImageLoader,Fresco,Picasso,Glide
- android studio快捷键
- Hibernate中的fetch和lazy介绍
- 关于DataBinding生成的类找不到的问题
- 小“Siri” 开发
- 使用Handler延迟简单实现欢迎页面的跳转
- 常用Debug模式下的打印DLog