POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong
~去博客园看该题解~
题目
POJ3321 Apple Tree
题意概括
有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作:
1.改变其中一个结点的权值(0变1,1变0)
2.询问子树X的节点权值和。
输入描述
一组数据。
先是一个数n,表示有n个节点。
接下去n-1行,每行表示一条边。
然后一个数m,表示有m个操作。
然后m行,每行一个字母一个数x,如果字母是Q,则是询问;否则是修改。
输出描述
每一个询问一行,表示答案。
题解
直接把题目变成dfs序上单点修改和区间sum询问的问题。
单点修改,不用线段树,树状数组就可以了。
如果你非要用线段树我也不拦你……
代码
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=+,M=N*;
struct Edge{
int cnt,y[M],nxt[M],fst[N];
void set(){
cnt=;
memset(fst,,sizeof fst);
}
void add(int a,int b){
y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
}
}e;
int n,m;
int in[N],out[N],time;
int tree[N],v[N];//树状数组
int lowbit(int x){
return x&-x;
}
void dfs(int prev,int rt){
in[rt]=++time;
for (int i=e.fst[rt];i;i=e.nxt[i])
if (e.y[i]!=prev)
dfs(rt,e.y[i]);
out[rt]=time;
}
void update(int x,int d){
for (;x<=n;x+=lowbit(x))
tree[x]+=d;
}
int sum(int x){
int ans=;
for (;x>;x-=lowbit(x))
ans+=tree[x];
return ans;
}
int query(int L,int R){
return sum(R)-sum(L-);
}
int main(){
e.set();
scanf("%d",&n);
for (int i=,a,b;i<n;i++){
scanf("%d%d",&a,&b);
e.add(a,b);
e.add(b,a);
}
time=;
dfs(,);
for (int i=;i<=n;i++)
tree[i]=lowbit(i),v[i]=;
scanf("%d",&m);
for (int i=,x;i<=m;i++){
char ch[];
scanf("%s%d",ch,&x);
if (ch[]=='Q')
printf("%d\n",query(in[x],out[x]));
else
update(in[x],-v[x]*),v[x]^=;
}
return ;
}
POJ3321Apple Tree Dfs序 树状数组的更多相关文章
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
随机推荐
- LightOJ1004
#include<bits/stdc++.h> using namespace std; int Map[106][106]; int Vis[106][106]; int Num[106 ...
- Boostrap轮图片可以左右滑动
记得引用Boostrap的js和css html代码: <div id="Mycarousel" class="carousel slide col-md-12&q ...
- 10)django-ORM(创建,字段类型,字段参数)
一:ORM关系对象映射(Object Relational Mapping,简称ORM) ORM分两种: DB first 先在数据库中创建数据库表等 Code first 先创建类,然后根据类创建数 ...
- GIT 版本管理-github&码云
GIT 是什么 git是一个用于帮助用户实现版本控制的软件. 把本地代码push到远程服务器 git add.git commitpush code to remove machine.git pus ...
- 判断js数据类型的四种方法,以及各自的优缺点(转)
转载地址:https://blog.csdn.net/lhjuejiang/article/details/79623973 数据类型分为基本类型和引用类型: 基本类型:String.Number.B ...
- 通过printf从目标板到调试器的输出
最近在SEGGER的博客上看到Johannes Lask写的一篇关于在调试时使用printf函数从目标MCU输出信息到调试器的文章,自我感觉很有启发,特此翻译此文并推荐给各位同仁.当然限于个人水平,有 ...
- 其他 Confluence 6 的 cookies 和备注
其他 Confluence 的 cookies 针对 Confluence 的功能,我们还使用了其他的一些 cookies 来存储基本的 产品持久性(product presentation).Con ...
- leetcode(js)算法之17电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母 示例: 输入:"23" 输出:[" ...
- func_get_args函数
func_get_args ------获取一个函数的所有参数 function foo() { $numargs = func_num_args(); //参数数量 echo "参数个数是 ...
- Niagara workbench 介绍文档---翻译
一. 发现在建立station的时候存在一些问题,所以对技术文档部分做一个详细的了解,在这之前对出现的问题总结一下 1. 在 Windows操作系统中Application Direction中可以 ...