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

首先预处理出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. java多线程编程核心技术——第二章总结

    第一节synchronized同步方法目录 1.1方法内的变量为线程安全的 1.2实例变量非线程安全 1.3多个对象多个锁 1.4synchronized方法与锁对象 1.5脏读 1.6synchro ...

  2. I2C Bus

    概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...

  3. HDU1548(楼梯问题bfs)

    #include"cstdio" #include"queue" #include"cstring" using namespace std ...

  4. bzoj4545

    lct+SAM bzoj4516+bzoj2555 这道题唯一的用处就是教会了我真正的广义SAM dfs时保留当前节点在后缀自动机中的位置,每个点接着父亲建 lct动态维护right集合大小,用lct ...

  5. hibernate 数据关联多对多

    多对多,必须有一张关系表来维持关系 数据库student,teacher student_teacher 三张表 但是在pojo中只需要建立student和teacher两个类,除非关系表也代表某种业 ...

  6. CentOS7下二进制文件安装MySQL5.6

    1.查看已装包 [root@host2 ~]# rpm -qa | grep mysql mysql-libs-5.1.71-1.el6.x86_64 [root@host2 ~]# [root@ho ...

  7. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  8. [matlab]bp神经网络工具箱学习笔记

    基本就三个函数: newff():创建一个bp神经网络 train():训练函数 sim():仿真函数 同时具有可视化界面,但目前不知道可视化界面如何进行仿真,且设置不太全 工具箱:Neural ne ...

  9. [51nod1384]全排列

    法一:next_permutation函数,两个参数分别为起始指针和末尾指针. #include<bits/stdc++.h> using namespace std; typedef l ...

  10. 阶段2-新手上路\项目-移动物体监控系统\Sprint2-摄像头子系统开发\第2节-V4L2图像编程接口深度学习

    参考资料: http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.htmlhttp://blog.csdn.net/eastmoon5021 ...