pku-3321 Apple Tree(dfs序+树状数组)
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
For every inquiry, output the correspond answer per line.
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
题意:
给一棵树,树上结点权值一开始都为1,每次操作将1与0相互转换,求某个结点的子树(包括自己)的权值和
解题思路:
将结点转换为dfs序,并用L和R数组维护该结点的子树以转换为线性结构,用树状数组或者线段树维护区间就行了,我写的是树状数组的
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 1e5 + 7;
const ll MAXM = 1e5 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
struct node
{
int from, to;
int next;
} E[MAXN << 1];
int n, q;
int cnt = -1;
int head[MAXN];
void add_edge(int u, int v)
{
E[++cnt].from = u;
E[cnt].to = v;
E[cnt].next = head[u];
head[u] = cnt;
}
int a[MAXN], c[MAXN];
int cnt1 = 0;
int L[MAXN];
int R[MAXN];
int lowbit(int x)
{
return x & (-x);
}
int gesum(int x)
{
int ans = 0;
for (int i = x; i > 0; i -= lowbit(i))
ans += c[i];
return ans;
}
void add(int x, int y)
{
for (int i = x; i <= n; i += lowbit(i))
c[i] += y;
}
void dfs(int now, int fa)
{
L[now] = ++cnt1;
for (int i = head[now]; i != -1; i = E[i].next)
{
int to = E[i].to;
if (to != fa)
dfs(to, now);
}
R[now] = cnt1;
}
void init()
{
memset(head, -1, sizeof(head));
cnt = -1;
cnt1 = 0;
for (int i = 1; i <= n; i++)
a[i] = 1, add(i, 1);
}
int main()
{
scanf("%d", &n);
init();
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs(1, -1);
scanf("%d", &q);
for (int i = 0; i < q; i++)
{
int x;
char op[10];
scanf(" %s%d", op, &x);
if (op[0] == 'Q')
printf("%d\n", gesum(R[x]) - gesum(L[x] - 1));
else
{
if (a[L[x]])
{
a[L[x]] ^= 1;
add(L[x], -1);
}
else
{
a[L[x]] ^= 1;
add(L[x], 1);
}
}
}
return 0;
}
pku-3321 Apple Tree(dfs序+树状数组)的更多相关文章
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
随机推荐
- Xcode崩溃日志分析工具symbolicatecrash用法
1.什么是symbolicatecrash? symbolicatecrash是Xcode自带的一个分析工具,可以通过机器上的崩溃日志和应用的.dSYM文件定位发生崩溃的位置,把crash日志中的一堆 ...
- DEVOPS技术实践_14:使用docker部署jenkins
一 基础环境准备 [root@node6 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node6 ~]# yum -y ...
- $loj\ 6045$ [雅礼集训 $2017\ Day8$] 价 网络流
正解:网络流 解题报告: 传送门$QwQ$ 这题还,挺有趣的我$jio$得. 考虑依然先是照着最小割的模子建图呗,然后从意义上来分析,割一条边就相当于不吃一种减肥药/买一种药材.由已知得,买的药材数量 ...
- 惊呆了!Java程序员最常犯的错竟然是这10个
和绝大多数的程序员一样,我也非常的宅.周末最奢侈的享受就是逛一逛技术型网站,比如说 programcreek,这个小网站上有一些非常有意思的主题.比如说:Java 程序员最常犯的错竟然是这 10 个, ...
- 面试中经常问到的Redis七种数据类型,你都真正了解吗?
前言 Redis不是一个简单的键值对存储,它实际上是一个支持各种类型数据结构的存储.在传统的键值存储中,是将字符串键关联到字符串值,但是在Redis中,这些值不仅限于简单的字符串,还可以支持更复杂的数 ...
- 突破CRUD | 简单优雅的代码生成工具诞生记(万字长文慎入)
0.学习本文你或许可以收获 1.一个需求从产生.分析到解决的全过程思考2.简单的面向对象分析实践3.UML类图实践4.设计模式的实践应用5.最后收获一款还算不错的代码生成工具实现思路和源代码 本文将从 ...
- SpringBoot介绍与使用
SpringBoot介绍与使用 1.什么是SpringBoot SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品: 我们可 ...
- Pandas 数据分析,高中体测练习
分析体测成绩 需求: 体侧成绩转变成分数 开卷考试 excel完成可以 pandas读取excel代码中 完成 一个手输入 进一步,画图,分布,体重正常,肥胖,偏瘦比例,绘制饼图 男生跑步1000成绩 ...
- php hash比较缺陷
PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值都解释为0,所以如果两个不同的密码经过哈希以后,其哈希值都是以”0E”开头的,那么PHP将会认为他 ...
- 梳理js数组去重中代码比较简洁的方案
一.es6 Set去重 function removal(arr) { return Array.from(new Set(arr)) } let arr=[1,2,1,3,4,5,5] remova ...