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 ...
随机推荐
- [译]The Python Tutorial#12. Virtual Environments and Packages
[译]The Python Tutorial#Virtual Environments and Packages 12.1 Introduction Python应用经常使用不属于标准库的包和模块.应 ...
- vncserver 启动停止方式
vnc启停方式:vncserver :1 ; vncserver -kill :1
- dialog BLE SDK 学习(1)
dialog DA14580 SDK版本:5.0.4. 本文介绍了SDK的运行流程,剖析arch_main.c的工作过程. dialog的BLE协议栈,本来是想学习一下,看看是否能够移植到其他平台上, ...
- IIS6.0,Apache低版本,PHP CGI 解析漏洞
IIS6.0解析漏洞 在IIS6.0下存在这样的文件"名字.asp;名字.jpg" 代表了jpg文件可以以asp脚本类型的文件执行. 根据这个解析漏洞我们可以上传这种名字类型的图片 ...
- Python-S9-Day122-Python爬虫
11 初识爬虫 11 初识爬虫 12 今日作业 11.1 初识黄页: 11.2 互联网就是一张大的蜘蛛网: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种 ...
- sqlserver2008链接服务器中执行存储过程配置过程
参考:http://www.cnblogs.com/ycsfwhh/archive/2010/12/15/1906507.html 1.双方启动MSDTC服务MSDTC(分布式交易协调器),协调跨多个 ...
- [python][django学习篇][12]继续设计博客首页,点击博客标题能显示文章的详情
回顾一下开发流程:配置url, 编写视图函数,编写对应模板 配置URL 首页视图匹配的 URL 去掉域名后,是一个空的字符串.每篇文章的详情有着不同的 URL,因此可以设计文章详情页面URl:< ...
- Python-伪私有属性
原文:http://blog.itpub.net/26250550/viewspace-1411768/ 通常在 Python 中,我们都被告知可以使用双下划线开头的方法名定义方法来达到私有函数的目标 ...
- PAT1026
要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick,即“时 ...
- ORA-12012: 自动执行作业 "SYS"."ORA$AT_OS_OPT_SY_21" 出错
oracle 12.2.0.1版本报错: Errors in file /u01/app/oracle/diag/rdbms/easdb/easdb2/trace/easdb2_j000_27520. ...