template<typename T> inlineboolckmin(T& a, const T& b){ return (a > b)? a = b, true: false; }
int n, m; int C[MAXN], flow[MAXN], d[MAXN], p[MAXN]; // flow: u --> pre(u) 流量, d: u --> 匹配位置 距离
inlinevoidaugment(int u){ int lc = u << 1, rc = u << 1 | 1; d[u] = INF, p[u] = 0; if (C[u] > 0) d[u] = 0, p[u] = u; if (lc <= n && ckmin(d[u], d[lc] + ((flow[lc] > 0)? -1: 1))) p[u] = p[lc]; if (rc <= n && ckmin(d[u], d[rc] + ((flow[rc] > 0)? -1: 1))) p[u] = p[rc]; }
intmain(){ #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", C + i);
memset(d, 0x3f, sizeof d); for (int u = n; u; --u) augment(u);
int ans = 0; for (int k = 1; k <= m; ++k) { int s, t = 0, cost = INF, ds = 0; scanf("%d", &s); for (int u = s; u > 0; u /= 2) { if (ckmin(cost, ds + d[u])) t = u; ds += ((flow[u] < 0)? -1: 1); } ans += cost; for (int u = s; u != t; u /= 2) ++flow[u]; for (int u = p[t]; u != t; u /= 2) --flow[u]; --C[p[t]]; for (int u = p[t]; u != t; u /= 2) augment(u); for (int u = s; u > 0; u /= 2) augment(u); printf("%d%c", ans, " \n"[k == m]); } return0; }
HDU 6634「2019 Multi-University Training Contest」Salty fish
voiddfs(int u){ d[u] = 1, son[u] = -1; depth[u] = depth[pre[u]] + 1; for (int v, i = head[u]; ~i; i = edges[i].nxt) { dfs(v = edges[i].to); if (son[u] == -1 || d[son[u]] < d[v]) son[u] = v, d[u] = d[v] + 1; } Idx[u] = (~son[u])? Idx[son[u]]: ++idx; map<int, LL>& f = M[Idx[u]]; f[depth[u]] += A[u]; for (int v, i = head[u]; ~i; i = edges[i].nxt) { if ((v = edges[i].to) == son[u]) continue; for (IT ite = M[Idx[v]].begin(); ite != M[Idx[v]].end(); ++ite) f[ite->first] += ite->second; } for (size_t k = 0; k < P[u].size(); ++k) { constint& i = P[u][k]; IT ite = f.upper_bound(depth[u] + K[i]); if (ite == f.begin()) continue; --ite; while (C[i] > 0) { LL w = min((LL) C[i], ite->second); ite->second -= w, C[i] -= w, ans -= w; if (ite->second > 0) continue; if (ite == f.begin()) { f.erase(ite); break; } int dep = ite->first; --ite, f.erase(dep); } } } }
intmain(){ #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif int Ti; scanf("%d", &Ti); while (Ti--) { for (int i = 1; i <= n; ++i) P[i].clear(); for (int i = 1; i <= idx; ++i) M[i].clear(); ans = idx = 0, Graph::init();
scanf("%d%d", &n, &m); for (int i = 2; i <= n; ++i) scanf("%d", pre + i), Graph::AddEdge(pre[i], i); for (int i = 1; i <= n; ++i) scanf("%d", A + i), ans += A[i]; for (int u, i = 1; i <= m; ++i) scanf("%d%d%d", &u, K + i, C + i), P[u].push_back(i);
inlineintGc(){ return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> inlinevoidread(T& x){ x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
typedeflonglong LL; constint MAXN = 1e5 + 5;
int n, m, q; int A[MAXN], X[MAXN], S[MAXN], C[MAXN], Q[MAXN];
LL Ans[MAXN]; int cnt[MAXN];
structItem { int idx, v; Item(int _i, int _v): idx(_i), v(_v) { } booloperator < (const Item& rhs) const { return v < rhs.v; } }; priority_queue<Item> PQ;
namespace DSU { int fa[MAXN];
inlinevoidinit(int _n){ for (int i = 0; i <= _n; ++i) fa[i] = i; } intfindfa(int u){ return (fa[u] == u)? u: fa[u] = findfa(fa[u]); } }
intmain(){ #ifndef ONLINE_JUDGE freopen("data/ex_vegetables3.in", "r", stdin); #endif read(n), read(m), read(q); for (int i = 1; i <= n; ++i) read(A[i]), read(S[i]), read(C[i]), read(X[i]); int Mx = 0; for (int i = 1; i <= q; ++i) read(Q[i]), Mx = max(Mx, Q[i]);
DSU::init(Mx); for (int i = 1; i <= n; ++i) PQ.push(Item(i, A[i] + S[i]));
LL ans = 0; for (int p = 1; p <= Mx; ++p) { int flow = m; while (flow > 0 && !PQ.empty()) { int i = PQ.top().idx, v = PQ.top().v; PQ.pop(); int k = (X[i] == 0)? Mx: min(Mx, (C[i] + X[i] - 1) / X[i]); k = DSU::findfa(k); if (k == 0) continue; ++cnt[k]; if (cnt[k] == m) DSU::fa[k] = DSU::findfa(k - 1); --flow, --C[i], ans += v; if (C[i] > 0) PQ.push(Item(i, A[i])); } Ans[p] = ans; }
for (int i = 1; i <= q; ++i) printf("%lld\n", Ans[Q[i]]); return0; }
intmain(){ #ifndef ONLINE_JUDGE freopen("data/ex_sequence3.in", "r", stdin); #endif int Ti; read(Ti); while (Ti--) { read(n), read(K), read(L); for (int i = 1; i <= n; ++i) read(A[i]); for (int i = 1; i <= n; ++i) read(B[i]);
inlineintGc(){ return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> inlinevoidread(T& x){ x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
typedeflonglong LL; typedef pair<LL, LL> Pll;
constint MAXN = 1e5 + 5; const LL INFLL = 1e18;
structPoint { int x, w, c; Point() = default; Point(int _x, int _w, int _c): x(_x), w(_w), c(_c) { } booloperator < (const Point& rhs) const { return x < rhs.x; } } P[MAXN << 1];
int n, m, p; priority_queue<Pll, vector<Pll>, greater<Pll> > d0, d1;
signedmain(){ #ifndef ONLINE_JUDGE freopen("data/ex_hole7.in", "r", stdin); #endif read(n), read(m); for (int x, i = 1; i <= n; ++i) read(x), P[++p] = Point(x, -1, -1); LL sc = 0; for (int x, w, c, i = 1; i <= m; ++i) { read(x), read(w), read(c); P[++p] = Point(x, w, c), sc += c; } if (sc < n) returnputs("-1"), 0;
sort(P + 1, P + 1 + p); LL ans = 0; d1.push(Pll(INFLL, n)); for (int i = 1; i <= p; ++i) { int x = P[i].x, w = P[i].w; if (w == -1) { Pll t = d1.top(); d1.pop(); ans += t.first + x, --t.second; if (t.second > 0) d1.push(t); d0.push(Pll(-2LL * x - t.first, 1)); } else { LL tot = 0, c = P[i].c; while (c > 0 && !d0.empty()) { Pll t = d0.top(); if (t.first + x + w >= 0) break; d0.pop(); LL s = min((LL) c, t.second); ans += s * (t.first + w + x), t.second -= s, c -= s; if (t.second > 0) d0.push(t); tot += s, d1.push(Pll(-2LL * x - t.first, s)); } if (tot > 0) d0.push(Pll(-x - w, tot)); if (c > 0) d1.push(Pll(-x + w, c)); } }