POJ3321Apple Tree[树转序列 BIT]
| 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
"C 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
"Q 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
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
Source
//
// 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]的更多相关文章
- Tree( 树) 组件[4]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一.方法列表 //部分方法onClick : funct ...
- Tree( 树) 组件[3]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 事件列表很多事件的回调函数都包含'node'参数, ...
- Tree( 树) 组件[2]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件.一. 异步加载如果想从数据库里获取导航内容, 那么就必须 ...
- Tree( 树) 组件[1]
本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件. 一. 加载方式//class 加载方式<ul c ...
- JQuery Easy Ui (Tree树)详解(转)
第一讲:JQuery Easy Ui到底是什么呢? 首先咱们知道JQuery是对Java Script的封装,是一个js库,主要提供的功能是选择器,属性修改和事件绑定等等.. JQuery ui是在j ...
- hdu5044 Tree 树链拆分,点细分,刚,非递归版本
hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...
- 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格
EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...
- 第二百二十六节,jQuery EasyUI,Tree(树)组件
jQuery EasyUI,Tree(树)组件 本节课重点了解 EasyUI 中 Tree(树)组件的使用方法,这个组件依赖于 Draggable(拖 动)和 Droppable(放置)组件. 一.加 ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
随机推荐
- 使用Unity3D的50个技巧
使用Unity3D的50个技巧 刚开始学习Unity3D时间不长,在看各种资料.除了官方的手册以外,其他人的经验也是非常有益的.偶尔看到老外这篇文章,觉得还不错,于是翻译过来和大家共享.原文地址:ht ...
- SharePoint回环检查(Loopback Check)相关问题
Loopback Check(回环检查)本来不是一个SharePoint问题,是Windows Server为了增强自身安全性在Server 2003 SP1后引入的一个功能, 在近几个月中导致了一系 ...
- 连接QuickBooks Online实现于IOS App数据同步功能的个人记录
公司项目需要用WebService与QBO实现后台数据同步,由于国内没有做过类似第三方产品接口的资料,前前后后找了N久,终于实现功能,现把实现功能步骤贴上来分享: QBO开发者地址(主要用于创建QBO ...
- ubuntu处理中文时设置locale
ubuntu下涉及中文处理的程序时,需要正确设置,否则会出现问号或者乱码的情况 具体的原因: http://wiki.ubuntu.org.cn/Locale locale的解释 http://wik ...
- Android ListView添加多种类型的ItemView
一般复杂的ListView都会重写BaseAdapter,通过重用convertView来减少inflate,通过setTag()和ViewHolder改变ItemView的内容. 重写BaseAda ...
- Presenting view controllers on detached view controllers is discouraged <CallViewController: 0x14676e240>.
今天在优化app时,发现程序出现这种警告:“ Presenting view controllers on detached view controllers is discouraged <C ...
- iOS 中内存分配与分区
关于RAM ROM RAM与ROM就是具体的存储空间,统称为存储器 RAM(random access memory):运行内存,CPU可以直接访问,读写速度非常快,但是不能掉电存储.它又分为: 动态 ...
- 编译生成IOS开发使用的FFmpeg的过程
前言:本篇随笔纯属是参照<iOS 使用 FFmpeg>的过程,本人自己操作了一遍,但是本人记性不好,觉得这样的过程可以记录在博客中,以后需要可以快速回头翻阅细节.所以特地参考<iOS ...
- Asp.net MVC验证那些事(4)-- 自定义验证特性
在项目的实际使用中,MVC默认提供的Validation Attribute往往不够用,难以应付现实中复杂多变的验证需求.比如, 在注册用户的过程中,往往需要用户勾选”免责声明”,这个checkbox ...
- Asp.net MVC中提交集合对象,实现Model绑定
Asp.net MVC中的Model自动绑定功能,方便了我们对于request中的数据的处理, 从客户端的请求数据,自动地以Action方法参数的形式呈现.有时候我们的Action方法中想要接收数组类 ...