poj 3321(树状数组)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 24954 | Accepted: 7447 |
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
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
题意:一棵树上有n个结点,每个节点上面都有一个苹果,现在给两个操作:
C x 如果第 x 个节点上存在苹果,则摘掉,如果没有,那么会长一个出来。
Q x 问 x 的子树里面有多少个苹果。
题解:DFS进行节点的重新标记,求出每个结点的"管辖范围",然后每次更新左区间,求和就用sum(R[x]) - sum(L[x]-1)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
using namespace std;
const int N = ;
int L[N],R[N],c[N]; ///[L[i],R[i]] 是第i个点的管辖范围
bool flag[N];
int n,key;
vector <int> edge[N];
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=n;i+=lowbit(i)){
c[i]+=v;
}
}
int getsum(int idx){
int sum = ;
for(int i=idx;i>=;i-=lowbit(i)){
sum+=c[i];
}
return sum;
}
void dfs(int idx){
L[idx] = key;
for(int i=;i<edge[idx].size();i++){
key+=;
dfs(edge[idx][i]);
}
R[idx] = key;
}
int main()
{
while(scanf("%d",&n)!=EOF){
key = ;
memset(c,,sizeof(c));
memset(flag,false,sizeof(flag));
for(int i=;i<=n;i++) edge[i].clear();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(v);
}
dfs();
for(int i=;i<=n;i++){
update(i,);
}
int q;
scanf("%d",&q);
while(q--){
char s[];
int x;
scanf("%s%d",s,&x);
if(s[]=='Q'){
printf("%d\n",getsum(R[x])-getsum(L[x]-));
}else{
if(flag[x]){
update(L[x],);
}else update(L[x],-);
flag[x] = !flag[x];
}
}
}
return ;
}
poj 3321(树状数组)的更多相关文章
- POJ 3321 树状数组(+dfs+重新建树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27092 Accepted: 8033 Descr ...
- POJ 2352Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42898 Accepted: 18664 Descripti ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- poj 3928 树状数组
题目中只n个人,每个人有一个ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判的ID和技能值都在两个选手之间的时候才能进行一场比赛,现在问一共能组织多少场比赛. 由于排完序之后,先插入的一定 ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- poj 2182 树状数组
这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...
- POJ 2352 树状数组
学习自:链接以及百度百科 以及:https://www.bilibili.com/video/av18735440?from=search&seid=363548948825132979 理解 ...
- POJ 2299树状数组求逆序对
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...
随机推荐
- 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季
目录 要实现的功能 思路: 视图层 控制器里: 扩展函数里 要实现的功能 用表单里的提交过来的sort数据,批量修改表里的排序值 界面效果: 思路: 视图层表单提交数据主键=>sort值 控制器 ...
- Android系统中标准Intent的使用
Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...
- 关于json数据中的多反斜杆转译--StringEscapeUtils.unescapeJava(踩过的坑)
一.需求 现有一个字符串str String str = "{\\\"name\\\":\\\"spy\\\",\\\"id\\\\&quo ...
- 如何将emoji表情存放到mysql数据库中
昨晚在爬取猫眼电影评论时在将评论信息插入到数据库中时出现问题,总是在插入一条数据时就会报错: 看着应该时字符编码的问题,比如新建的数据库新建的表,默认字符编码是:Latin1, 这种编码是无法插入中文 ...
- Flow Problem HDU - 3549
Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...
- c++ 操作符优先级
优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的操作符作用域操作符后置自增操作 ...
- 数学算法:poweroj1026-丑数(根据固定倍数得到从小到大的序列)
题目: 1026: 丑数 Time Limit: 1000 MS Memory Limit: 65536 KB Total Submit: 257 Accepted: 112 Page View: 1 ...
- [Bzoj2588]Count on a tree(主席树+LCA)
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- mysql-show processlist之writing to net
mysql提示Writing to net解决 最近发现某一个数据库cpu占用比较过.超过200%了. 首先查看数据库慢日志,设定慢日志5秒,基本上没有产生日,没有超过5秒的语句. show proc ...
- linux系统下单节点hadoop2的配置
Jdk安装: jdk-7u45-linux-x64.gz cp jdk-7u45-linux-x64.gz /usr/java/ cd /usr/java/ tar -zxvf jdk-7u45-li ...