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 动态更新某个元素,并且求和,显然是二叉索引树,但是节点的标号不连续,二叉索引树必须是连续的,所以需要转化成连续的,多叉树的形状已经建 ...
随机推荐
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- HDU 3507 Print Article(斜率优化DP)
题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上 ...
- Play Framework 2.2.6 安装
网络上很多安装方法都是互相复制黏贴的, 都没有人考虑到启动application 还有依赖很多jar 包,而其中typesafe 官网提供的只是一个mini的 启动器来安装,很慢,所以以下下载完整包. ...
- jQuery对象与Dom对象的相互转换
1.jQuery对象转换为Dom对象 [index] var $d = $("#id"); ]; get(index) var $d = $("#id"); ) ...
- lintcode:anagrams 乱序字符串
题目 乱序字符串 给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 您在真实的面试中是否遇到过这个 ...
- LR_问题_控制器不能使用定义的负载生成器
问题描述 在controller 中设置了面向目标的方案后 执行提示 The target you defined cannot be reached.the LoadRunner Controlle ...
- React组件测试(模拟组件、函数和事件)
一.模拟组件 1.用到的工具 (1)browerify (2)jasmine-react-helpers (3)rewireify(依赖注入) (4)命令:browserify - t reactif ...
- [iOS]解决模拟器无法输入中文问题
第一步:设置schem 菜单项 -> Product-> Scheme -> Edit Scheme -> 然后在弹出的界面里 选择OPtion 项, 设置 Applicat ...
- 计算机技能get(windows系统)
1.快速打开程序,比如计算器,注册表,先按win键(不用再按win+r啦),输入程序名字,如calc,regedit等,直接打开. 2.自动左右分屏,win+上下左右方向键,win+↑ 最大化,win ...
- epoll和poll效率差异
http://blog.163.com/sky20081816@126/blog/static/164761023201073033517435/ 百度“epoll和poll”