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. 【codeforces 234F】Fence

    [题目链接]:http://codeforces.com/problemset/problem/234/F [题意] 你有n块板要凃油漆; 然后每块板有高度h[i];(宽度都为1) 然后每块板只能凃同 ...

  2. 洛谷 P2393 yyy loves Maths II

    P2393 yyy loves Maths II 题目背景 上次蒟蒻redbag可把yyy气坏了,yyy说他只是小学生,蒟蒻redbag这次不坑他了. 题目描述 redbag给了yyy很多个数,要yy ...

  3. 确保 Xcode 每次 Build 时都自己主动更新资源

    參考:p=22" target="_blank">http://quick.cocoachina.com/?p=22 刚建立的quickproject.每次修改lu ...

  4. D3js-绘制地图时出现过小, 设置scale还是无效 的解决方法

    使用d3绘制某个地市的地图时,把scale成非常大但是还是无法达到想要的效果. //---------------------------------------------------------- ...

  5. inheritance in kentico

    Visual inheritance http://devnet.kentico.com/docs/7_0/devguide/index.html?visual_inheritance.htm The ...

  6. 9.variant move function change_cast

    包含的头文件 #include <iostream> #include <string> #include <boost/array.hpp> //异构的容器 #i ...

  7. 移动端video播放时不弹出页面层

    移动端视频在播放时会主动弹出页面,有的浏览器不会.对那些会的浏览器进行处理: 直接加上下面三个属性即可,兼容方面就不说了,微信上是很ok的. <video x5-playsinline=&quo ...

  8. servlet中怎么注入service

    在工作中使用到spring的mvc框架,分为controller/service/dao三个层次.偶尔会用到servlet替换掉controller,这就遇到如何在servlet中使用注入到sprin ...

  9. APP信息获取接口

    https://itunes.apple.com/lookup?id=APPID&callback=2 http://myapp.com/cgi-bin/mapp/mapp_info?type ...

  10. 洛谷P1339 [USACO09OCT]热浪Heat Wave(最短路)

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...