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. Maven学习总结(24)——Maven版本管理详解

    Maven的版本分为快照和稳定版本,快照版本使用在开发的过程中,方便于团队内部交流学习.而所说的稳定版本,理想状态下是项目到了某个比较稳定的状态,这个稳定包含了源代码和构建都要稳定. 一.如何衡量项目 ...

  2. java ee5的新特性

    1.标注 一种元数据,作用分为三类:编写文档@Document.代码分析@Deparecated(过时的)和编译检查@override(重写) 2.EJB3 EJB2的升级版,商业化的java bea ...

  3. HTTP Status 404 - /servlet/Item/AddItemServlet

    我想学习编程的人对404和500都是非常敏感非常熟悉的.在做DRP系统的时候多次遇到这两个错误,今天让我遇到他并且让我铭记他,那就是一个"/": 这是说jsp出问题了,并且找不到, ...

  4. dotnet core test with NUnit

    https://github.com/nunit/dotnet-test-nunit if you are using Visual Studio. Your project.json in your ...

  5. threejs 入门教程1

    最近在看threejs开发指南,总结一下制作最基础的3d场景的8步: 1. 设置场景大小 2. 创建WebGl渲染器 3. 指定根节点元素 4. 初始化场景 5. 添加相机到场景 6. 创建物体到场景 ...

  6. H.264标准(一)mp4封装格式详解

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  7. 05:Cave Cows 1 洞穴里的牛之一

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 很少人知道其实奶牛非常喜欢到洞穴里面去探险.     洞窟里有N(1≤N≤100)个洞室,由 ...

  8. Ini配置文件操作

    package cn.com.szhtkj.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...

  9. 前端那些事之----jQuery

    1.jquery是什么     一个js的框架,可以方便的使用js 2 什么是jQuery对象     是由jQuery封装后的DOM对象     注意:与DOM对象的方法不同,不可以混用,但是可以相 ...

  10. WebKit.NET-0.5简单应用(2)——音量解决方案

    查找WebKit.NET相关文档,没有找到音量控制解决方法.换思路进行解决,尝试用Win32 API进行解决 [DllImport("winmm.dll")] public sta ...