E - Apple Tree(树状数组+DFS序)
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
题解:题目有两种操作:1.更改某节点值(1~0变换)。2.询问某节点和其所有子节点所有权值之和。
我们可以从1开始进行DFS重新编号,对于每一个原来的节点,记录下这个节点的编号Start[i]和子节点中最大编号End[i],这样我们查询一个节点和其所有字节点的权值之和时,便可运用树状数组的求法:
sum[End[i]]-sum(Start[i]-1).
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=;
int n,c[maxn];
void update(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=;
for(int i=x;i>=;i-=lowbit(i))
ans+=c[i];
return ans;
}
struct node
{
int v,next;
}e[maxn*];
int cnt=;
int head[maxn];
void add(int u,int v){
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
int tot=;
int Start[maxn],End[maxn];
int viss[maxn];
void DFS(int x)
{
Start[x]=++tot;
for(int i=head[x];i!=-;i=e[i].next){
DFS(e[i].v);
}
End[x]=tot;
}
int main()
{
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=;i<=n-;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
DFS();
memset(viss,,sizeof(viss));
for(int i=;i<=n;i++)c[i]=lowbit(i);
int q;
scanf("%d",&q);
while(q--){
char s[];
int x;
scanf("%s%d",s,&x);
if(s[]=='Q'){
printf("%d\n",sum(End[x])-sum(Start[x]-));
}
else{
if(!viss[x]){
viss[x]=;
update(Start[x],);
}
else{
viss[x]=;
update(Start[x],-);
}
}
}
return ;
}
E - Apple Tree(树状数组+DFS序)的更多相关文章
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- POJ 3321 Apple Tree 树状数组+DFS
题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...
- 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序
[题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...
- HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)
Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 【BZOJ-1103】大都市meg 树状数组 + DFS序
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2009 Solved: 1056[Submit][Sta ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- [luogu P3787][新创无际夏日公开赛] 冰精冻西瓜 [树状数组][dfs序]
题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...
随机推荐
- 寒假day21
标签模块报了一些错误,暂时没有找出原因.刷了一些面试题
- missing KW_END at ')' near '<EOF>'
case when 没写 end
- python类(3)感悟
1.关于类属性attribute和实例(对象)特性property思考 为什么特性会出现,类属性不能完全替代它吗? 属性: python在为属性赋值时,只会搜索对象本身的__dict__,如果找不到对 ...
- BZOJ [Cqoi2017] 小Q的棋盘
题解:枚举最后在哪里停止,然后剩下的步数/2 也就是找最大深度 枚举终止位置算是一种思路吧 #include<iostream> #include<cstdio> #inclu ...
- Java语言概述-JavaSE
代码虐我千百遍,我视代码如初恋 初级学习思想: 先了解Java 下载中英文文档对照学习 多看,多学 多敲,狂练 多标注注释 总结—创造 https://baike.baidu.com/(Java百度百 ...
- rsync搭建
服务器: 查看是否安装:rpm -qa rsync 未安装则:yum install -y rsync 添加rsync用户 useradd -s /sbin/nologin -M rsync 编辑/e ...
- springboot整合redis简单示例
一.项目架构 二.项目内容 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 瑞芯微RK3399六核-迅为3399开发板介绍
迅为3399开发板基于瑞芯微的RK3399处理器设计,Rockchip RK3399是瑞芯微推出的一款低功耗.高性能的应用处理器芯片,该芯片基于Big.Little架构,即具有独立的NEON协同处理器 ...
- Cannot read property 'XXXX' of null/undifined
这个问题可能的原因有很多 1.如果你的js直接写在自执行函数或者head标签内的script里面,那么可以检查一下你的代码有没有用到页面里的节点,因为这样写的代码在页面加载完成之前就会开始执行,如果有 ...
- 洛谷 P3371 【模板】单源最短路径(弱化版)(dijkstra邻接链表)
题目传送门 解题思路: 传送门 AC代码: #include<iostream> #include<cstdio> #include<cstring> using ...