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. 分库代价高的情况下,如何优化ES解决亿级数据量检索

    数据平台已迭代三个版本,从一开始遇到很多常见的难题,到现在终于有片段时间整理一些已完善的文档,在此分享以供所需朋友的实现参考,但愿能帮助大家少走些弯路,在此篇幅中偏重于ElasticSearch的优化 ...

  2. ASP.NET-viewBag Viewdata Tempdata

    ASP.NET MVC提供3种选择ViewData,ViewBag,TempData来从controller到View及后续请求传输数据. ViewData和ViewBag很相似,而TempData有 ...

  3. BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...

  4. Ubuntu安装keepalived

    Ubuntu安装keepalived 一.Keepalived是什么鬼东西: keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. 二.Ke ...

  5. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

  6. JAVA性能优化的五种方式

    一,JAVA性能优化之设计优化 设计优化处于性能优化手段的上层.它往往须要在软件开发之前进行.在软件开发之前,系统架构师应该就评估系统可能存在的各种潜在问题和技术难点,并给出合理的设计方案,因为软件设 ...

  7. php利用href进行页面传值的正确姿势

    首先在a.php中 <?php $a = "world"; echo "<a href='b.php?m=$a'>删除</a>"; ...

  8. GitHub客户端和Shell的基本操作和理解

    GitHub客户端和Shell指令的简单实用 客户端操作, web端操作, shell指令操作. 掌握了这三种操作,基本上就可以很好的运用gitHub了. 创建项目, 可以通过web端进行创建. 可以 ...

  9. CxImage学习

    官方下载地址是:http://www.xdp.it/cximage/ 打开工程后可以看到下例这些工程: - CxImage - CxImageCrtDll - CxImageMfcDll - dome ...

  10. 深入理解 JavaScript 异步——转载

    本文章转载于深入理解 JavaScript 异步 前言 2014年秋季写完了<深入理解javascript原型和闭包系列>,已经帮助过很多人走出了 js 原型.作用域.闭包的困惑,至今仍能 ...