题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1。

分析:

对树进行dfs,将树变为序列,记录每个点进栈出栈的时间戳作为其对应区间的左右端点,那么其子节点对应区间必在该区间内,对这段区间求和即可,用树状数组进行修改与求和。

代码:

program apple;
type
point=^node;
node=record
x:longint; next:point;
end;
var
a:array[..]of point;
bit:array[..]of longint;
b,v1,v2:array[..]of longint;
g:array[..]of boolean;
n,i,m,d,x,y:longint; c:char;
procedure add(x,y:longint);
var p:point;
begin
new(p); p^.x:=y; p^.next:=a[x]; a[x]:=p;
end;
procedure dfs(x:longint);
var p:point;
begin
inc(d); v1[x]:=d; g[x]:=true; new(p); p:=a[x];
while p<>nil do
begin
if g[p^.x]=false then dfs(p^.x); p:=p^.next;
end;
inc(d); v2[x]:=d;
end;
procedure change(x:longint);
var i,y:longint;
begin
y:=x;
if b[x]= then i:= else i:=-;
x:=v1[x];
while (x<=*n) do
begin inc(bit[x],i); inc(x,x and (-x));end;
b[y]:=-b[y];
end;
function query(x:longint):longint;
var s:longint;
begin
s:=;
while x> do
begin inc(s,bit[x]); dec(x,x and (-x)); end;
exit(s);
end;
begin
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y); add(y,x);
end;
d:=; dfs();
for i:= to n do change(i);
readln(m);
for i:= to m do
begin
readln(c,x);
if c='C' then change(x)
else writeln(query(v2[x])-query(v1[x]-));
end;
end.

POJ 3321:Apple Tree(dfs序+树状数组)的更多相关文章

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

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

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

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

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

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

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

  6. POJ 3321 Apple Tree DFS序+fenwick

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

  7. POJ3321Apple Tree Dfs序 树状数组

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

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

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

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

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

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

随机推荐

  1. POJ 3057 Evacuation(二分匹配)

    分析: 这是一个时间和门的二元组(t,d)和人p匹配的问题,当我们固定d0时,(t,d0)匹配的人数和t具有单调性. t增加看成是多增加了边就行了,所以bfs处理出p到每个d的最短时间,然后把(t,d ...

  2. Aizu 0525 Osenbei(状压+贪心)

    题意:翻煎饼,只能横着翻或者竖着翻.问最多有多少朝上? 行只有10,所以枚举一下2^10的状态,每列取0或1中最大的一个. 在枚举外面把饼翻好,枚举里面指针指一下就好.(位运算或bitset乱搞 #i ...

  3. IPython安装过程 @win7 64bit

    http://www.360doc.com/content/14/0902/11/16740871_406476389.shtml 为了测验测验一下IPython的应用,今天折腾了好久的从安装包msi ...

  4. 【Java】基本数据类型以及其转换

    整理了一下Java基本数据类型和面试可能涉及的知识.      字节数(byte)  位数(bit)  取值范围 整型  byte  1  8  -2^7 ~ 2^7 -1 short   2  16 ...

  5. Lepus天兔数据库监控系统安装配置

    [root@redis01 ~]# cd /opt/[root@redis01 opt]# wget https://www.apachefriends.org/xampp-files/5.6.36/ ...

  6. Java 解决IE浏览器下载文件,文件名出现乱码问题

    /** * 区分ie 和其他浏览器的下载文件乱码问题 * @param request * @param fileName * @return */ public String getFileName ...

  7. MySQL主从复制原理及配置过程

    一.Mysql数据库的主从复制原理过程: (多实例的安装请参考我的另一篇文章:https://www.cnblogs.com/Template/p/9258500.html) Mysql的主从复制是一 ...

  8. 用随机森林分类器和GBDT进行特征筛选

    一.决策树(类型.节点特征选择的算法原理.优缺点.随机森林算法产生的背景) 1.分类树和回归树 由目标变量是离散的还是连续的来决定的:目标变量是离散的,选择分类树:反之(目标变量是连续的,但自变量可以 ...

  9. python3.7 random模块

    #!/usr/bin/env python __author__ = "lrtao2010" #python3.7 random模块 import random #随机模块 # r ...

  10. poj 3104 晾衣服问题 最大化最小值

    题意:n件衣服各含有ai水分,自然干一分钟一个单位,放烘干机一分钟k个单位,问:最短时间? 思路: mid为最短时间 如果 a[i]-mid>0说明需要放入烘干机去烘干 烘干的时间为x  那么满 ...