Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26995   Accepted: 8007

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong
 

题意:加减一个节点的苹果(异或) 查询以x为根的子树的苹果数
求dfs序(先序遍历),每个子树就是序列上的一段,单点修改区间查询
//
// main.cpp
// poj3211
//
// Created by Candy on 9/19/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+,M=1e5+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,x;
char c[];
struct edge{
int v,ne;
}e[N<<];
int cnt=,h[N];
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int a[N],p=,st[N],ed[N];
void dfs(int u,int fa){
p++;st[u]=p;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dfs(v,u);
}
ed[u]=p;
}
int bit[N];
inline int lowbit(int x){
return x&-x;
}
void buildBit(){
for(int i=;i<=n;i++){
a[i]=;
bit[i]+=a[i];
if(i+lowbit(i)<=n) bit[i+lowbit(i)]+=bit[i];
}
}
inline void add(int x,int d){
while(x<=n){
bit[x]+=d;x+=lowbit(x);
}
}
inline int sum(int x){
int ans=;
while(x>){
ans+=bit[x];x-=lowbit(x);
}
return ans;
}
inline int query(int l,int r){
return sum(r)-sum(l-);
}
int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n-;i++) ins(read(),read());
dfs(,);
buildBit();
m=read();
//for(int i=1;i<=n;i++) printf("%d %d %d %d\n",st[i],ed[i],a[i],bit[i]);
for(int i=;i<=m;i++){
scanf("%s",c);
if(c[]=='C'){
x=read();int b=st[x];
if(a[b]) add(b,-);
else add(b,);
a[b]^=;
}
if(c[]=='Q'){
x=read();
printf("%d\n",query(st[x],ed[x]));
}
}
return ;
}
 

POJ3321Apple Tree[树转序列 BIT]的更多相关文章

  1. Tree( 树) 组件[4]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...

  2. Tree( 树) 组件[3]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 事件列表很多事件的回调函数都包含'node'参数, ...

  3. Tree( 树) 组件[2]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 异步加载如果想从数据库里获取导航内容, 那么就必须 ...

  4. Tree( 树) 组件[1]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件. 一. 加载方式//class 加载方式<ul c ...

  5. JQuery Easy Ui (Tree树)详解(转)

    第一讲:JQuery Easy Ui到底是什么呢? 首先咱们知道JQuery是对Java Script的封装,是一个js库,主要提供的功能是选择器,属性修改和事件绑定等等.. JQuery ui是在j ...

  6. hdu5044 Tree 树链拆分,点细分,刚,非递归版本

    hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...

  7. 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格

    EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...

  8. 第二百二十六节,jQuery EasyUI,Tree(树)组件

    jQuery EasyUI,Tree(树)组件 本节课重点了解 EasyUI 中 Tree(树)组件的使用方法,这个组件依赖于 Draggable(拖 动)和 Droppable(放置)组件. 一.加 ...

  9. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

随机推荐

  1. mongodb 查询的用法

    想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csharp-dri ...

  2. js的一些坑,持续增加,学习js应该注意的问题

    大家来补充 1.变量类型模糊,容易出现问题; var a='1',b=1; a==b; //true a===b; //false 2.全局变量与函数内部变量同名时,在函数内部声明变量,声明位置虽然在 ...

  3. 后台运行进程(background job)

    在一些日常业务中,总有一些长时间处理的任务,系统运行这些任务需要一晚甚至一个周末. 这就需要后台运行单元(background work process)来完成,而且其是不会发生超时(time out ...

  4. LeakCanary中英文文档+使用例子

    Android 开源界最伟(jian)大(zhi)高(kai)效(gua)的公司 Square 又向业界投下一颗重磅炸弹.推出了一个叫 LeakCanary 的玩意儿,可以通过简单粗暴的方式来让开发者 ...

  5. 利用Dreamweaver配置PHP服务器的站点

    配置的步骤: 1.打开Dreamweaver的站点------->新建站点-------->点击保存 2.点击服务器------>保存 3.配置完成之后就可以看到在Dreamweav ...

  6. Android - 模块添加与编译

    Android5.1 Ubuntu14.04 Android系统编译依靠.mk文件 添加google服务 我们自己的ROM里没有google服务 完整的google包里包含google框架和各种服务, ...

  7. android 最简单的自定义圆点view

    首先创建一个选择器,用来判断圆点状态,可以根本自己的需求改 <selector xmlns:android="http://schemas.android.com/apk/res/an ...

  8. 怎么查看Mac电脑的开机记录?

    可以使用last命令查看Mac电脑来看开机记录,同时也能查看关机记录. 首先打开mac的命令终端: 命令行终端敲入命令:last | grep reboot (查看开机时间记录) 命令行终端敲入命令: ...

  9. 【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  10. 【代码笔记】iOS-获得现在的时间

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...