题目,是对一颗树,单点修改、子树查询。典型的dfs序入门题。

DFS序可以将一颗树与子树们表示为一个连续的区间,然后用线段树来维护;感觉算是树链剖分的一种吧,和轻重链剖分不同的是这是对子树进行剖分的。

我用非递归方式求DFS序。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111
struct Edge{
int v,nxt;
}edge[MAXN<<];
int n,NE,head[MAXN];
void addEdge(int u,int v){
edge[NE].v=v; edge[NE].nxt=head[u]; head[u]=NE++;
} int f[MAXN],l[MAXN],r[MAXN],stack[MAXN],odr;
void dfs(){
int top=;
stack[++top]=;
while(top){
int u=stack[top];
if(l[u]){
r[u]=odr; --top;
continue;
}
l[u]=++odr;
for(int i=head[u]; i!=-; i=edge[i].nxt){
int v=edge[i].v;
if(f[u]==v) continue;
f[v]=u; stack[++top]=v;
}
}
} int N,tree[MAXN<<],x,y;
void update(int i,int j,int k){
if(i==j){
tree[k]^=;
return;
}
int mid=i+j>>;
if(x<=mid) update(i,mid,k<<);
else update(mid+,j,k<<|);
tree[k]=tree[k<<]+tree[k<<|];
}
int query(int i,int j,int k){
if(x<=i&&j<=y) return tree[k];
int mid=i+j>>,res=;
if(x<=mid) res=query(i,mid,k<<);
if(y>mid) res+=query(mid+,j,k<<|);
return res;
} int main(){
int m,a,b;
char op[];
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=;i<n;++i){
scanf("%d%d",&a,&b);
addEdge(a,b);
addEdge(b,a);
}
dfs();
for(N=; N<odr; N<<=);
for(int i=; i<=n; ++i){
x=l[i];
update(,N,);
}
scanf("%d",&m);
while(m--){
scanf("%s%d",op,&a);
if(op[]=='Q'){
x=l[a]; y=r[a];
printf("%d\n",query(,N,));
}else{
x=l[a];
update(,N,);
}
}
return ;
}

POJ3321 Apple Tree(DFS序)的更多相关文章

  1. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  2. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  5. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  6. POJ 3321 Apple Tree DFS序+fenwick

    题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...

  7. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  8. POJ3321/Apple tree/(DFS序+线段树)

    题目链接 Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9692 Accepted: 3217 Descr ...

  9. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

随机推荐

  1. cocos基础教程(6)坐标与锚点讲解

    坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系.原点为屏幕左下角,x向右,y向上. 世界坐标系(World Coordinate) VS 本地坐标系(Node L ...

  2. ■SQL注入自学[第三学:注入点的读写、out_file]

    00x1 判断是否可读 code: http:.php?id and (select count(*) from mysql.user) >0--/*返回正确的话,说明没有是有读的权限.返回错误 ...

  3. 【ERROR】使用jquery的ajax出现error:readyState=4,status=500

    使用jquery的ajax出现error:readyState=4,status=500,ajax代码如下: $.ajax({ url : "../toBeFinMisManage/show ...

  4. java 异常处理 Throwable Error 和Exception

    Java异常类层次结构图:       异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误. 比如使用 ...

  5. poj2778

    题意:给出字符串长度n(<=2000000000),给出不可以包含的序列,最多10个,每个长度最大是10.问长度为n的合法序列有多少个?序列中只可能包含ACTG四个字符. 分析:AC自动机(DF ...

  6. Java for LeetCode 160 Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. (转)JAVA AJAX教程第一章-初始AJAX

    既然是认识AJAX,理论和实践相结合,这样让自己学的更快,理解更深入,我分一下几点: 1.  认识传统的同步交互方式和AJAX解决方案 2.  AJAX使用到的技术 3.  实例体验AJAX 一.同步 ...

  8. Js数组里删除指定的元素(不是指定的位置)

    转载自:http://my.oschina.net/zh119893/blog/265964 之前一直是做后端的,从来也没有写过js,但是却一直想学学,也只是基于兴趣而已!现在到了这个公司,确实大量的 ...

  9. php的socket通信(二)

    案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...

  10. 读取STL模型

    读取二进制格式的STL模型文件 std::ifstream fin;fin.open(stlFilePath, std::ios::in | std::ios::binary);bool isBina ...