题解:P14160 [ICPC 2022 Nanjing R] 工厂重现
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; typedef long long ll; typedef double db; typedef unsigned long long ull; typedef long double ldb; typedef pair<ll, ll> pii;
const int maxn = 100100;
ll n, m; vector<pii> G[maxn];
struct line { ll k, b; line(ll _k = 0, ll _b = 0) : k(_k), b(_b) {} } tag[maxn];
inline line operator + (const line &a, const line &b) { return line(a.k + b.k, a.b + b.b); }
int nt, ls[maxn], rs[maxn], sz[maxn], p[maxn]; ll val[maxn]; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int newnode(int k) { int u = ++nt; sz[u] = 1; p[u] = rnd(); val[u] = k; return u; }
inline void pushup(int x) { sz[x] = sz[ls[x]] + sz[rs[x]] + 1; }
inline void pushtag(int x, line y) { if (!x) { return; } val[x] += y.k * sz[ls[x]] + y.b; tag[x] = tag[x] + y; }
inline void pushdown(int x) { if (tag[x].k == 0 && tag[x].b == 0) { return; } pushtag(ls[x], tag[x]); pushtag(rs[x], line(tag[x].k, tag[x].b + (sz[ls[x]] + 1) * tag[x].k)); tag[x] = line(); }