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/problem/C
Description
This tree has a special property: when a value val is added to a value of node i, the value -val is added to values of all the children of node i. Note that when you add value -val to a child of node i, you also add -(-val) to all children of the child of node i and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
"1 x val" — val is added to the value of node x;
"2 x" — print the current value of node x.
In order to help Iahub understand the tree better, you must answer m queries of the preceding type.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 200000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000). Each of the next n–1 lines contains two integers vi and ui (1 ≤ vi, ui ≤ n), meaning that there is an edge between nodes vi and ui.
Each of the next m lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1 ≤ x ≤ n, 1 ≤ val ≤ 1000.
Output
For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.
Sample Input
5 5
1 2 1 1 2
1 2
1 3
2 4
2 5
1 2 3
1 1 2
2 1
2 2
2 4
Sample Output
3
3
0
HINT
题意
给出一颗有n个节点并一1为根节点的树,每个节点有它的权值,现在进行m次操作,操作分为添加和查询,当一个节点的权值添加val,则它的孩子节点的权值要添加-b。
题解:
dfs序+树状数组
分成两颗树做
http://blog.csdn.net/keshuai19940722/article/details/18967661
代码
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} //************************************************************************************** struct node
{
int l,r,v,d;
}node[maxn];
int n,m;
vector<int> e[maxn];
int bit[][maxn];
int cnt;
void add(int x,int val,int *b)
{
while(x<=n*)
{
b[x]+=val;
x+=(x&(-x));
}
}
int get(int x,int *b)
{
int ans=;
while(x>)
{
ans+=b[x];
x-=(x&(-x));
}
return ans;
}
void dfs(int x,int fa,int d)
{
node[x].l=cnt++;
node[x].d=d;
for(int i=;i<e[x].size();i++)
{
if(e[x][i]==fa)
continue;
dfs(e[x][i],x,-d);
}
node[x].r=cnt++;
}
int main()
{
n=read(),m=read();
for(int i=;i<=n;i++)
node[i].v=read();
for(int i=;i<n;i++)
{
int a=read(),b=read();
e[a].push_back(b);
e[b].push_back(a);
}
cnt=;
dfs(,-,);
for(int i=;i<m;i++)
{
int op=read();
if(op==)
{
int a=read(),b=read();
add(node[a].l,b,bit[node[a].d]);
add(node[a].r+,-b,bit[node[a].d]);
}
else
{
int a=read();
printf("%d\n",node[a].v+get(node[a].l,bit[node[a].d])-get(node[a].l,bit[-node[a].d]));
}
}
}
Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组的更多相关文章
- 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 ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构
D. Water Tree Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
随机推荐
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法----细节决定成败 (sort用法)
C++中cin.cin.get().cin.getline().getline().gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有 ...
- STL源码分析读书笔记--第5章--关联式容器
1.关联式容器的概念 上一篇文章讲序列式容器,序列式容器的概念与关联式容器相对,不提供按序索引.它分为set和map两大类,这两大类各自有各自的衍生体multiset和multimap,的底层机制都是 ...
- sqlserver安装相关问题
最近在部署一个工程,数据库(sqlserver2005develop)遇到不少问题,下面将一一列出. 安装完毕后,无法连接到本地实例. 打开microsoft sql server 2005-> ...
- POJ2406----Power Strings解题报告
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 43514 Accepted: 18153 D ...
- 第三百五十四天 how can I 坚持
你的问题主要在于:读书不多而买书太多:读书太少又特爱思考,还话唠.. 2012年毕业,辗转无锡,上海,最后来到了北京,逛了北京, 2013年,清明去爬了长城,从天通苑搬到了甜水园, 2014年,爬了泰 ...
- NetBeans IDE 7.4 Beta版本build JavaFX时生成的可执行jar包执行时找不到依赖的jar包
现象,执行时抛出java.lang.ClassNotFoundException异常: Executing E:\secondegg\secondegg-reversi\dist\run8022211 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- Struts ForwardAction Example
In Struts MVC model, you have to go thought the Action Controller to get a new view page. In some ca ...
- SD卡中的命令CMD
SD卡中的命令是SD控制器和SD卡之间的桥梁,它封装了SD卡的实现细节,不影响SD卡中FLASH的读写变更. 命令的长度是48位,它的字段如图: SD校准定义的CMD如下:
- 使用MSSQL,连接oracle,对oracle数据进行操作
EXEC sp_addlinkedserver--创建链接服务器.链接服务器让用户可以对 OLE DB 数据源进行分布式异类查询. @server = 'Mktg',--要创建的链接服务器的名称.s ...