Luo Valley P3178 [HAOI2015] explanations operation tree split tree chain segment tree +

Topic links: https://www.luogu.org/problem/P3178

This question is a split tree chain of title templates.
But colleagues solve this problem Road refresh my understanding of the two:

The first understanding is: not only can be split tree strand processing chain, and to process the sub-tree , because:
the node usubtree numbered points are all covered seg[u]to seg[u]+size[u]-1within this range!

The second understanding is: tree line delay operation of the delay not mark their mark, that is to say:
lazy[rt]not a mark delay value itself, but that rtin itself does not have much value passed to the delay rt<<1and rt<<1|1the.

This question is then a bare tree chain split title, applied to the sub-tree updates and delay operations.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
#define INF (1<<29)
const int maxn = 100010;
int fa[maxn],
    dep[maxn],
    size[maxn],
    son[maxn],
    top[maxn],
    seg[maxn], seg_cnt,
    rev[maxn],
    n, w[maxn];
long long sumv[maxn<<2], lazy[maxn<<2];
vector<int> g[maxn];
void dfs1(int u, int p) {
    size[u] = 1;
    for (vector<int>::iterator it = g[u].begin(); it != g[u].end(); it ++) {
        int v = (*it);
        if (v == p) continue;
        fa[v] = u;
        dep[v] = dep[u] + 1;
        dfs1(v, u);
        size[u] += size[v];
        if (size[v] >size[son[u]]) son[u] = v;
    }
}
void dfs2(int u, int tp) {
    seg[u] = ++seg_cnt;
    rev[seg_cnt] = u;
    top[u] = tp;
    if (son[u]) dfs2(son[u], tp);
    for (vector<int>::iterator it = g[u].begin(); it != g[u].end(); it ++) {
        int v = (*it);
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
void push_down(int rt, int len) {
    if (lazy[rt]) {
        int l_len=len-len/2, r_len = len/2;
        lazy[rt<<1] += lazy[rt];
        lazy[rt<<1|1] += lazy[rt];
        sumv[rt<<1] += lazy[rt] * l_len;
        sumv[rt<<1|1] += lazy[rt] * r_len;
        lazy[rt] = 0;
    }
}
void push_up(int rt) {
    sumv[rt] = sumv[rt<<1] + sumv[rt<<1|1];
}
void build(int l, int r, int rt) {
    int mid = (l + r) / 2;
    if (l == r) {
        sumv[rt] = w[rev[l]];
        return;
    }
    build(lson); build(rson);
    push_up(rt);
}
void update(int L, int R, long long v, int l, int r, int rt) {
    if (L <= l && r <= R) {
        sumv[rt] += (r-l+1) * v;
        lazy[rt] += v;
        return;
    }
    push_down(rt, r-l+1);
    int mid = (l + r) / 2;
    if (L <= mid) update(L, R, v, lson);
    if (R > mid) update(L, R, v, rson);
    push_up(rt);
}
long long query_sum(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) return sumv[rt];
    push_down(rt, r-l+1);
    int mid = (l + r) / 2;
    long long tmp = 0;
    if (L <= mid) tmp += query_sum(L, R, lson);
    if (R > mid) tmp += query_sum(L, R, rson);
    return tmp;
}
long long ask_sum(int u, int v) {
    long long res = 0;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap(u, v);
        res += query_sum(seg[top[u]], seg[u], 1, n, 1);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap(u, v);
    res += query_sum(seg[v], seg[u], 1, n, 1);
    return res;
}
int m, op, x, a;
string s;
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) cin >> w[i];
    for (int i = 1; i < n; i ++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dep[1] = fa[1] = 1;
    dfs1(1, -1);
    dfs2(1, 1);
    build(1, n, 1);
    while (m --) {
        cin >> op;
        if (op == 1) {
            cin >> x >> a;
            update(seg[x], seg[x], a, 1, n, 1);
        }
        else if (op == 2) {
            cin >> x >> a;
            update(seg[x], seg[x]+size[x]-1, a, 1, n, 1);
        }
        else {
            cin >> x;
            cout << ask_sum(1, x) << endl;
        }
    }
    return 0;
}

Author: zifeiy

Guess you like

Origin www.cnblogs.com/codedecision/p/11773779.html