POJ 3321:Apple Tree 树状数组
来源:互联网 发布:linux空间扩容 编辑:程序博客网 时间:2024/06/03 11:20
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
题意是有一个树,树根始终是1。最开始每一个节点上都上有苹果,然后就开始折腾了。
操作为Q时是询问,该分叉上现在一共有多少个苹果。
操作为C时,如果当前该节点上有苹果,摘掉。如果当前该节点没有苹果,长出一个来。
先dfs出每一个节点的起始时间和结束时间,然后根据起始时间和结束时间使用树状数组求和的思想就能得到结果。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std;#define MY_MAX 220000int C[MY_MAX];vector<vector<int>> G(MY_MAX/2);int HasApple[MY_MAX/2];int sta[MY_MAX];int en[MY_MAX];int ncount=0;int n,m;void dfs(int v){sta[v] = ++ncount;for(int i=0;i<G[v].size();i++){dfs(G[v][i]);}en[v] = ++ncount;}int lowbit(int x) { return x&(-x); } void add(int x,int val) { while(x<=en[1]+1) { C[x] = C[x] + val; x=x+lowbit(x); } } int sum(int x) { int res=0; while(x>0) { res+=C[x]; x=x-lowbit(x); } return res; } int main(){//freopen("i.txt","r",stdin);//freopen("o.txt","w",stdout);int i,temp1,temp2;char oper[5];scanf("%d",&n);for(i=1;i<=n-1;i++){scanf("%d%d",&temp1,&temp2);G[temp1].push_back(temp2);HasApple[i]=1;}HasApple[n]=1;dfs(1);scanf("%d",&m);memset(C,0,sizeof(C));for(i=1;i<=en[1]+1;i++){add(i,1);}for(i=1;i<=m;i++){scanf("%s%d",oper,&temp1);if(oper[0]=='C'){if(HasApple[temp1]){HasApple[temp1]=0;add(sta[temp1]+1,-1);add(en[temp1]+1,-1);}else{HasApple[temp1]=1;add(sta[temp1]+1,1);add(en[temp1]+1,1);}}else if(oper[0]=='Q'){printf("%d\n",(sum(en[temp1])-sum(sta[temp1]))/2+HasApple[temp1]);}}//system("pause");return 0;}
- poj 3321 Apple Tree( 树状数组 )
- POJ 3321 apple tree 树状数组
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- poj 3321 apple tree 树状数组 水
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- POJ 3321 Apple Tree (树状数组)
- POJ 3321 Apple Tree (dfs + 树状数组)
- POJ 3321 Apple Tree (DFS + 树状数组)
- Poj 3321 Apple Tree - 树状数组
- [poj 3321]Apple Tree[树状数组]
- POJ 3321 Apple Tree DFS + 树状数组
- POJ 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree(dfs+树状数组)
- poj 3321 Apple Tree 树状数组
- poj-3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree【树状数组】
- epoll是如何监控多个描述符及如何获得通知(1) http://blog.chinaunix.net/uid-23629988-id-3569332.html
- C++编程思想
- DOM解析
- 游戏节目
- 高性能网络I/O框架-netmap源码分析(1) http://blog.chinaunix.net/uid-23629988-id-3594118.html
- POJ 3321:Apple Tree 树状数组
- ios如何进行网络的推送
- 高性能网络I/O框架-netmap源码分析(2) http://blog.chinaunix.net/uid-23629988-id-3608622.html
- java SE复习笔记55
- 关于陀螺仪数据处理(得到姿态)的一些方法(1)
- 高性能网络I/O框架-netmap源码分析(3) http://blog.chinaunix.net/uid-23629988-id-3614187.html
- 高性能网络I/O框架-netmap源码分析(4) http://blog.chinaunix.net/uid-23629988-id-3642305.html
- 高性能网络I/O框架-netmap源码分析(5) http://blog.chinaunix.net/uid-23629988-id-3693204.html
- Servlet的三种实现