hdu 3966(树链剖分)

题意:有一棵树,上面n个节点,每个节点都有一个权值,有三种操作,I a b c,把从节点a到节点b路径上所有点(包括a和b)权值都加c,D a b c,把从节点a到节点b路径上所有点权值都减c,Q a,问节点a的权值。
题解:树链剖分模板题,线段树维护区间和,用到单点查询,区间更新。

#include 
#include 
#include 
using namespace std;
const int N = 50005;
struct Edge {
    int u, v, nxt;
    Edge() {};
    Edge(int a, int b, int c): u(a), v(b), nxt(c) {}
}e[N << 1];
int val[N], top[N], size[N], fa[N], dep[N], son[N];
int n, m, q, head[N], cnt, tree[N << 2], flag[N << 2], id[N], tot;

void AddEdge(int u, int v) {
    e[cnt] = Edge(u, v, head[u]);
    head[u] = cnt++;
    e[cnt] = Edge(v, u, head[v]);
    head[v] = cnt++;
}

void pushup(int k) {
    tree[k] = tree[k * 2] + tree[k * 2 + 1];
}

void pushdown(int k, int left, int right) {
    if (flag[k] != 0) {
        int mid = (left + right) / 2;
        tree[k * 2] += flag[k] * (mid - left + 1);
        flag[k * 2] += flag[k];
        tree[k * 2 + 1] += flag[k] * (right - mid);
        flag[k * 2 + 1] += flag[k];
        flag[k] = 0;
    }
}

void build(int k, int left, int right) {
    tree[k] = flag[k] = 0;
    if (left != right) {
        int mid = (left + right) / 2;
        build(k * 2, left, mid);
        build(k * 2 + 1, mid + 1, right);
    }
}

int query(int k, int left, int right, int pos) {
    if (left == right)
        return tree[k];
    pushdown(k, left, right);
    int mid = (left + right) / 2;
    if (pos <= mid)
        return query(k * 2, left, mid, pos);
    return query(k * 2 + 1, mid + 1, right, pos);
}

void modify(int k, int left, int right, int l, int r, int v) {
    if (l <= left && right <= r) {
        tree[k] += (right - left + 1) * v;
        flag[k] += v;
        return;
    }
    pushdown(k, left, right);
    int mid = (left + right) / 2;
    if (l <= mid)
        modify(k * 2, left, mid, l, r, v);
    if (r > mid)
        modify(k * 2 + 1, mid + 1, right, l, r, v);
    pushup(k);
}

void dfs1(int u, int f, int depth) {
    size[u] = 1; fa[u] = f; dep[u] = depth; son[u] = 0;
    for (int i = head[u]; i + 1; i = e[i].nxt) {
        int v = e[i].v;
        if (v == f) continue;
        dfs1(v, u, depth + 1);
        size[u] += size[v];
        if (size[son[u]] < size[v]) son[u] = v;    
    }
}

void dfs2(int u, int tp) {
    id[u] = ++tot;
    top[u] = tp;
    if (son[u]) dfs2(son[u], tp);
    for (int i = head[u]; i + 1; i = e[i].nxt) {
        int v = e[i].v;
        if (v == fa[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

void init() {
    for (int i = 1; i <= n; i++)
        scanf("%d", &val[i]);
    memset(head, -1, sizeof(head));
    cnt = tot = 0;
    int u, v;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        AddEdge(u, v);
    }
    dfs1(1, 0, 1);
    dfs2(1, 1);
    build(1, 1, tot);
    for (int i = 1; i <= n; i++)
        modify(1, 1, tot, id[i], id[i], val[i]);
}

void solve(int u, int v, int a) {
    int tp1 = top[u], tp2 = top[v];
    int res = 0;
    while (top[u] != top[v]) {
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        modify(1, 1, tot, id[tp1], id[u], a);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (dep[u] > dep[v])
        swap(u, v);
    modify(1, 1, tot, id[u], id[v], a);
}

int main() {
    while (scanf("%d%d%d", &n, &m, &q) == 3) {
        init();
        char op[5];
        int a, b, c;
        while (q--) {
            scanf("%s", op);
            if (op[0] == 'Q') {
                scanf("%d", &a);
                printf("%d\n", query(1, 1, tot, id[a]));
            }
            else if (op[0] == 'I') {
                scanf("%d%d%d", &a, &b, &c);
                solve(a, b, c);
            }
            else {
                scanf("%d%d%d", &a, &b, &c);
                solve(a, b, -c);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(ACM-树链剖分)