id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - 3321

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

"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

"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
/*
Author: 2486
Memory: 10004 KB Time: 766 MS
Language: G++ Result: Accepted
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define root 1, 1, N
const int MAXN = 2e5 + 5;
int sum[MAXN << 2], LU[MAXN], RU[MAXN], N, M, A, B, tot, K;
char op[10]; /***********加边模板***************/
int Head[MAXN], Next[MAXN], rear;
struct edge {
int u,v;
} es[MAXN]; void Edge_Init() {
rear = 0;
memset(Head, -1, sizeof(Head));
} void Edge_Add(int u,int v) {
es[rear].u = u;
es[rear].v = v;
Next[rear] = Head[u];
Head[u] = rear ++;
} void DFS(int to, int from) {
LU[to] = ++ tot;//用来标记属于它的子树的序列
for(int i = Head[to] ; ~ i; i = Next[i]) {
int v = es[i].v;
if(v == from) continue;
DFS(v, to);
}
RU[to] = tot;
}
/**********************************/ void pushup(int rt) {
sum[rt] = sum[rt << 1] + sum[rt << 1|1];
} void build(int rt, int l, int r) {
if(l == r) {
sum[rt] = 1;
return ;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int rt,int l, int r) {
if(l == r) {
sum[rt] ^= 1;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p, lson);
else update(p, rson);
pushup(rt);
} int query(int L, int R,int rt, int l, int r) {
if(L <= l && r <= R) {
return sum[rt];
}
int mid = (l + r) >> 1;
int res = 0;
if(L <= mid) res += query(L, R, lson);
if(R > mid) res += query(L, R, rson);
return res;
} int main() {
//freopen("D://imput.txt","r",stdin);
while(~ scanf("%d", &N)) {
tot = 0;
build(root);
Edge_Init();
for(int i = 1; i < N ; i ++) {
scanf("%d%d", &A, &B);
Edge_Add(A, B);
Edge_Add(B, A);
}
DFS(1, -1);
scanf("%d", &M);
while(M --) {
scanf("%s %d", op, &K);
if(op[0] == 'C') {
update(LU[K],root);
} else {
printf("%d\n", query(LU[K],RU[K],root));
}
}
}
return 0;
}

POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)的更多相关文章

  1. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  4. poj 3321 Apple Tree(一维树状数组)

    题目:http://poj.org/problem?id=3321 题意: 苹果树上n个分叉,Q是询问,C是改变状态.... 开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号 白 ...

  5. POJ 3321 Apple Tree(树状数组)

    点我看题目  题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...

  6. POJ 3321 Apple Tree (DFS + 树状数组)

    题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...

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

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

  8. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  9. #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Descr ...

随机推荐

  1. python--(常用模块-1)

    python--(常用模块-1) 一.模块的简单认识: 什么是模块,模块就是我们把装有特有功能的代码进行归类的结果,从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块 ...

  2. promise的原理

    promise的原理 一旦状态改变,就不会再变,任何时候都可以得到这个结果.Promise 对象的状态改变,只有两种可能:从 pending 变为 fulfilled 和从 pending 变为 re ...

  3. 赵雅智_使用SQLiteDatabase操作SQLite数据库及事务

    知识点具体解释:http://blog.csdn.net/zhaoyazhi2129/article/details/9025995 详细代码: MainActivity.java package c ...

  4. java.lang.ClassNotFoundException: org.jaxen.JaxenException

    java.lang.ClassNotFoundException: org.jaxen.JaxenException java.lang.ClassNotFoundException: org.jax ...

  5. 根据数据库表结构生成java类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

  6. MyEclipse中安装Testng插件

    下载testng.eclipse插件 Testng相应jar包,这里使用的是5.12的版本号 直接找到myeclipse的安装文件夹,将org.testng.eclipse_5.12.0.6.jar ...

  7. nyist oj 138 找球号(二)(hash 表+位运算)

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中.每一个球上都有一个整数编号i(0<=i< ...

  8. 黑马day15 文件上传&amp;apche的工具包

    1.肯定要导入apche的jar包 2.要使用的类的介绍.. 2.1DiskFileItemFactory  public DiskFileItemFactory(int sizeThreshold, ...

  9. doT.js实现混合布局,判断,数组,函数使用,取模,数组嵌套

    doT.js实现混合布局 数据结构 { "status": "1", "msg": "获取成功", "info ...

  10. hpuoj--1122-- HH的随机数(数据去重)

    1122: HH的随机数 时间限制: 1 Sec  内存限制: 128 MB 提交: 476  解决: 75 [提交][状态][讨论版] 题目描述 HH想在学校中请一些同学一起做一项问卷调查,为了实验 ...