C. Propagating tree

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/383/problem/C

Description

Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.

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序+树状数组的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  6. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

  7. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  8. POJ3321Apple Tree Dfs序 树状数组

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

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

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

随机推荐

  1. Dev gridControl z

    Dev gridControl 添加表标题 1.OptionsView ->ShowViewCaption = True 2.ViewCaption = "标题" Dev g ...

  2. Dyslexic Gollum

    题意: 求长度是n的二进制串中,不含长度大于等于k的回文串的个数 分析: dp[i][j][k]表示长度i,后11位状态是j不含长度大于等于k的回文串的个数(因为k最大是10,所把后11位状态压缩,d ...

  3. JavaScript 教程学习进度备忘

    书签:"JavaScript 课外书"即“JS 教程”底部的“马上开始学习 JavaScript 高级教程吧 !”链接跳过,它属于高级教程:另外跳过的内容有待跟进 ________ ...

  4. Laravel不同数据库的模型之间关联

    假设ModelA和ModelB之间是BelongsTo的关系 如果同属于一个数据库连接 那么 public function a(){ return $this->belongsTo(" ...

  5. 【LeetCode】58 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. Camel In Action 阅读笔记 第一章 认识Camel 1.1 Camel 介绍

    1.1 Camel 介绍 Camel 是一个为了您的项目集成变得高效有趣的集成框架,Camel 项目在2007年初开始的,相对来说它还比较年轻,但它已然是一个非常成熟的开源项目,它所使用的是Apach ...

  7. Java基础 —— Java常用类

    Java常用类: java.lang包: java.lang.Object类: hashcode()方法:返回一段整型的哈希码,代表地址. toString()方法:返回父类名+"@&quo ...

  8. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

  9. cookie一些简单的操作

    cookie 保存数据      document.cookie=name+'='+value+';expires='+date; //name=shiyou ;expires=Tue Mar 08 ...

  10. Hadoop MapReduce概念学习系列之map并发任务数和reduce并发任务数的原理和代码实现(十八)

    首先,来说的是,reduce并发任务数,默认是1. 即,在jps后,出现一个yarnchild.之后又消失. 这里,我控制reduce并发任务数6 有多少个reduce的并发任务数可以控制,但有多少个 ...