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条西瓜蔓连接,形成一个有 ...
随机推荐
- numpy ndarray 打印格式化
1.ndarray打印省略问题 np.set_printoptions(threshold=np.inf) 2.ndarray打印换行限制 加上下面这句代码,输出时打印不换行 np.set_print ...
- Java算法练习——两数之和
题目链接 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...
- Navicat Premium 12安装和破解
链接:https://pan.baidu.com/s/1x8AFWlJYGIl3TlbA1vX63g 提取码:9hu0 安装步骤: 1.下载好后点击navicat12018_premium_cs_x ...
- hdu 6581 Vacation【思维】
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=6581 VacationTime Limit: 10000/5000 MS (Java/Others) ...
- sql注入入门--基本命令
本文转载自http://blog.csdn.net/zgyulongfei/article/details/41017493 本文仅献给想学习渗透测试的sqlmap小白,大牛请绕过. > > ...
- docker入门1---docker的简介和安装
Tomxin7 Simple, Interesting | 简单,有趣 什么是Docker? 简介: Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发 ...
- P3370 【模板】字符串哈希 题解
地址:https://www.luogu.org/problem/P3370 求不同字符串的数量 这题用set也可以过,但是hash更高大上嘛. 哈希其实就是将一个字符串映射成一个值,并且要让这些值不 ...
- Linux-socket编程接口介绍
1.建立连接 (1).socket.socket函数类似于open,用来打开一个网络连接,如果打开成功则返回一个网络文件描述符(int类型),之后我们操作这个网络连接都可以通过这个网络文件描述符. ( ...
- MobX 在 hook 中的使用
关于 mobX 在 react 16.8.0 以上的用法 以下例子均取自官网文档 一般用法: import { observer, useLocalStore } from 'mobx-react'; ...
- Android通过包名打开第三方应用
import android.content.ComponentName; import android.content.Context; import android.content.Intent; ...