CF1804G Flow Control

· · 题解

独立做出的 *3500。

首先扫描线,然后考虑加速模拟过程。

题面中的除 2 下取整是突破口。如果有一堆数,它们的极差 d \ge 2,那么进行一次除 2 操作后,d 至多变成 \left\lceil\frac{d}{2}\right\rceil。所以我们模拟 O(\log V) 次,就可以使所有数极差 \le 1。

模拟的过程中需要去重。如果两个数某个时刻相同了,那么它们之后的变化是一样的。所以直接把它们在并查集上并起来即可。

模拟到极差 \le 1 后,之后的变化会进入循环,那就不用继续模拟了,计算循环了多少遍就行了。

复杂度大概是 O(n \log n \log V)。

:::info[代码] 细节较多。

// Problem: G. Flow Control
// Contest: Codeforces - Nebius Welcome Round (Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/1804/G
// Memory Limit: 512 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

namespace IO {
    const int maxn = 1 << 20;

    char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;

    inline char gc() {
        return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
    }

    template<typename T = int>
    inline T read() {
        char c = gc();
        T x = 0;
        bool f = 0;
        while (c < '0' || c > '9') {
            f |= (c == '-');
            c = gc();
        }
        while (c >= '0' && c <= '9') {
            x = (x << 1) + (x << 3) + (c ^ 48);
            c = gc();
        }
        return f ? ~(x - 1) : x;
    }

    inline int reads(char *s) {
        char c = gc();
        int len = 0;
        while (isspace(c)) {
            c = gc();
        }
        while (!isspace(c) && c != -1) {
            s[len++] = c;
            c = gc();
        }
        return len;
    }

    inline string reads() {
        char c = gc();
        string s;
        while (isspace(c)) {
            c = gc();
        }
        while (!isspace(c) && c != -1) {
            s += c;
            c = gc();
        }
        return s;
    }

    inline void flush() {
        fwrite(obuf, 1, oS - obuf, stdout);
        oS = obuf;
    }

    struct Flusher {
        ~Flusher() {
            flush();
        }
    } AutoFlush;

    inline void pc(char ch) {
        if (oS == obuf + maxn) {
            flush();
        }
        *oS++ = ch;
    }

    template<typename T>
    inline void write(T x) {
        static char stk[64], *tp = stk;
        if (x < 0) {
            x = ~(x - 1);
            pc('-');
        }
        do {
            *tp++ = x % 10;
            x /= 10;
        } while (x);
        while (tp != stk) {
            pc((*--tp) | 48);
        }
    }

    inline void write(const char *s) {
        for (int i = 0; s[i]; ++i) {
            pc(s[i]);
        }
    }

    template<typename T>
    inline void writesp(T x) {
        write(x);
        pc(' ');
    }

    template<typename T>
    inline void writeln(T x) {
        write(x);
        pc('\n');
    }
}

using IO::read;
using IO::reads;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;

const int maxn = 400100;

ll n, m, et, val[maxn], b[maxn];

struct event {
    ll x, i;
    event(ll _x = 0, ll _i = 0) : x(_x), i(_i) {}
} a[maxn];

int fa[maxn], sz[maxn];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

inline void merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
        fa[x] = y;
        sz[y] += sz[x];
    }
}

void solve() {
    n = read();
    m = read();
    for (int i = 1; i <= n; ++i) {
        int l = read();
        int r = read();
        b[i] = read();
        a[++et] = event(l, i);
        a[++et] = event(r + 1, -i);
    }
    sort(a + 1, a + et + 1, [&](const event &a, const event &b) {
        return a.x < b.x;
    });
    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
        sz[i] = 1;
    }
    set<pll> S;
    ll sum = 0, tag = 0, ans = 0, cnt = 0;
    for (int i = 1, j = 1; i <= et; i = (++j)) {
        while (j < et && a[j + 1].x == a[i].x) {
            ++j;
        }
        if (j == et) {
            break;
        }
        for (int k = i; k <= j; ++k) {
            int p = a[k].i;
            if (p > 0) {
                auto it = S.lower_bound(pll(b[p] - tag, 0));
                if (it != S.end() && it->fst == b[p] - tag) {
                    merge(p, it->scd);
                } else {
                    S.emplace(b[p] - tag, p);
                }
                sum += b[p];
                val[p] = b[p] - tag;
                ++cnt;
            } else {
                p = -p;
                sum -= val[find(p)] + tag;
                --sz[find(p)];
                --cnt;
                if (!sz[find(p)]) {
                    S.erase(pll(val[find(p)], find(p)));
                }
            }
        }
        if (!cnt) {
            tag = 0;
            continue;
        }
        ll t = a[j + 1].x - a[j].x;
        int ct = 0;
        while (t) {
            if ((int)S.size() == 1 && sum <= m) {
                ll mn = min((m - sum) / cnt + 1, t);
                t -= mn;
                tag += mn;
                ans += sum * mn + mn * (mn - 1) / 2 * cnt;
                sum += mn * cnt;
                if (!t) {
                    break;
                }
                set<pll> T;
                pll lst(-1, -1);
                ll ns = 0;
                for (pll p : S) {
                    p.fst += tag;
                    ns += p.fst / 2 * sz[find(p.scd)];
                    if (lst.fst != -1 && lst.fst == p.fst / 2) {
                        merge(p.scd, lst.scd);
                    } else {
                        lst = pll(p.fst / 2, p.scd);
                        T.emplace(p.fst / 2, p.scd);
                        val[p.scd] = p.fst / 2;
                    }
                }
                S = move(T);
                sum = ns;
                tag = 0;
                --t;
                if (!t) {
                    break;
                }
                mn = min((m - sum) / cnt + 1, t);
                ll p = t / (mn + 1);
                t -= (mn + 1) * p;
                ans += (sum * mn + mn * (mn - 1) / 2 * cnt) * p;
                continue;
            }
            if (ct == 2) {
                ll mn = min((m - sum) / cnt + 1, t);
                ll p = t / (mn + 1);
                t -= (mn + 1) * p;
                ans += (sum * mn + mn * (mn - 1) / 2 * cnt) * p;
                ct = 0;
                continue;
            }
            if (sum > m) {
                set<pll> T;
                pll lst(-1, -1);
                ll ns = 0;
                for (pll p : S) {
                    p.fst += tag;
                    ns += p.fst / 2 * sz[find(p.scd)];
                    if (lst.fst != -1 && lst.fst == p.fst / 2) {
                        merge(p.scd, lst.scd);
                    } else {
                        lst = pll(p.fst / 2, p.scd);
                        T.emplace(p.fst / 2, p.scd);
                        val[p.scd] = p.fst / 2;
                    }
                }
                ct += ((int)S.size() == 2 && (int)T.size() == 2 && ns <= m && prev(T.end())->fst - T.begin()->fst == 1);
                S = move(T);
                sum = ns;
                tag = 0;
                --t;
            } else {
                ll mn = min((m - sum) / cnt + 1, t);
                t -= mn;
                tag += mn;
                ans += sum * mn + mn * (mn - 1) / 2 * cnt;
                sum += mn * cnt;
            }
        }
    }
    printf("%lld\n", ans);
}

int main() {
    int T = 1;
    // scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}

:::