POJ 3321- Apple Tree(标号+BIT)
题意:
给你一棵树,初始各节点有一个苹果,给出两种操作,C x 表示若x节点有苹果拿掉,无苹果就长一个。
Q x查询以x为根的子树中有多少个苹果。
分析:
开始这个题无从下手,祖先由孩子的标号不能确定,就想能不能重新编号
,对与一棵树我们以先根序进行编号这就保证了一个子树在一个连续的区间内,然后就是BIT了。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct edge{
int v,next;
}e[N*];
int used[N],bit[N],l[N],r[N],id,f[N],n,head[N*];
int len;
void add_edge(int u,int v){
e[len].v=v;
e[len].next=head[u];
head[u]=len++;
}
void add(int x,int d){
while(x<=n){
bit[x]+=d;
x+=x&(-x);
}
}
int sum(int x){
int num=;
while(x>){
num+=bit[x];
x-=x&(-x);
}
return num;
}
void dfs(int i){
l[i]=++id;//根节点编号就是子树左边界
used[i]=;
for(int j=head[i];j!=-;j=e[j].next){
if(!used[e[j].v]){
dfs(e[j].v);
}
}
r[i]=id;//最后一个子孙的编号右边界
}
int main()
{
while(~scanf("%d",&n)){
memset(bit,,sizeof(bit));
memset(f,,sizeof(f)); memset(used,,sizeof(used));
memset(head,-,sizeof(head));
int u,v;
len=;
for(int i=;i<n-;++i){
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
id=;
dfs();
for(int i=;i<=n;++i)
add(i,);
int m,fork;
char op[];
scanf("%d",&m);
while(m--){
scanf("%s%d",op,&fork);
if(op[]=='C'){
if(f[fork]){
add(l[fork],);
f[fork]=;
}
else{
add(l[fork],-);
f[fork]=;
}
}
else if(op[]=='Q'){
printf("%d\n",sum(r[fork])-sum(l[fork]-));
}
}
}
return ;
}
POJ 3321- Apple Tree(标号+BIT)的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- #5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 7503 Descr ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree dfs+二叉索引树
题目:http://poj.org/problem?id=3321 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable
http://www.itblah.com/django-error-manyrelatedmanager-object-iterable/ Django: Error: ‘ManyRelatedMa ...
- Java 正则表达式的总结和一些小例子
字符串处理是许多程序中非常重要的一部分,它们可以用于文本显示,数据表示,查找键和很多目的.在Unix下,用户可以使用正则表达式的强健功能实现这些 目的,从Java1.4起,Java核心API就引入了j ...
- Android 解决ListView中每一项与button冲突
在listView的item里面如果有button,ImageButton等控件,会使得ListView不会被点击,解决方法是: ①在Button上面添加属性 android:focusable=&q ...
- IE插件DebugBar如何安装及使用
DebugBar插件是一款功能很强大的IE插件,用户可以从各个不同的角度剖析Web页面内部,包括页面 详细代码.CSS样式表.所有链接.所有图片代码.脚本信息等等,不管你是编程大虾还是IT新人都和适合 ...
- Sina App Engine(SAE)入门教程(7)- Storage使用
参考阅读 sae storage api 文档 Storage 说明文档 Storage 大文件上传说明 storage是什么? 因为sae禁用了代码环境的本地读写,但是在网站运行的过程中,必定会出现 ...
- C语言命名规则
一.程序风格: 1.严格采用阶梯层次组织程序代码: 各层次缩进的分格采用VC的缺省风格,即每层次缩进为4格,括号位于下一行. 要求相匹配的大括号在同一列,对 ...
- Splunk作为日志分析平台与Ossec进行联动
背景: Ossec安装后用了一段时间的analogi作为ossec的报警信息显示平台,但是查看报警分类信息. 以及相关图标展示等方面总有那么一点点的差强人意,难以分析.因此使用逼格高一点的splunk ...
- Unified Emoji表情for Android
这个是我做Android以来碰到的最烦的东西,该死的emoji表情,恨之入骨..无奈这个问题分配给我了.我也只能硬着头皮做. 0.吐个槽先 首先,你要明白什么是emoji表情,不知道的google,不 ...
- openCv 图像顺时针、逆时针旋转
通过下面这个函数调用 Rotate90(workImg,270); //顺时针旋转 Rotate90(workImg,90); //逆时针旋转 实现,其实用该函数旋转任意度数对正方形图都ok,只是长方 ...
- UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...