写点啥好呢。?

· · 题解

可爱 lct 练手题。

不难发现,温度最低的尽量高,是一棵支持动态加边的最大生成树,用 lct 解决。

假设我们要连接 (u, v),具体步骤为:

一个常用的 trick,lct 不方便维护边权,可以在两个点之间建一个虚点转成点权。

:::success[code]

#include <bits/stdc++.h>
// #define fastio
#define int long long
#define bw(x) (1 << (x))
#define eb emplace_back
#define pii pair<int, int>
#define inf (0x3f3f3f3f3f3f3f3f)
#define F(x, v) for (auto x : (v))
#define ALL(x) (x).begin(), (x).end()
#define L(i, a, b) for (register int i = (a); i <= (b); ++i)
#define R(i, a, b) for (register int i = (a); i >= (b); --i)
#define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define debug(a) cerr << "\033[32m[DEBUG] " << #a << " = " << (a) << " at line " << __LINE__ << "\033[0m\n"
using namespace std; bool bgmem;
#ifdef fastio
struct IO {
    #define ion bw(20)
    char i[ion], o[ion], *icl = i, *icr = i, *oc = o;
    char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; }
    void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; }
    void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } 
    void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } 
    IO& operator>>(char &c) { return c = gc(), *this; }
    IO& operator<<(char c) { return pc(c), *this; }
    IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; }
    IO& operator>>(auto &x) { return rd(x), *this; }
    IO& operator<<(auto x) { return pr(x), *this; }
} io;
#define cin io
#define cout io
#endif
template < const int P > class modint { public:
    int x;
    modint(int _ = 0) { x = (_ % P + P) % P; }
    explicit operator int() { return x; }
    friend modint operator+(modint x, modint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; }
    friend modint operator-(modint x, modint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; }
    friend modint operator*(modint x, modint y) { return modint((__int128)(x.x * y.x)); }
    modint& operator+=(modint x) { return (*this) = (*this) + x; }
    modint& operator-=(modint x) { return (*this) = (*this) - x; }
    modint& operator*=(modint x) { return (*this) = (*this) * x; }
};
inline int cmax(int& x, int c) { return x = max(x, c); }
inline int cmin(int& x, int c) { return x = min(x, c); }
int tes = 1, cas;
namespace qwq {
    const int N = 4e5 + 5;
    int n, m, op, x, y, a[N], pos[N]; pii e[N];
    namespace lct {
        int son[N][2], siz[N], fa[N], vlen[N], vtem[N], slen[N], stem[N], sid[N], tag[N], tot = 0;
        #define dir(u) (son[fa[u]][1] == u)
        #define irt(u) (fa[u] == 0 || (son[fa[u]][0] != u && son[fa[u]][1] != u))
        void modify(int u) { swap(son[u][0], son[u][1]), tag[u] ^= 1; }
        void up(int u) { 
            siz[u] = siz[son[u][0]] + siz[son[u][1]] + 1; 
            slen[u] = slen[son[u][0]] + slen[son[u][1]] + vlen[u];
            stem[u] = vtem[u], sid[u] = u;
            if (son[u][0] && stem[u] > stem[son[u][0]]) stem[u] = stem[son[u][0]], sid[u] = sid[son[u][0]];
            if (son[u][1] && stem[u] > stem[son[u][1]]) stem[u] = stem[son[u][1]], sid[u] = sid[son[u][1]];
        }
        void down(int u) { if (tag[u]) modify(son[u][0]), modify(son[u][1]), tag[u] = 0; }
        void all(int u) { if (!irt(u)) all(fa[u]); down(u); }
        void rotate(int u) {
            int v = fa[u], w = fa[v], d = dir(u);
            if (!irt(v)) son[w][dir(v)] = u;
            fa[u] = w, son[v][d] = son[u][d ^ 1];
            if (son[u][d ^ 1]) fa[son[u][d ^ 1]] = v;
            son[u][d ^ 1] = v, fa[v] = u, up(v), up(u);
        } void splay(int u) {
            all(u);
            for (int v = fa[u]; !irt(u); rotate(u), v = fa[u]) if (!irt(v))
                rotate(dir(u) == dir(v) ? v : u);
        } 
        void access(int u) { for (int v = 0; u; v = u, u = fa[u]) splay(u), son[u][1] = v, up(u); } 
        void mkrt(int u) { access(u), splay(u), modify(u); } 
        int frt(int u) { 
            access(u), splay(u); 
            while (son[u][0]) u = son[u][0], down(u);
            splay(u);
            return u; 
        } 
        void split(int u, int v) { mkrt(u), access(v), splay(v); } 
        void link(int u, int v) { mkrt(u); if (frt(v) != u) fa[u] = v; }
        void cut(int u, int v) { 
            split(u, v);
            if (son[v][0] == u && !son[u][1]) son[v][0] = fa[u] = 0, up(v); 
        }
        pii ask(int u, int v) { split(u, v); return make_pair(stem[v], sid[v]); }
        int length(int u, int v) { split(u, v); return slen[v]; }
    }
    void init() {}
    void clear() {}
    void solve() {
        cin >> n >> m;
        L(i, 1, n) lct::vtem[i] = lct::stem[i] = inf;
        lct::tot = n;
        L(_, 1, m) {
            string op;
            cin >> op;
            if (op == "find") {
                int id, u, v, t, l;
                cin >> id >> u >> v >> t >> l;
                u++, v++;
                pos[id] = ++lct::tot, e[lct::tot] = {u, v};
                lct::vtem[lct::tot] = lct::stem[lct::tot] = t;
                lct::vlen[lct::tot] = lct::slen[lct::tot] = l;
                lct::sid[lct::tot] = lct::tot;
                if (lct::frt(u) != lct::frt(v)) lct::link(u, lct::tot), lct::link(lct::tot, v);
                else {
                    auto [w, d] = lct::ask(u, v);
                    if (w > t) continue;
                    lct::cut(e[d].first, d), lct::cut(d, e[d].second);
                    lct::link(u, lct::tot), lct::link(lct::tot, v);
                }
            } else if (op == "move") {
                int u, v;
                cin >> u >> v;
                u++, v++;
                if (lct::frt(u) != lct::frt(v)) {
                    cout << "-1\n";
                } else {
                    cout << lct::length(u, v) << "\n";
                }
            } else {
                int id, l;
                cin >> id >> l;
                lct::mkrt(pos[id]);
                lct::vlen[pos[id]] = l;
                lct::up(pos[id]);
            }
        }
    }
} bool edmem; signed main() {
    // FRE("", "");
#ifndef fastio
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#endif
    // cin >> tes;
    qwq::init(); while (++cas <= tes) qwq::clear(), qwq::solve();
#ifdef fastio
    fwrite(io.o, 1, io.oc - io.o, stdout);
#endif
    cerr << "time  : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n";
    cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n";
    return 0;
}
// I will never love You Know Who again. =(

:::