树链剖分+线段树 spoj375 Query on a tree

传送门:点击打开链接

题意:边更新,路径查询边权最大值

思路:第一道树链剖分题,其实树链剖分就相当于把树的边给分类了一样,分成了重边和轻边。

然后,有一个性质,重链的条数和轻边的条数都不会超过log(n),所以总的复杂也只有log(n)

也就是说,本身要维护一个路径上的某个东西,如果直接用线段树做,并不是很好做,

树链剖分就相当于把重链和轻边分开求,因为重链在DFS序下编号是可以连在一起的,所以重链是整条都被另一种数据结构求出了答案,轻边是单独的被求出答案,然后两部分答案合并起来~

听起来很复杂,但是实际操作起来却非常的神奇,代码也并没有那么繁琐,但是却十分十分的巧妙~

#include<bits/stdc++.h>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int MX = 1e4 + 5;
const int INF = 0x3f3f3f3f;

struct Edge {
    int u, v, nxt, cost;
} E[MX << 2];
int Head[MX], h_r;
void edge_init() {
    h_r = 0;
    memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v, int cost) {
    E[h_r].u = u; E[h_r].v = v; E[h_r].cost = cost;
    E[h_r].nxt = Head[u];
    Head[u] = h_r++;
}

int MAX[MX << 2], A[MX];
void push_up(int rt) {
    MAX[rt] = max(MAX[rt << 1], MAX[rt << 1 | 1]);
}
void build(int l, int r, int rt) {
    if(l == r) {
        MAX[rt] = A[l];
        return;
    }
    int m = (l + r) >> 1;
    build(lson); build(rson);
    push_up(rt);
}
int query(int L, int R, int l, int r, int rt) {
    if(L <= l && r <= R) {
        return MAX[rt];
    }
    int m = (l + r) >> 1, ret = -INF;
    if(L <= m) ret = max(ret, query(L, R, lson));
    if(R > m) ret = max(ret, query(L, R, rson));
    return ret;
}
void update(int x, int d, int l, int r, int rt) {
    if(l == r) {
        MAX[rt] = d;
        return;
    }
    int m = (l + r) >> 1;
    if(x <= m) update(x, d, lson);
    else update(x, d, rson);
    push_up(rt);
}

int fa[MX], top[MX], siz[MX], son[MX], dep[MX], id[MX], rear;
void DFS1(int u, int f, int d) {
    fa[u] = f; dep[u] = d;
    son[u] = 0; siz[u] = 1;
    for(int i = Head[u]; ~i; i = E[i].nxt) {
        int v = E[i].v;
        if(v == f) continue;
        DFS1(v, u, d + 1);
        siz[u] += siz[v];
        if(siz[son[u]] < siz[v]) {
            son[u] = v;
        }
    }
}
void DFS2(int u, int tp) {
    top[u] = tp;
    id[u] = ++rear;
    if(son[u]) DFS2(son[u], tp);
    for(int i = Head[u]; ~i; i = E[i].nxt) {
        int v = E[i].v;
        if(v == fa[u] || v == son[u]) continue;
        DFS2(v, v);
    }
}
void HLD_presolve() {
    rear = 0;
    DFS1(1, 0, 1);
    DFS2(1, 1);
    for(int i = 0; i < 2 * (rear - 1); i += 2) {
        int u = E[i].u, v = E[i].v;
        if(dep[u] < dep[v]) swap(u, v);
        A[id[u]] = E[i].cost;
    }
    A[1] = -INF;
    build(1, rear, 1);
}
void HLD_update(int x, int d) {
    x = (x - 1) * 2;
    int u = E[x].u, v = E[x].v;
    if(dep[u] < dep[v]) swap(u, v);
    update(id[u], d, 1, rear, 1);
}
int HLD_query(int u, int v) {
    int tp1 = top[u], tp2 = top[v], ans = -INF;
    while(tp1 != tp2) {
        if(dep[tp1] < dep[tp2]) {
            swap(u, v);
            swap(tp1, tp2);
        }
        ans = max(ans, query(id[tp1], id[u], 1, rear, 1));
        u = fa[tp1]; tp1 = top[u];
    }
    if(u == v) return ans;
    if(dep[u] > dep[v]) swap(u, v);
    ans = max(ans, query(id[son[u]], id[v], 1, rear, 1));
    return ans;
}

int main() {
    int T, n; //FIN;
    scanf("%d", &T);
    while(T--) {
        edge_init();
        scanf("%d", &n);
        for(int i = 1; i <= n - 1; i++) {
            int u, v, cost;
            scanf("%d%d%d", &u, &v, &cost);
            edge_add(u, v, cost);
            edge_add(v, u, cost);
        }
        HLD_presolve();

        char op[10]; int a, b;
        while(scanf("%s", op), op[0] != 'D') {
            scanf("%d%d", &a, &b);
            if(op[0] == 'Q') printf("%d\n", HLD_query(a, b));
            else HLD_update(a, b);
        }
    }
    return 0;
}

你可能感兴趣的:(树链剖分+线段树 spoj375 Query on a tree)