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 < ...
随机推荐
- 自动适应iframe右边的高度
在开发项目过程中,用iframe嵌套,会发现一个问题,用iframe嵌套的html结构右边不会自动适应高度. 这时候找到了一个解决方法: <iframe name="my_iframe ...
- 移动端H5-第一课css篇
1.移动端开发视窗口的添加 h5端开发下面这段话是必须配置的 meta name="viewport" content="width=device-width, init ...
- go语言 新手学习笔记 go基础教程
目前这方面的资料相对较少,自己手动整理汇集. 第一章:安装 第一节:下载go语言 第二节:windows 安装 go语言 第三节: 第二章:基本语法 第一节:类型 .
- pip安装教程
首先你得安装了Python,这个网上教程大把. 关于pip的安装教程网上也很多,但是安装过程中遇到了很多问题. 我把安装pip需要的资源都放到云盘上了,直接下载就行,省得去找.(点我下载) 里面有两个 ...
- 浅谈float浮动
float大概是css3以前网页布局里最常用的一个属性了,经常看到一言不合就浮动的代码,就一起来深入挖掘一下这个一半天使一半魔鬼的属性吧. 本文是读张鑫旭大神慕课网float视频后的一些总结及一些拓展 ...
- webservice简单引用
//1.创建网站//2.新建项=>添加web服务//运行texttweb.asmx可以通过访问http://域名/webservice/texttweb.asmx来验证了//3.添加服务引用=& ...
- Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- 【代码笔记】iOS-短信分享
代码: ViewController.h #import <UIKit/UIKit.h> //短信分享 #import <MessageUI/MFMailComposeViewCon ...
- 【读书笔记】iOS-GCD-GCD与perfomSelector系方法比较
一,GCD是导师步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Queue中,CGD就能生成必要的线程并计划执行 ...
- 面试题整理:C#(一)
该系列持续更新,从网上以及身边收集的问题 1.可访问性级别有哪几种 public 访问不受限制.protected 访问仅限于包含类或从包含类派生的类型.internal 访问仅限于当前程序集.pro ...