题解:P11134 【MX-X5-T6】「GFOI Round 1」Abiogenesis

· · 题解

特殊图求最小生成树,不难想到 Boruvka 算法。该算法流程如下:

  1. 初始每个点作为一个单独的连通块。

  2. 对每个连通块找出连向其余连通块的边中,边权最小的一条边。

  3. 枚举每个连通块,把该连通块的最小边加进生成树中。

  4. 重复 2~3 操作直到连通块数不再变化,此时若连通块数为 1 则找到最小生成树;否则图不连通。

由于每次进行 2~3 操作之后,连通块数至少减半,所以至多反复 O(\log n) 轮。

考虑某一轮中,如何对每个连通块求出连出去的最小边。发现只需对于每个点求出其连出去的最小边。

于是问题转化为:n 个点中,每个点 u 有一个颜色(所在连通块) c_u。对于每个 u,求出 \min\limits_{c_u\neq c_v\land \max(l_u,l_v)\le \min(r_u,r_v)}a_u+a_v+|b_u-b_v|。

先按照 b_u 从小到大排序,不妨设 b_u\ge b_v。b_u<b_v 的情况再反过来做一遍即可。

然后边的权值变成了 (a_u+b_u)+(a_v-b_v)。

先考虑没有颜色限制怎么做。按照 b_u 从小到大枚举 u,查询 u 的前缀中区间与 (l_u,r_u) 有交的 a_v-b_v 的最小值。需要维护区间对 x 取 \min,区间求 \min。使用带懒标记的线段树或拆成若干个单点取 \min 即可。

再考虑加上颜色的限制,维护区间最小值 \min 和区间次小值 \min',并强制让 \min 和 \min' 的点的颜色不同。这样的话 u 要么是取 \min 要么是取 \min',判断 c_u 是否是 \min 的点的颜色即可。

时间复杂度 O(n\log^2 n)。常数较大。

// Problem: T497952 「GFOI Round 1」Abiogenesis
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/T497952
// Memory Limit: 512 MB
// Time Limit: 3500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define eb emplace_back
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
bool Mbe;

const int N = 1e5 + 5;
const int inf = 1e9;

int n, sz, fa[N];
int len, tp[N << 1];
ll res;

int gf(int x) { return x == fa[x] ? x : fa[x] = gf(fa[x]); }
void mrg(int x, int y, ll w) { if ((x = gf(x)) != (y = gf(y))) fa[x] = y, res += w, sz--; }

struct P {
    int l, r, a, b;
    P () { }
    P (int _l, int _r, int _a, int _b) :
        l(_l), r(_r), a(_a), b(_b) { }
} a[N];

struct T { 
    int x, y, cx, cy;
    T () { }
    T (int _x, int _y, int _cx, int _cy) :
        x(_x), y(_y), cx(_cx), cy(_cy) { }
};

inline T ad(T l, T r) {
    if (l.x > r.x) swap(l, r);
    if (l.y > r.x && l.cx != r.cx) l.y = r.x, l.cy = r.cx;
    else if (l.y > r.y && l.cx != r.cy) l.y = r.y, l.cy = r.cy;
    return l;
}

struct SEG {
    T t[N << 3], z[N << 3], res;
    #define ls x << 1
    #define rs x << 1 | 1
    #define m ((l + r) >> 1)
    void bld(int l, int r, int x) {
        z[x] = t[x] = T(inf, inf, 0, 0);
        if (l == r) return;
        bld(l, m, ls), bld(m + 1, r, rs);
    }
    void cov(int l, int r, int L, int R, int p, int c, int x) {
        if (R < L) return;
        t[x] = ad(t[x], T(p, inf, c, 0));
        if (L <= l && r <= R) return z[x] = ad(z[x], T(p, inf, c, 0)), void();
        if (L <= m) cov(l, m, L, R, p, c, ls);
        if (R > m) cov(m + 1, r, L, R, p, c, rs);
    }
    void qry(int l, int r, int L, int R, int x) {
        if (R < L) return;
        res = ad(res, z[x]);
        if (L <= l && r <= R) return res = ad(res, t[x]), void();
        if (L > m) return qry(m + 1, r, L, R, rs);
        if (R <= m) return qry(l, m, L, R, ls);
        qry(l, m, L, R, ls), qry(m + 1, r, L, R, rs);
    }
    void c(int l, int r, int p, int c) { cov(1, len, l, r, p, c, 1); }
    void q(int l, int r) { res = T(inf, inf, 0, 0), qry(1, len, l, r, 1); }
} S;

pi out[N];

inline void chk(pi &x, pi y) {
    x = min(x, y);
}

void bolovka() {
    for (int i = 1; i <= n; i++) fa[i] = i;
    while (1) {
        int tsz = sz;
        S.bld(1, len, 1);
        for (int i = 1; i <= n; i++) out[i] = mp(inf, 0);
        for (int i = 1; i <= n; i++) {
            int u = gf(i);
            S.q(a[i].l, a[i].r); T res = S.res;
            ll v1 = res.x + a[i].a + a[i].b, v2 = res.y + a[i].a + a[i].b;
            if (res.cx != u) chk(out[u], mp(v1, res.cx)), chk(out[res.cx], mp(v1, u));
            else if (res.cy != u) chk(out[u], mp(v2, res.cy)), chk(out[res.cy], mp(v2, u));
            S.c(a[i].l, a[i].r, a[i].a - a[i].b, gf(i));
        }
        S.bld(1, len, 1);
        for (int i = n; i; i--) {
            int u = gf(i);
            S.q(a[i].l, a[i].r); T res = S.res;
            ll v1 = res.x + a[i].a - a[i].b, v2 = res.y + a[i].a - a[i].b;
            if (res.cx != u) chk(out[u], mp(v1, res.cx)), chk(out[res.cx], mp(v1, u));
            else if (res.cy != u) chk(out[u], mp(v2, res.cy)), chk(out[res.cy], mp(v2, u));
            S.c(a[i].l, a[i].r, a[i].a + a[i].b, gf(i));
        }
        for (int i = 1; i <= n; i++) {
            if (gf(i) != i || !out[i].se) continue;
            mrg(i, out[i].se, out[i].fi);
        }
        if (tsz == sz) { res = -1; break; }
        if (sz == 1) break;
    }
}

void solve() {
    cin >> n, res = len = 0, sz = n;
    for (int i = 1, l, r, a, b; i <= n; i++)
        cin >> l >> r >> a >> b, ::a[i] = P(l, r, a, b), tp[++len] = l, tp[++len] = r;
    if (n == 1) return cout << 0 << '\n', void();
    sort(a + 1, a + n + 1, [](P &x, P &y) { return x.b < y.b; });
    sort(tp + 1, tp + len + 1), len = unique(tp + 1, tp + len + 1) - tp - 1;
    for (int i = 1; i <= n; i++) 
        a[i].l = lower_bound(tp + 1, tp + len + 1, a[i].l) - tp,
        a[i].r = lower_bound(tp + 1, tp + len + 1, a[i].r) - tp;
    bolovka();
    cout << res << '\n';
}

bool Med;
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cerr << (&Mbe - &Med) / 1048576.0 << " MB\n";
    #ifdef FILE
        freopen(".in", "r", stdin);
        freopen(".out", "w", stdout);
    #endif
    int T = 1;
    cin >> T;
    while (T--) solve();
    cerr << (int)(1e3 * clock() / CLOCKS_PER_SEC) << " ms\n";
    return 0;
}