Codeforces Round #225 (Div. 2)---E. Propagating tree(时间戳+线段树)

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 test(s)

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

Output

3

3

0

Note

The values of the nodes are [1,?2,?1,?1,?2] at the beginning.

Then value 3 is added to node 2. It propagates and value -3 is added to it’s sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1,?5,?1,??-?2,??-?1].

Then value 2 is added to node 1. It propagates and value -2 is added to it’s sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it’s sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3,?3,??-?1,?0,?1].

You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)

很明显要先跑一个dfs得到时间戳,但是这还不够,这里对某个节点加上某个数以后,它的儿子要减去那个数,它儿子的儿子要加上那个数,所以我们要根据深度的奇偶,把时间戳搞成2个数组,建立2颗线段树

/*************************************************************************
    > File Name: CF225-E.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月21日 星期二 21时11分04秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 300010;

int par[N];
int oddL[N];
int oddR[N];
int evenL[N];
int evenR[N];
int subL[N];
int subR[N];
int dep[N];
int arr[N];
int now[N];
int sta[N];
int is_leaf[N];

struct node{
    int add;
    int l, r;
    int sum;
}tree[2][N << 2];

struct EDGE {
    int to;
    int nxt;
}edge[N << 1];
int head[N], tot;

void addedge(int from, int to) {
    edge[tot].to = to;
    edge[tot].nxt = head[from];
    head[from] = tot++;
}

void build(node tree[], int p, int l, int r) {
    tree[p].l = l;
    tree[p].r = r;
    tree[p].add = 0;
    if (l == r) {
        tree[p].sum = now[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(tree, p << 1, l, mid);
    build(tree, p << 1 | 1, mid + 1, r);
    tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
}

void pushdown(node tree[], int p) {
    if (tree[p].add) {
        tree[p << 1].add += tree[p].add;
        tree[p << 1 | 1].add += tree[p].add;
        int m = tree[p << 1].r - tree[p << 1].l + 1;
        tree[p << 1].sum += m * tree[p].add;
        m = tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1;
        tree[p << 1 | 1].sum += m * tree[p].add;
        tree[p].add = 0;
    }
}

void update(node tree[], int p, int l, int r, int val) {
    if (l == tree[p].l && r == tree[p].r) {
        tree[p].add += val;
        int m = r - l + 1;
        tree[p].sum += m * val;
        return;
    }
    pushdown(tree, p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (r <= mid) {
        update(tree, p << 1, l, r, val);
    }
    else if (l > mid) {
        update(tree, p << 1 | 1, l, r, val);
    }
    else {
        update(tree, p << 1, l, mid, val);
        update(tree, p << 1 | 1, mid + 1, r, val);
    }
    tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
}

int query(node tree[], int p, int pos) {
    if (tree[p].l == tree[p].r) {
        return tree[p].sum;
    }
    pushdown(tree, p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (pos <= mid) {
        return query(tree, p << 1, pos);
    }
    else {
        return query(tree, p << 1 | 1, pos);
    }
}

void getDepth(int u, int fa, bool flag) { // 1 -> 奇数, 2 -> 偶数
    par[u] = fa;
    dep[u] = flag;
    bool ok = 1;
    for (int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (v == fa) {
            continue;
        }
        getDepth(v, u, flag ^ 1);
        ok = 0;
    }
    is_leaf[u] = ok;
}

void dfs1(int u, int fa, int &ret) {
    if (dep[u]) {
        oddL[u] = ++ret;
    }
    for (int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (v == fa) {
            continue;
        }
        dfs1(v, u, ret);
    }
    if (dep[u]) {
        oddR[u] = ret;
    }
}

void dfs2(int u, int fa, int &ret) {
    if (!dep[u]) {
        evenL[u] = ++ret;
    }
    for (int i = head[u]; ~i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (v == fa) {
            continue;
        }
        dfs2(v, u, ret);
    }
    if (!dep[u]) {
        evenR[u] = ret;
    }
}

int main() {
    int n, m;
    while (~scanf("%d%d", &n, &m)) {
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &arr[i]);
        }
        memset(head, -1, sizeof(head));
        memset(is_leaf, 0, sizeof(is_leaf));
        tot = 0;
        int u, v;
        for (int i = 1; i <= n - 1; ++i) {
            scanf("%d%d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        int L1 = 0, L2 = 0;
        getDepth(1, -1, 1);
        dfs1(1, -1, L1);
        dfs2(1, -1, L2); //偶数
        for (int i = 1; i <= n; ++i) {
            subL[i] = inf;
            subR[i] = -inf;
            if (is_leaf[i]) {
                continue;
            }
            for (int j = head[i]; ~j; j = edge[j].nxt) {
                if (edge[j].to == par[i]) {
                    continue;
                }
                if (dep[i]) {
                    subL[i] = min(subL[i], evenL[edge[j].to]);
                    subR[i] = max(subR[i], evenR[edge[j].to]);
                }
                else {
                    subL[i] = min(subL[i], oddL[edge[j].to]);
                    subR[i] = max(subR[i], oddR[edge[j].to]);
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (!dep[i]) { //偶数
                now[evenL[i]] = arr[i];
            }
        }
        if (L2 >= 1) {
            build(tree[0], 1, 1, L2);
        }
        for (int i = 1; i <= n; ++i) {
            if (dep[i]) {
                now[oddL[i]] = arr[i];
            }
        }
        if (L1 >= 1) {
            build(tree[1], 1, 1, L1);
        }
        int op, x, y;
        while (m--) {
            scanf("%d", &op);
            if (op == 1) {
                scanf("%d%d", &x, &y);
                if (dep[x]) {
                    int l = oddL[x];
                    int r = oddR[x];
                    update(tree[1], 1, l, r, y);
                    if (!is_leaf[x]) {
                        l = subL[x];
                        r = subR[x];
                        update(tree[0], 1, l, r, -y);
                    }
                }
                else {
                    int l = evenL[x];
                    int r = evenR[x];
                    update(tree[0], 1, l, r, y);
                    if (!is_leaf[x]) {
                        l = subL[x];
                        r = subR[x];
                        update(tree[1], 1, l, r, -y);
                    }
                }
            }
            else {
                scanf("%d", &x);
                if (dep[x]) {
                    printf("%d\n", query(tree[1], 1, oddL[x]));
                }
                else {
                    printf("%d\n", query(tree[0], 1, evenL[x]));
                }
            }
        }
    }
    return 0;
}
时间: 2024-08-09 18:54:45

Codeforces Round #225 (Div. 2)---E. Propagating tree(时间戳+线段树)的相关文章

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 Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numb

Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所击败了(l<=x<=r),被击败的人立马退出游戏让你最后输出每个人是被谁击败的,最后那个胜利者没被 人击败就输出0 思路:他的每次修改的是一个区间的被击败的人,他而且只会记录第一次那个被击败的人,用线段树堕落标记的话他会记录最后一次的,所以我们倒着来修改, 然后因为那个区间里面还包含了自己,在线段

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l;r] of the string s is the string slsl+1-sr. For example, the substrings

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C..但是挺简单的..只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护.每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.

Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All

Codeforces Round #245 (Div. 1)——Guess the Tree

本文出自:http://blog.csdn.net/svitter 实验环境:Myeclipse10 + tomcat7.0 有时间会写windows和linux下的tomcat配置,现在时间有限,暂且不写了..有些东西也是没有理解透彻. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <%@ page language="java" contentType="

Codeforces Round #406 (Div. 2) E. Till I Collapse(主席树)

题目链接:Codeforces Round #406 (Div. 2) E. Till I Collapse 题意: 给你n个数,对于每一个k(1<=k<=n),划分区间,每个区间只能有k个不同的数字, 问最小的划分区间的个数. 题解: 用主席树倒着将数插入,对于每个区间询问第k个不同数的位置就行了. #include<bits/stdc++.h> #define F(i,a,b) for(int i=a;i<=b;i++) #define ___ freopen("

Codeforces Round #225 (Div. 1) C 树状数组 || 线段树

看到这题很开心啊,有印象跟以前做过的很像,貌似最近就做过一个,以时间戳为区间来建立树状数组,然后一开始我以为题意是,给x点加val,它以下的所有节点都加-val:所以一开始就以 加 和 减 建立了两个树状数组,最后 减去就是答案,写完发现跟案例对不上啊,读了题目也没发现读错了,对于那句话 我理解错了,后来看了 这个: http://blog.csdn.net/keshuai19940722/article/details/18967661 仔细看看处理部分,我还以为分奇偶性有规律呢,后来才发现读