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序+树状数组)的更多相关文章

  1. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  2. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  3. 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 ...

  4. 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 ...

  5. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  6. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  7. 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 ...

  8. 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 ...

  9. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  10. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

随机推荐

  1. [译文] C# 已成旧闻, 向前, 抵达 C# 9!

    C# 8 is old news. Onward, to C# 9! (C# 已成旧闻, 向前, 抵达 C# 9!) Did you know that planning is already und ...

  2. node多进程的创建与守护

    node是单线程运行,我们的node项目如何利用多核CPU的资源,同时提高node服务的稳定性呢? 1. node的单线程 进程是一个具有一定独立功能的程序在一个数据集上的一次动态执行的过程,是操作系 ...

  3. [工具] Git版本管理(四)(贡献开源代码、git配置、git免密、gitignore)

    一.开源项目贡献代码 1.fork项目代码 例如,我们想向tornado框架贡献代码,首先搜索tornado. 然后,将tornado的代码fork到我们的仓库中. 2.clone到本地进行开发 克隆 ...

  4. 20191121-9 Scrum立会报告+燃尽图 05

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/10069 一: 组名:组长组 组长:杨天宇 组员:魏新  罗杨美慧  王歆 ...

  5. 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)

    [题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...

  6. Java程序运行原理

    概念介绍: ![file](https://img2018.cnblogs.com/blog/1454321/202001/1454321-20200104145655999-149562495.jp ...

  7. keuectl命令

    Kubernetes命令行 kubectl用于运行Kubernetes集群命令的管理工具 kubectl命令行语法 kubectl [command] [TYPE] [NAME] [flags] co ...

  8. Linux网络管理之多网卡绑定

    一.bonding介绍 在企业Linux服务器管理里中,服务器的可靠性.可用性以及I/O速度都非常重要,保持服务器的高可用和安全性是生产环境的重要指标,其中最重要的一点是服务器网络连接的高可用性.通常 ...

  9. 输入n个学生,并且输入成绩,判断是否偏科

    H学校的领导主任决定分析一下今年所有N名学生的考试成绩,从中找出偏科的学生,考试成绩包含语文,数学,英语三门课程的分数,已知偏科的定义是:某一门课程的分数大于等于90,并且另外两门的分数小于等于70. ...

  10. 小白学Java:老师!泛型我懂了!

    目录 小白学Java:老师!泛型我懂了! 泛型概述 定义泛型 泛型类的定义 泛型方法的定义 类型变量的限定 原生类型与向后兼容 通配泛型 非受限通配 受限通配 下限通配 泛型的擦除和限制 类型擦除 类 ...