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. android开发软件

    android开发软件: http://developer.android.com/sdk/index.html#download

  2. 安卓 安装 platforms 的时候报错--访问 url 出错

    强制使用http 访问连接,并在hosts中添加地址,即可 问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com ...

  3. 状态管理cookie 案例

    1状态管理:服务器为了追踪同一个客户端发出的请求,将多次交互看成一个整体看待 2:cookie的生存时间,默认情况下,cookie保存在浏览器内存中,只要不关闭浏览器,cookie就一直存在 如果希望 ...

  4. mybatis源码学习: 编译的方法

    mybatis3用了一段时间,抽出时间来研究一下.具体用法参考官方文档就行,源码在这里.mybatis相对而言,规模较小,可以从中学习如何编写高质量的java项目. mybatis3使用maven管理 ...

  5. Flex之HTTPService组件调用

    1.采用<s:HTTPService>标签来实现: <?xml version="1.0" encoding="utf-8"?>< ...

  6. PHP上传大文件和处理大数据

    1. 上传大文件 /* 以1.5M/秒的速度写入文件,防止一次过写入文件过大导致服务器出错(chy/20150327) */ $is_large_file = false; if( strlen($x ...

  7. 在Spring MVC中使用注解的方式校验RequestParams

    概述   Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...

  8. [札记]IL经典指令解析之方法调度

    call.callvirt和calli指令用于完成方法调用,有何区别呢? 1)call使用静态调度,也就是根据引用类型的静态类型来调度方法.call指令根据引用变量的类型来调用方法,因此通常用于调用非 ...

  9. rhel5.5 Oracle10g安装记录

    ---恢复内容开始--- Rhel5.5 Oracle10g安装成功截图如下

  10. 第二百三十三天 how can I 坚持

    刚才看了场球,亚冠恒大和迪拜阿尔阿赫利,1:0,刚打开电脑就看到了进球,还是很幸运的. 在家待了一天,阴天,预报明天又中到大雪 啊,下吧.好希望下场大雪啊. 最近一直感觉好累,写代码不容易啊 ,还是因 ...