多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和。

首先预处理出DFS序L[i]和R[i]

把问题转化为区间查询总和问题。单点修改,区间查询,树状数组即可。

注意修改的时候也要按照dfs序修改,因为你查询就是按照dfs查的,所以修改也要用dfs序修改

L[i]是唯一的。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int first[maxn], L[maxn], R[maxn], a[maxn];
struct edge {
int u, v;
int next;
} e[maxn * ];
int n, num;
void add (int u, int v)
{
++num;
e[num].u = u;
e[num].v = v;
e[num].next = first[u];
first[u] = num;
}
bool book[maxn];
int index;
void dfs (int cur)
{
L[cur] = index;
for (int i = first[cur]; i; i = e[i].next) {
if (book[e[i].v] == ) {
book[e[i].v] = ;
index++;
dfs (e[i].v);
}
}
R[cur] = index;
}
int c[maxn];//树状数组,多case的记得要清空
int lowbit (int x)//得到x二进制末尾0的个数的2次方 2^num
{
return x&(-x);
}
void addc (int pos,int val)//在第pos位加上val这个值
{
while (pos<=n) { //n是元素的个数
c[pos] += val;
pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) //求解:1--pos的总和
{
int ans = ;
while (pos) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
} void work ()
{
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf ("%d%d", &u, &v);
add (u, v);
}
for (int i = ; i <= n; ++i) {
addc (i, );
a[i] = ;
}
dfs ();
int m;
scanf ("%d", &m);
for (int i = ; i <= m; ++i) {
char str[];
int id;
scanf ("%s%d", str, &id);
if (str[] == 'Q') {
printf ("%d\n", get_sum (R[id]) - get_sum (L[id] - ));
} else {
id = L[id]; //查询用dfs序查,那么更改也要用dfs序更改
a[id] = -a[id];
addc (id, a[id]);
}
}
return ;
}
int main ()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (~scanf ("%d", &n)) {
index = ;
work ();
memset (first, , sizeof first);
num = ;
memset (book, , sizeof book);
memset (c, , sizeof c);
}
return ;
}

POJ 3321 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序 线段树 单点更新 区间求和)

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

  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. POJ 3321 Apple Tree DFS序+fenwick

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

  6. POJ3321Apple Tree Dfs序 树状数组

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

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

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

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

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

  9. 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. 清理:db上面的过期的binlog,释放磁盘空间。 (转)

    如果10台以内的db的话,自己手动ssh进去,clean就足以,但是上百台呢,就要写脚本了.大概思路:在 一台db跳转机上面, 写一个脚本,访问slave,远程获取正在复制的master上面的binl ...

  2. POST-GET请求

    在应用中最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet.post与get的不同之处在于post的参数不是放在URL字串里面 ...

  3. 每天一道算法题(1) ——不用乘除法求和1+2+…+n

    题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 方法1:使用函数指针. typedef int (*fu ...

  4. 详解MYSQL各种优化原理

    说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原 ...

  5. 《精通Spring4.X企业应用开发实战》读后感第五章(FactoryBean)

  6. 在Visual Studio开发的项目中引用GAC中的dll

    Open the windows Run dialog (Windows Key + r) Type C:\Windows\assembly\gac_msil. This is some sort o ...

  7. EF Code first 和 DDD (领域驱动设计研究)系列一

    在上个公司工作时,开发公司产品的过程中,接触到了EF Code first. 当时,整个产品的架构都是Lead developer设计建立的,自己也不是特别理解,就赶鸭子上架跟着一起开发了. 现在回过 ...

  8. 2.XML实体注入漏洞攻与防

    XML实体注入基础 当允许引用外部实体时,通过构造恶意内容,可导致读取任意文件.执行系统命令.探测内网端口.攻击内网网站等危害. 简单了解XML以后,我们知道要在XML中使用特殊字符,需要使用实体字符 ...

  9. Go语言学习记录之一(返回指针与返回值的区别)

    先来一个返回指针的测试,结果跟想象一样 type A map[int]string type B struct { A c int } func main() { b := B{make(A), 10 ...

  10. JpetStore目录文件关系分析

    http://www.cnblogs.com/sunsonbaby/archive/2004/09/11/42245.html 从功能方向  至上向下分析 com.ibatis.jpetstore.s ...