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 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- 由CHAR(2)引发的BUG
我们在设计数据库标志位字段时,为考虑其扩展性,一般会设置为CHAR(2),例如 FLAG CHAR(2),这样我们就需要注意了,如果你给字段 FLAG赋值为‘0’,它在数据库中的真实情况是‘0+空格’ ...
- 解决Cygwin中文乱码
如下图所示,在执行ping或者cmd.exe命令时,Cygwin出现中文乱码: 解决方案: 在Cygwin终端上右键-->Options…-->Text-->修改Locale 为 z ...
- AutoEventWireup解释
这一事件聚合了当前页是否自动关联某些特殊事件. 首先,从浏览器页面出发的事件不能立刻在本地得到处理,而是POST至服务器上,因此,asp.net建立了委托(代理)机制.在建立一个事件的同事,建立相应的 ...
- LR_问题_无法打开IE浏览器、监视服务器资源
无法打开IE浏览器 使用web(http)协议录制时,无法打开IE浏览器,且生成的日志信息为 ****** Start Log Message ****** Web Recorder version ...
- Python list去重及找出,统计重复项
http://bbs.chinaunix.net/thread-1680208-1-1.html 如何找出 python list 中有重复的项 http://www.cnblogs.com/feis ...
- java io异步
1.一般来说,可以通过多线程的方式来实现异步 2.同步和异步着重点在于多个任务的执行过程中,一个任务的执行是否会导致整个流程的暂时等待: 3.而阻塞和非阻塞着重点在于发出一个请求操作时,如果进行操作的 ...
- Swift入门(十一)——类型转换与is、as操作
三种操作:is.as?和as! Swift是强类型语言,但也允许开发者通过is.as?和as!这三种操作来对类型进行判断和强制转换.其中is用作类型判断,而as?和as!则分别是类型转换的可选形式和强 ...
- Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍
1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制
- go的优缺点
1.1 不允许左花括号另起一行1.2 编译器莫名其妙地给行尾加上分号1.3 极度强调编译速度,不惜放弃本应提供的功能1.4 错误处理机制太原始1.5 垃圾回收器(GC)不完善.有重大缺陷1.6 禁止未 ...
- HDU5086——Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...