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

首先预处理出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. bzoj 3157 & bzoj 3516 国王奇遇记 —— 推式子

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3157 https://www.lydsy.com/JudgeOnline/problem.p ...

  2. 在java web项目中编写自己的代码生成器

    在java web项目中编写自己的代码生成器

  3. spring--设置注入VS构造注入

    1.在传统的程序设计中,调用亲自创建被调用者的实例,即由程序控制“对象之间的依赖关系”,这种方式的耦合度比较高:控制反转就是将由程序控制的“对象间的依赖关系”转交给Ioc容器来进行控制,被调用者的实例 ...

  4. JavaScript设计模式--桥梁模式--引入

    1.使用情况 (1)事件的监控 #1,利用页面的button来选择宠物的例子(思路) button.addEvent(element,"click",getPetByBame); ...

  5. 值得细细品读的URL资源

    Web安全渗透方面的资源指南: 知乎综合问答:https://www.zhihu.com/question/21914899 渗透测试经典演练系统:http://www.freebuf.com/sec ...

  6. ubuntu安装wget

    ubuntu安装wget apt-get update apt-get install wget wget --version

  7. console (控制台)

    console 模块提供了一个简单的调试控制台,类似于 Web 浏览器提供的 JavaScript 控制台. 该模块导出了两个特定的组件: 一个 Console 类,包含 console.log()  ...

  8. Luogu 2258 [NOIP2014] 子矩阵

    被普及组虐了,感觉

  9. Entity Framework Code-First(9.11):DataAnnotations - InverseProperty Attribute

    DataAnnotations - InverseProperty Attribute: We have seen in the Code-First Convention section that ...

  10. 2. DVWA亲测命令执行漏洞

        先看low级: 提示让我们输入一个IP地址来实现ping,猜测会是在系统终端中实现的, 我们正常输入127.0.0.1: 那我们就可以利用这个使用其他CMD命令  我们输入127.0.0.1& ...