题意:给一棵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. MySQL对数据库数据进行复制的基本过程详解

    MySQL对数据库数据进行复制的基本过程详解 这篇文章主要介绍了MySQL对数据库数据进行复制的基本过程,解读了Slave的一些相关配置,需要的朋友可以参考下 复制 复制是从一个MySQL服务器(ma ...

  2. 配置centos7解决 docker Failed to get D-Bus connection 报错

    在centos7的容器里面出现了一个BUG,就是serveice启动服务的时候出现报错,不能用service启动服务.[root@e13c3d3802d0 /]# service httpd star ...

  3. 27. Remove Element@python

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  4. Python中的字典与集合

    今天我们来讲一讲python中的字典与集合 Dictionary:字典 Set:集合 字典的语法: Dictionary字典(键值对) 语法: dictionary = {key:value,key: ...

  5. Html5_标签

    HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...

  6. UVA - 11572 Unique Snowflakes 滑动扫描

    题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...

  7. HDU:4185-Oil Skimming

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...

  8. 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)

    An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  9. LAMP动态网站安装脚本

    #!/bin/bash #auto make install LAMP #by authors zhangjianghua #httpd define path variable H_FILES=ht ...

  10. luogu2764 最小路径覆盖问题

    最小路径覆盖,看这里 #include <iostream> #include <cstring> #include <cstdio> #include <q ...