题意:给一棵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. 课下作业04-2String的使用方法

    1.动手动脑之String.equals()方法public class StringEquals { public static void main(String[] args) { String ...

  2. SAP HANA

    DROP PROCEDURE ""."ZCONCAT_EKKO_EBN"; CREATE PROCEDURE ""."ZCONCA ...

  3. 转 Hystrix入门指南 Introduction

    https://www.cnblogs.com/gaoyanqing/p/7470085.html

  4. 《Spring源码深度解析》第三章 默认标签的解析

    上一章提到了,默认标签和自定义标签要分开解析.本章重点介绍默认标签的解析.在 DefaultBeanDefinitionDocumentReader 中: private void parseDefa ...

  5. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡

    ========================================================================================== 一.基础介绍 == ...

  6. leetcode-4-basic

    解题思路:这道题比较简单,代码不贴了.需要注意的是: 数字与字符串之间的转换, char str[100]; sprintf(str, "%d", num); 解题思路: 这道题是 ...

  7. CentOS 7.0 使用 yum 安装 MariaDB 及 简单配置

    1.安装MariaDB 安装命令 yum -y install MariaDB-server MariaDB-client 安装完成MariaDB,首先启动MariaDB 设置开机启动 接下来进行Ma ...

  8. navigationcontroller和navigationbar和navigationitem之间的区别以及不用nib实现点击屏幕关闭虚拟键盘20130911

    1.UIViewController UIView的关系. UIView是视图,UIViewController是视图控制器,两者之间是从属关系,当创建一个UIViewController的时候,一般 ...

  9. Ubuntu添加环境变量

    在 Ubuntu 系统中有两种设置环境变量 PATH 的方法.第一种适用于为单一用户设置 PATH,第二种是为全局设置 PATH. 第一种方法: 在用户主目录下有一个 .bashrc 文件,可以在此文 ...

  10. 1036: [ZJOI2008]树的统计Count(树链剖分)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 19830  Solved: 8067[Submit ...