题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作

1.将某个结点的苹果数异或 1
2.查询一棵子树内的苹果数
n,m<=100000
 
思路:最近一段时间在思考树上统计问题的算法
发现询问一棵子树中信息的问题一般都是DFS序+线段树或BIT维护
树上两点之间的查询一般都是树剖维护
比如说这题,单点修改+区间查询子树信息,转化为DFS序用BIT维护即可
注意有一个性质:U在DFS序中第一次出现的时刻是b[u],则它的子树就是区间(b[u],b[u]+size[u]-1)
 var t:array[..]of longint;
head,vet,next,a,b,c,size,flag:array[..]of longint;
n,m,x,y,i,j,tot,time,p:longint;
ch:string; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; procedure dfs(u:longint);
var e,v:longint;
begin
flag[u]:=;
inc(time); a[time]:=u; b[u]:=time; size[u]:=;
e:=head[u];
while e<> do
begin
v:=vet[e];
if flag[v]= then
begin
dfs(v);
size[u]:=size[u]+size[v];
end;
e:=next[e];
end;
end; function lowbit(x:longint):longint;
begin
exit(x and (-x));
end; procedure update(x,p:longint);
begin
while x<=n do
begin
t[x]:=t[x]+p;
x:=x+lowbit(x);
end;
end; function sum(x:longint):longint;
begin
sum:=;
while x> do
begin
sum:=sum+t[x];
x:=x-lowbit(x);
end;
end; begin
assign(input,'poj3321.in'); reset(input);
assign(output,'poj3321.out'); rewrite(output);
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs();
for i:= to n do
begin
c[i]:=;
update(b[i],);
end;
readln(m);
for i:= to m do
begin
readln(ch);
x:=;
for j:= to length(ch) do x:=x*+ord(ch[j])-ord('');
if ch[]='C' then
begin
if c[x]= then p:=-
else p:=;
update(b[x],p);
c[x]:=c[x] xor ;
end;
if ch[]='Q' then writeln(sum(b[x]+size[x]-)-sum(b[x]-));
end;
close(input);
close(output);
end.
 

【POJ3321】Apple Tree(DFS序,树状数组)的更多相关文章

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

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

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

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

  3. 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 ...

  4. 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 ...

  5. POJ3321Apple Tree Dfs序 树状数组

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

  6. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  7. 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 ...

  8. 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 ...

  9. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  10. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

随机推荐

  1. Maven:项目结构

    目录结构图: project        |- src            |- main   //工程源代码目录                |- java        //工程java源代 ...

  2. UML类图中的六种线条与六种关系

    1.泛化(generalize) 实线空心三角箭头. 2.实现(realize) 虚线空心三角箭头. 3.聚合(aggregation) 实线空心菱形箭头. 4.组合(composition) 实线实 ...

  3. SAP 常用业务数据表设计

    表的要求表中使用的字段请尽量参照各模块的SAP字段标准使用习惯:  例:"ZXSLRZX销售组织对应的利润中心"中的销售组织应该使用VKORG.利润中心应该使用PRCTR.根据表的 ...

  4. Linux 命令、配置文件及操作

    Linux 命令.配置文件及操作 命令 命令 参数 说明 A alias.unalias 命令别名 B C cat 查看文件内容 cd 切换目录 chown 修改拥有着 chgrp 修改所属组 chm ...

  5. 【Linux】开放指定端口设置

    这里以开放tomcat的8080端口为例 1.开放Linux的8080端口 vi /etc/sysconfig/iptables 进入编辑页面,在指定位置新增以下配置 -A INPUT -m stat ...

  6. Linux 安装Nginx+PHP+MySQL教程

    一.安装nginx 通过yum安装openssl: yum -y install openssl openssl-devel 通过yum安装pcre: yum -y install pcre-deve ...

  7. linux下的一些命令分析与shell的一些命令

    对> 与 >>的理解 echo "aaa" > aaa.txt 这个是在aaa.txt中写入aaa   可以用cat  aaa.txt查看 echo &qu ...

  8. Java并发编程的艺术 记录(二)

    volatile的应用 volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量.Java语言提供了volatil ...

  9. machine_desc

    每一个machine,都要定义一个自己的machine_desc结构,该结构定义了该machine的一些最基本的特性. struct machine_desc { unsigned int nr; / ...

  10. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...