每日一水第36弹(树链剖分dfs序[HAOI2015]T2)

http://www.lydsy.com/JudgeOnline/problem.php?id=4034

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;
const int N = 100010;
int head[N], nxt[N*2], pnt[N*2], E;
void add_edge(int a, int b)
{
    pnt[E] = b;
    nxt[E] = head[a];
    head[a] = E++;
}
int pre[N], succ[N], dep[N], size[N];
int L[N], R[N];
void dfs(int u, int fa=-1)
{
    pre[u] = fa, size[u] = 1, succ[u] = -1;
    dep[u] = ~fa ? dep[fa] + 1 : 0;
    int v, mx = 0;
    for(int i = head[u]; ~i; i = nxt[i]) {
        if(fa == (v = pnt[i])) {
            continue;
        }
        dfs(v, u);
        if(size[v] > mx) {
            succ[u] = v, mx = size[v];
        }
        size[u] += size[v];
    }
}
int first[N], id[N], cnt, mp[N];
void make_chain(int u, bool in_chain=false)
{
    first[u] = in_chain ? first[pre[u]] : u;
    L[u] = id[u] = ++cnt;
    mp[cnt] = u;
    if(~succ[u]) {
        make_chain(succ[u], true);
    }
    for(int i = head[u]; ~i; i = nxt[i]) {
        if(succ[u] == pnt[i] ¦¦ pre[u] == pnt[i]) {
            continue;
        }
        make_chain(pnt[i]);
    }
    R[u] = cnt;
}
int v[N];
struct Tree* pool;
struct Tree
{
    Tree *lc, *rc;
    int l, r;
    long long sum, add;
    Tree(int l = 0, int r = 0): l(l), r(r) {
        sum = 0; add = 0;
        if(l == r) {
            sum = v[mp[l]];
            return ;
        }
        int m = (l + r) >> 1;
        lc = new (pool++) Tree(l, m);
        rc = new (pool++) Tree(m + 1, r);
        sum = lc->sum + rc->sum;
    }
    void down() {
        if(add) {
            lc->add += add;
            rc->add += add;
            lc->sum += 1LL * add * (lc->r - lc->l + 1);
            rc->sum += 1LL * add * (rc->r - rc->l + 1);
            add = 0;
        }
    }
    void insert(int L, int R, int v) {
        if(L > r ¦¦ R < l) return ;
        if(L <= l && r <= R) {
            add += v;
            sum += 1LL * (r - l + 1) * v;
            return ;
        }
        down();
        int m = (l + r) >> 1;
        lc->insert(L, R, v);
        rc->insert(L, R, v);
        sum = lc->sum + rc->sum;
    }
    long long query(int L, int R) {
        if(L > r ¦¦ R < l) return 0;
        if(L <= l && r <= R) {
            return sum;
        }
        down();
        int m = (l + r) >> 1;
        return lc->query(L, R) + rc->query(L, R);
    }
}node[1 << 18], *tree;
long long query(int u, int v) 
{
    long long ret = 0;
    while(first[u] != first[v]) {
        if(dep[first[u]] < dep[first[v]]) {
            swap(u, v);
        }
        ret += tree->query(id[first[u]], id[u]);
        u = pre[first[u]];
    }
    if(dep[u] > dep[v]) {
        swap(u, v);
    }
    ret += tree->query(id[u], id[v]);
    return ret;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++) {
        scanf("%d", &v[i]);
    }
    E = 0;
    memset(head, -1, sizeof(head));
    int a, b;
    for(int i = 1; i < n; i++) {
        scanf("%d%d", &a, &b);
        a--;b--;
        add_edge(a, b);
        add_edge(b, a);
    }
    cnt = 0, dfs(0), make_chain(0);
    pool = node;
    tree = new (pool++) Tree(1, n);
    while(m--) {
        int op;
        scanf("%d", &op);
        int x,a;
        if(op == 1) {
            scanf("%d%d", &x,&a);
            x--;
            tree->insert(id[x], id[x], a);
        } else if(op == 2) {
            scanf("%d%d", &x, &a);
            x--;
            tree->insert(L[x], R[x], a);
        } else {
            scanf("%d", &x);
            x--;
            long long ret = query(x, 0);
            printf("%lld\n" ,ret);
        }
    }
    return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注