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

首先预处理出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. 在Golang中使用C语言代码实例

    转自:http://www.jb51.net/article/56720.htm cgo 使得在 Golang 中可以使用 C 代码. Hello World 为了有一个较为直观的了解,我们来看一个简 ...

  2. python 基础 字典 小例子

    统计单词次数 作为字典存储 cotent = "who have an apple apple is free free is money you know" result = { ...

  3. [java] volatile关键字对while循环条件提升问题补充

    在java并发编程中,代码如下: volatile boolean asleep; ... while(!asleep){ countSomeSheep(); } 如果此处忘记将asleep变量设置为 ...

  4. ABCD四个人说真话的概率都是1/3。假如A声称B否认C说D是说谎了,那么D说过的那句话真话的概率是多少

    ABCD四个人说真话的概率都是1/3.假如A声称B否认C说D是说谎了,那么D说过的那句话 真话的概率是多少 记"A声称B否认C说D说谎"为X,那么由贝叶斯公式,所求的 P(D真)P ...

  5. socket消息发送

    expressClient.html <html><head><meta http-equiv="Content-Type" content=&quo ...

  6. Maven Cargo 远程部署到tomcat7x

    pom.xml中加入cargo的Plugin声明: <plugin> <groupId>org.codehaus.cargo</groupId> <artif ...

  7. R语言简单作图

    以下函数只为满足常用的若干作图需求. 基本作图: plot(x).plot(x, y) #散点图,最多两个变量     #可使用参数type生成不同的效果图.常用'l'.'o'.'h',分别为折线图, ...

  8. ubuntu下hive-0.8.1配置

    1.下载hive包wget http://labs.mop.com/apache-mirror/hive/stable/hive-0.8.1.tar.gz,并用tar -xzvf 将其解压到要安装的目 ...

  9. Struts2学习第七课 ActionSupport

    com.opensymphony.xwork2.ActionSupport类是默认的Action类,如果某个Action节点没有配置class属性,则ActionSupport即为待执行的Action ...

  10. Struts2学习第六课 实现登录登出功能

    关于Struts2请求的扩展名问题: 1).org.apache.struts2包下的default.properties中配置了struts2应用的一些常量 2).struts.action.ext ...