题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie's garden has $N$ plants labeled $1$ through $N$ ($2\leq N\leq 5\cdot 10^5$) from left to right. Bessie knows that plant $i$ requires at least $w_i$ ($0\leq w_i \leq 10^6$) units of water.
Bessie has a very peculiar irrigation system with $N-1$ canals, numbered $1$ through $N-1$. Each canal $i$ has an associated unit cost $c_i$ ($1\le c_i\le 10^6$), such that Bessie can pay $c_i k$ to provide plants $i$ and $i+1$ each with $k$ units of water, where $k$ is a non-negative integer.
Bessie is busy and may not have time to use all the canals. For each $2\leq i \leq N$ compute the minimum cost required to water plants $1$ through $i$ using only the first $i-1$ canals.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains a single positive integer $N$.The second line contains $N$ space-separated integers $w_1, \ldots, w_N$.
The third line contains $N-1$ space-separated integers $c_1, \ldots, c_{N-1}$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $N-1$ newline-separated integers. The $(i-1)$th integer should contain the minimum cost to water the first $i$ plants using the first $i-1$ canals.SAMPLE INPUT:
3 39 69 33 30 29
SAMPLE OUTPUT:
2070 2127
The minimum cost to water the first $2$ plants using the first canal is to pay $30 \cdot 69 = 2070$ by using the first canal $69$ times.
The minimum cost to water the first $3$ plants is to use the first canal $39$ times and the second canal $33$ times, paying $39 \cdot 30 + 29 \cdot 33 = 2127$.
SAMPLE INPUT:
3 33 82 36 19 1
SAMPLE OUTPUT:
1558 676
SAMPLE INPUT:
8 35 89 44 1 35 3 62 50 7 86 94 62 63 9 49
SAMPLE OUTPUT:
623 4099 4114 6269 6272 6827 8827
SCORING:
- Input 4: $N \leq 200$, and all $w_i \leq 200$.
- Inputs 5-6: All $w_i \leq 200$.
- Inputs 7-10: $N \leq 5000$.
- Inputs 11-14: All $w_i$ and $c_i$ are generated independently and uniformly at random.
- Inputs 15-19: No additional constraints.
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 24.6k平均输出 token(含思考) ↓
- 23.8输出速度 tokens/s ↑
- 976 秒延迟 ↓
- 1.59×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
Kimi K3.1 Pro(high)Kimi high
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 24.6k平均输出 token(含思考) ↓
- 23.8输出速度 tokens/s ↑
- 976 秒延迟 ↓
- 1.59×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 47.8k平均输出 token(含思考) ↓
- 171.4输出速度 tokens/s ↑
- 257 秒延迟 ↓
- 2.14×CPU 时间 / 官方解 ↓
- 不确定 1、同阶 2时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
2/3
解出的采样 ↑
- 0.0采样 1 · 编译错误
- 100采样 2 · 解出
- 100采样 3 · 解出
- 46.9k平均输出 token(含思考) ↓
- 117.9输出速度 tokens/s ↑
- 378 秒延迟 ↓
- 0.73×CPU 时间 / 官方解 ↓
- 同阶 2时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { Node *l, *r; ll len, sz, sum; int slope, mn, mx; unsigned pri; int lazyA; ll lazyB; bool rev; }; static Node* pool; static int poolPtr = 0; static unsigned rngState = 123456789u; static inline unsigned rnd() { rngState ^= rngState << 13; rngState ^= rngState >> 17; rngState ^= rngState << 5; return rngState; } static inline ll sz(Node* t) { return t ? t->sz : 0; } static Node* makeNode(ll len, int slope) { if (len <= 0) return nullptr; Node* t = &pool[poolPtr++]; t->l = t->r = nullptr; t->len = t->sz = len; t->slope = t->mn = t->mx = slope; t->sum = (ll)slope * len; t->pri = rnd(); t->lazyA = 1; t->lazyB = 0; t->rev = false; return t; } static inline void pull(Node* t) { t->sz = t->len + sz(t->l) + sz(t->r); t->sum = (ll)t->slope * t->len; t->mn = t->mx = t->slope; if (t->l) { t->sum += t->l->sum; t->mn = min(t->mn, t->l->mn); t->mx = max(t->mx, t->l->mx); } if (t->r) { t->sum += t->r->sum; t->mn = min(t->mn, t->r->mn); t->mx = max(t->mx, t->r->mx); } } // Replaces every slope s in the subtree by a*s+b. static void apply(Node* t, int a, ll b) { if (!t) return; t->slope = (int)(a * (ll)t->slope + b); t->sum = a * t->sum + b * t->sz; if (a == 1) { t->mn = (int)(t->mn + b); t->mx = (int)(t->mx + b); } else { int oldMn = t->mn, oldMx = t->mx; t->mn = (int)(-oldMx + b); t->mx = (int)(-oldMn + b); } t->lazyA *= a; t->lazyB = a * t->lazyB + b; } static inline void push(Node* t) { if (!t) return; if (t->rev) { swap(t->l, t->r); if (t->l) t->l->rev ^= 1; if (t->r) t->r->rev ^= 1; t->rev = false; } if (t->lazyA != 1 || t->lazyB != 0) { apply(t->l, t->lazyA, t->lazyB); apply(t->r, t->lazyA, t->lazyB); t->lazyA = 1; t->lazyB = 0; } } static Node* merge(Node* a, Node* b) { if (!a) return b; if (!b) return a; if (a->pri > b->pri) { push(a); a->r = merge(a->r, b); pull(a); return a; } else { push(b); b->l = merge(a, b->l); pull(b); return b; } } // Splits into the first k units of length and the remainder. static void split(Node* t, ll k, Node*& a, Node*& b) { if (!t) { a = b = nullptr; return; } push(t); ll leftSize = sz(t->l); if (k < leftSize) { split(t->l, k, a, t->l); b = t; pull(b); return; } if (k > leftSize + t->len) { split(t->r, k - leftSize - t->len, t->r, b); a = t; pull(a); return; } ll take = k - leftSize; if (take == 0) { a = t->l; t->l = nullptr; b = t; pull(b); } else if (take == t->len) { b = t->r; t->r = nullptr; a = t; pull(a); } else { Node* rightPart = makeNode(t->len - take, t->slope); rightPart->r = t->r; pull(rightPart); t->len = take; t->r = nullptr; a = t; b = rightPart; pull(a); } } // Returns the length before the first segment whose slope is at least c. static ll firstGE(Node* t, ll c) { if (!t || t->mx < c) return sz(t); push(t); if (t->l && t->l->mx >= c) return firstGE(t->l, c); ll leftSize = sz(t->l); if (t->slope >= c) return leftSize; return leftSize + t->len + firstGE(t->r, c); } static ll prefixSum(Node* t, ll k) { if (!t || k <= 0) return 0; push(t); ll leftSize = sz(t->l); if (k <= leftSize) return prefixSum(t->l, k); ll result = t->l ? t->l->sum : 0; k -= leftSize; if (k <= t->len) return result + (ll)t->slope * k; result += (ll)t->slope * t->len; return result + prefixSum(t->r, k - t->len); } static inline ll evaluate(Node* t, ll base, int lastSlope, ll length, ll d) { if (d >= length) { return base + (t ? t->sum : 0) + (ll)lastSlope * (d - length); } return base + prefixSum(t, d); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; pool = new Node[2 * N + 5]; // V_2(d) = c_1 * max(w_1,d). Its derivative is represented by T. ll base = c[1] * w[1]; ll length = w[1]; int lastSlope = (int)c[1]; Node* T = makeNode(length, 0); string output; output.reserve((size_t)N * 12); output += to_string(evaluate(T, base, lastSlope, length, w[2])); output.push_back('\n'); for (int j = 2; j < N; ++j) { ll W = w[j]; ll cost = c[j]; // Restrict the derivative to [0,W], extending with its final slope if needed. if (W < length) { Node *P, *discarded; split(T, W, P, discarded); T = P; } else if (W > length) { T = merge(T, makeNode(W - length, lastSlope)); } // Remove the suffix with slopes >= cost. It becomes a flat prefix. ll pos = firstGE(T, cost); Node *A, *X; if (pos == 0) { A = nullptr; X = T; } else if (pos == W) { A = T; X = nullptr; } else { split(T, pos, A, X); } ll flatLength = sz(X); base += (A ? A->sum : 0) + cost * flatLength; // Reverse the remaining segments and replace every slope s by cost-s. if (A) { apply(A, -1, cost); A->rev ^= 1; } T = merge(makeNode(flatLength, 0), A); length = W; lastSlope = (int)cost; output += to_string(evaluate(T, base, lastSlope, length, w[j + 1])); output.push_back('\n'); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { int l = 0, r = 0; unsigned priority = 0; ll len = 0, slope = 0; ll sumLen = 0, sumArea = 0; ll minSlope = 0, maxSlope = 0; int lazyA = 1; ll lazyB = 0; bool rev = false; }; struct FunctionTreap { vector<Node> tr; mt19937 rng{712367821}; FunctionTreap(int n) { tr.reserve(2 * n + 10); tr.emplace_back(); } int newNode(ll len, ll slope) { Node x; x.len = x.sumLen = len; x.slope = x.minSlope = x.maxSlope = slope; x.sumArea = len * slope; x.priority = rng(); tr.push_back(x); return (int)tr.size() - 1; } ll getLen(int t) const { return t ? tr[t].sumLen : 0; } ll getArea(int t) const { return t ? tr[t].sumArea : 0; } void pull(int t) { if (!t) return; Node &x = tr[t]; x.sumLen = x.len + getLen(x.l) + getLen(x.r); x.sumArea = x.slope * x.len + getArea(x.l) + getArea(x.r); x.minSlope = x.maxSlope = x.slope; if (x.l) { x.minSlope = min(x.minSlope, tr[x.l].minSlope); x.maxSlope = max(x.maxSlope, tr[x.l].maxSlope); } if (x.r) { x.minSlope = min(x.minSlope, tr[x.r].minSlope); x.maxSlope = max(x.maxSlope, tr[x.r].maxSlope); } } void applyReverse(int t) { if (!t) return; swap(tr[t].l, tr[t].r); tr[t].rev = !tr[t].rev; } // Replace every slope s by a*s+b. void applyAffine(int t, int a, ll b) { if (!t) return; Node &x = tr[t]; x.slope = a * x.slope + b; x.sumArea = a * x.sumArea + b * x.sumLen; if (a == 1) { x.minSlope += b; x.maxSlope += b; } else { ll oldMin = x.minSlope, oldMax = x.maxSlope; x.minSlope = b - oldMax; x.maxSlope = b - oldMin; } x.lazyB = a * x.lazyB + b; x.lazyA = a * x.lazyA; } void push(int t) { if (!t) return; if (tr[t].rev) { applyReverse(tr[t].l); applyReverse(tr[t].r); tr[t].rev = false; } if (tr[t].lazyA != 1 || tr[t].lazyB != 0) { int a = tr[t].lazyA; ll b = tr[t].lazyB; applyAffine(tr[t].l, a, b); applyAffine(tr[t].r, a, b); tr[t].lazyA = 1; tr[t].lazyB = 0; } } int merge(int a, int b) { if (!a) return b; if (!b) return a; if (tr[a].priority > tr[b].priority) { push(a); tr[a].r = merge(tr[a].r, b); pull(a); return a; } else { push(b); tr[b].l = merge(a, tr[b].l); pull(b); return b; } } // Split so that the first returned tree has total length k. pair<int, int> splitLen(int t, ll k) { if (!t) return {0, 0}; if (k <= 0) return {0, t}; if (k >= tr[t].sumLen) return {t, 0}; push(t); ll leftLen = getLen(tr[t].l); if (k <= leftLen) { auto p = splitLen(tr[t].l, k); tr[t].l = p.second; pull(t); return {p.first, t}; } ll rem = k - leftLen; ll nodeLen = tr[t].len; if (rem < nodeLen) { ll slope = tr[t].slope; int b = newNode(slope, nodeLen - rem); tr[b].r = tr[t].r; pull(b); tr[t].len = rem; tr[t].r = 0; pull(t); return {t, b}; } if (rem == nodeLen) { int b = tr[t].r; tr[t].r = 0; pull(t); return {t, b}; } auto p = splitLen(tr[t].r, rem - nodeLen); tr[t].r = p.first; pull(t); return {t, p.second}; } // Area under the finite segments over their first k units of length. ll areaPrefix(int t, ll k) { if (!t || k <= 0) return 0; if (k >= tr[t].sumLen) return tr[t].sumArea; push(t); int l = tr[t].l; ll leftLen = getLen(l); if (k <= leftLen) return areaPrefix(l, k); ll result = getArea(l); k -= leftLen; if (k <= tr[t].len) { result += tr[t].slope * k; return result; } result += tr[t].slope * tr[t].len; k -= tr[t].len; result += areaPrefix(tr[t].r, k); return result; } // Length before the first segment whose slope is at least c. ll lowerSlope(int t, ll c, bool &found) { push(t); int l = tr[t].l, r = tr[t].r; if (l && tr[l].maxSlope >= c) { return lowerSlope(l, c, found); } if (tr[t].slope >= c) { found = true; return getLen(l); } if (r && tr[r].maxSlope >= c) { ll base = getLen(l) + tr[t].len; return base + lowerSlope(r, c, found); } found = false; return 0; } }; struct Solver { int root = 0; ll minimum = 0; ll flatEnd = 0; ll finalSlope = 0; FunctionTreap treap; Solver(int n, ll w1, ll c1) : treap(n) { minimum = w1 * c1; flatEnd = w1; finalSlope = c1; } ll evaluate(ll x) { if (x <= flatEnd) return minimum; ll d = x - flatEnd; ll totalLen = treap.getLen(root); if (d >= totalLen) { return minimum + treap.getArea(root) + finalSlope * (d - totalLen); } return minimum + treap.areaPrefix(root, d); } void update(ll w, ll c, ll valueAtW) { ll oldFlatEnd = flatEnd; ll oldFinalSlope = finalSlope; ll crossing = 0; ll newFlatEnd = 0; ll upper = w; if (c <= oldFinalSlope) { bool found = false; ll offset = treap.lowerSlope(root, c, found); if (!found) offset = treap.getLen(root); crossing = oldFlatEnd + offset; if (w > crossing) newFlatEnd = w - crossing; upper = min(w, crossing); } ll baseValue; if (newFlatEnd > 0) baseValue = evaluate(crossing); else baseValue = valueAtW; ll newMinimum = c * newFlatEnd + baseValue; ll needed = upper - oldFlatEnd; if (needed <= 0) { root = 0; } else { ll totalLen = treap.getLen(root); if (needed < totalLen) { root = treap.splitLen(root, needed).first; } else if (needed > totalLen) { int extra = treap.newNode(needed - totalLen, oldFinalSlope); root = treap.merge(root, extra); } treap.applyReverse(root); treap.applyAffine(root, -1, c); } minimum = newMinimum; flatEnd = newFlatEnd; finalSlope = c; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; Solver solver(N, w[1], c[1]); string output; output.reserve((size_t)N * 12); for (int i = 2; i <= N; ++i) { ll answer = solver.evaluate(w[i]); output += to_string(answer); output.push_back('\n'); if (i < N) solver.update(w[i], c[i], answer); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; static const int64 INF_LEN = (1LL << 60); struct Line { int64 s, b; }; // Maps (s,b) to (a*s+k, b+e*s+f). struct Transform { int64 a, k, e, f; }; const Transform IDENTITY{1, 0, 0, 0}; bool isIdentity(const Transform &t) { return t.a == 1 && t.k == 0 && t.e == 0 && t.f == 0; } // Returns p after q. Transform compose(const Transform &p, const Transform &q) { return { p.a * q.a, p.a * q.k + p.k, q.e + p.e * q.a, q.f + p.e * q.k + p.f }; } struct Node { Node *l = nullptr, *r = nullptr; uint32_t priority; int64 len, sum; Line line; Transform tr = IDENTITY; bool rev = false; Node(int64 length, Line ln, uint32_t p) : priority(p), len(length), sum(length), line(ln) {} }; mt19937 rng(712367821); int64 lengthSum(Node *t) { return t ? t->sum : 0; } void pull(Node *t) { i128 total = (i128)lengthSum(t->l) + t->len + lengthSum(t->r); t->sum = total > INF_LEN ? INF_LEN : (int64)total; } Line getLine(const Node *t) { const Transform &p = t->tr; int64 s = (int64)((i128)p.a * t->line.s + p.k); int64 b = (int64)((i128)t->line.b + (i128)p.e * t->line.s + p.f); return {s, b}; } int64 evaluate(const Node *t, int64 x) { Line ln = getLine(t); return (int64)((i128)ln.s * x + ln.b); } void applyTransform(Node *t, const Transform &p) { if (t) t->tr = compose(p, t->tr); } void applyReverse(Node *t) { if (!t) return; swap(t->l, t->r); t->rev ^= true; } void push(Node *t) { if (!t) return; if (t->rev) { applyReverse(t->l); applyReverse(t->r); t->rev = false; } if (!isIdentity(t->tr)) { Transform p = t->tr; Line cur = getLine(t); applyTransform(t->l, p); applyTransform(t->r, p); t->line = cur; t->tr = IDENTITY; } } Node* mergeTrees(Node *a, Node *b) { if (!a) return b; if (!b) return a; if (a->priority < b->priority) { push(a); a->r = mergeTrees(a->r, b); pull(a); return a; } else { push(b); b->l = mergeTrees(a, b->l); pull(b); return b; } } // Keeps exactly the first d units of length and discards the remainder. Node* cutPrefix(Node *t, int64 d) { push(t); int64 left = lengthSum(t->l); if (d < left) return cutPrefix(t->l, d); if (d == left) return t->l; int64 rem = d - left; if (rem <= t->len) { t->len = rem; t->r = nullptr; pull(t); return t; } t->r = cutPrefix(t->r, rem - t->len); pull(t); return t; } Node* firstNode(Node *t) { while (true) { push(t); if (!t->l) return t; t = t->l; } } Node* lastNode(Node *t) { while (true) { push(t); if (!t->r) return t; t = t->r; } } Node* popFirst(Node *t) { push(t); if (!t->l) return t->r; t->l = popFirst(t->l); pull(t); return t; } // Finds the segment containing the point at distance d from the branch start. Node* locate(Node *t, int64 d) { push(t); int64 left = lengthSum(t->l); if (d <= left) return locate(t->l, d); int64 rem = d - left; if (rem <= t->len) return t; return locate(t->r, rem - t->len); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int64> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; Node *root = new Node(INF_LEN, {c[1], 0}, rng()); int64 minimizer = w[1]; int64 minimumValue = c[1] * w[1]; for (int i = 2; i < N; ++i) { int64 answer; int64 d = w[i] - minimizer; if (d <= 0) { answer = minimumValue; root = new Node(INF_LEN, {c[i], minimumValue}, rng()); minimizer = 0; } else { root = cutPrefix(root, d); answer = evaluate(lastNode(root), w[i]); applyReverse(root); applyTransform(root, Transform{-1, c[i], w[i], 0}); root = mergeTrees( root, new Node(INF_LEN, {c[i], minimumValue}, rng()) ); int64 newMinimizer = 0; while (getLine(firstNode(root)).s < 0) { Node *f = firstNode(root); newMinimizer += f->len; root = popFirst(root); } minimizer = newMinimizer; minimumValue = evaluate(firstNode(root), minimizer); } cout << answer << '\n'; } int64 answer; if (w[N] <= minimizer) { answer = minimumValue; } else { answer = evaluate(locate(root, w[N] - minimizer), w[N]); } cout << answer << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { Node *l, *r; ll len, sz, sum; int slope, mn, mx; unsigned pri; int lazyA; ll lazyB; bool rev; }; static Node* pool; static int poolPtr = 0; static unsigned rngState = 123456789u; static inline unsigned rnd() { rngState ^= rngState << 13; rngState ^= rngState >> 17; rngState ^= rngState << 5; return rngState; } static inline ll sz(Node* t) { return t ? t->sz : 0; } static Node* makeNode(ll len, int slope) { if (len <= 0) return nullptr; Node* t = &pool[poolPtr++]; t->l = t->r = nullptr; t->len = t->sz = len; t->slope = t->mn = t->mx = slope; t->sum = (ll)slope * len; t->pri = rnd(); t->lazyA = 1; t->lazyB = 0; t->rev = false; return t; } static inline void pull(Node* t) { t->sz = t->len + sz(t->l) + sz(t->r); t->sum = (ll)t->slope * t->len; t->mn = t->mx = t->slope; if (t->l) { t->sum += t->l->sum; t->mn = min(t->mn, t->l->mn); t->mx = max(t->mx, t->l->mx); } if (t->r) { t->sum += t->r->sum; t->mn = min(t->mn, t->r->mn); t->mx = max(t->mx, t->r->mx); } } // Replaces every slope s in the subtree by a*s+b. static void apply(Node* t, int a, ll b) { if (!t) return; t->slope = (int)(a * (ll)t->slope + b); t->sum = a * t->sum + b * t->sz; if (a == 1) { t->mn = (int)(t->mn + b); t->mx = (int)(t->mx + b); } else { int oldMn = t->mn, oldMx = t->mx; t->mn = (int)(-oldMx + b); t->mx = (int)(-oldMn + b); } t->lazyA *= a; t->lazyB = a * t->lazyB + b; } static inline void push(Node* t) { if (!t) return; if (t->rev) { swap(t->l, t->r); if (t->l) t->l->rev ^= 1; if (t->r) t->r->rev ^= 1; t->rev = false; } if (t->lazyA != 1 || t->lazyB != 0) { apply(t->l, t->lazyA, t->lazyB); apply(t->r, t->lazyA, t->lazyB); t->lazyA = 1; t->lazyB = 0; } } static Node* merge(Node* a, Node* b) { if (!a) return b; if (!b) return a; if (a->pri > b->pri) { push(a); a->r = merge(a->r, b); pull(a); return a; } else { push(b); b->l = merge(a, b->l); pull(b); return b; } } // Splits into the first k units of length and the remainder. static void split(Node* t, ll k, Node*& a, Node*& b) { if (!t) { a = b = nullptr; return; } push(t); ll leftSize = sz(t->l); if (k < leftSize) { split(t->l, k, a, t->l); b = t; pull(b); return; } if (k > leftSize + t->len) { split(t->r, k - leftSize - t->len, t->r, b); a = t; pull(a); return; } ll take = k - leftSize; if (take == 0) { a = t->l; t->l = nullptr; b = t; pull(b); } else if (take == t->len) { b = t->r; t->r = nullptr; a = t; pull(a); } else { Node* rightPart = makeNode(t->len - take, t->slope); rightPart->r = t->r; pull(rightPart); t->len = take; t->r = nullptr; a = t; b = rightPart; pull(a); } } // Returns the length before the first segment whose slope is at least c. static ll firstGE(Node* t, ll c) { if (!t || t->mx < c) return sz(t); push(t); if (t->l && t->l->mx >= c) return firstGE(t->l, c); ll leftSize = sz(t->l); if (t->slope >= c) return leftSize; return leftSize + t->len + firstGE(t->r, c); } static ll prefixSum(Node* t, ll k) { if (!t || k <= 0) return 0; push(t); ll leftSize = sz(t->l); if (k <= leftSize) return prefixSum(t->l, k); ll result = t->l ? t->l->sum : 0; k -= leftSize; if (k <= t->len) return result + (ll)t->slope * k; result += (ll)t->slope * t->len; return result + prefixSum(t->r, k - t->len); } static inline ll evaluate(Node* t, ll base, int lastSlope, ll length, ll d) { if (d >= length) { return base + (t ? t->sum : 0) + (ll)lastSlope * (d - length); } return base + prefixSum(t, d); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; pool = new Node[2 * N + 5]; // V_2(d) = c_1 * max(w_1,d). Its derivative is represented by T. ll base = c[1] * w[1]; ll length = w[1]; int lastSlope = (int)c[1]; Node* T = makeNode(length, 0); string output; output.reserve((size_t)N * 12); output += to_string(evaluate(T, base, lastSlope, length, w[2])); output.push_back('\n'); for (int j = 2; j < N; ++j) { ll W = w[j]; ll cost = c[j]; // Restrict the derivative to [0,W], extending with its final slope if needed. if (W < length) { Node *P, *discarded; split(T, W, P, discarded); T = P; } else if (W > length) { T = merge(T, makeNode(W - length, lastSlope)); } // Remove the suffix with slopes >= cost. It becomes a flat prefix. ll pos = firstGE(T, cost); Node *A, *X; if (pos == 0) { A = nullptr; X = T; } else if (pos == W) { A = T; X = nullptr; } else { split(T, pos, A, X); } ll flatLength = sz(X); base += (A ? A->sum : 0) + cost * flatLength; // Reverse the remaining segments and replace every slope s by cost-s. if (A) { apply(A, -1, cost); A->rev ^= 1; } T = merge(makeNode(flatLength, 0), A); length = W; lastSlope = (int)cost; output += to_string(evaluate(T, base, lastSlope, length, w[j + 1])); output.push_back('\n'); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { int l = 0, r = 0; unsigned priority = 0; ll len = 0, slope = 0; ll sumLen = 0, sumArea = 0; ll minSlope = 0, maxSlope = 0; int lazyA = 1; ll lazyB = 0; bool rev = false; }; struct FunctionTreap { vector<Node> tr; mt19937 rng{712367821}; FunctionTreap(int n) { tr.reserve(2 * n + 10); tr.emplace_back(); } int newNode(ll len, ll slope) { Node x; x.len = x.sumLen = len; x.slope = x.minSlope = x.maxSlope = slope; x.sumArea = len * slope; x.priority = rng(); tr.push_back(x); return (int)tr.size() - 1; } ll getLen(int t) const { return t ? tr[t].sumLen : 0; } ll getArea(int t) const { return t ? tr[t].sumArea : 0; } void pull(int t) { if (!t) return; Node &x = tr[t]; x.sumLen = x.len + getLen(x.l) + getLen(x.r); x.sumArea = x.slope * x.len + getArea(x.l) + getArea(x.r); x.minSlope = x.maxSlope = x.slope; if (x.l) { x.minSlope = min(x.minSlope, tr[x.l].minSlope); x.maxSlope = max(x.maxSlope, tr[x.l].maxSlope); } if (x.r) { x.minSlope = min(x.minSlope, tr[x.r].minSlope); x.maxSlope = max(x.maxSlope, tr[x.r].maxSlope); } } void applyReverse(int t) { if (!t) return; swap(tr[t].l, tr[t].r); tr[t].rev = !tr[t].rev; } // Replace every slope s by a*s+b. void applyAffine(int t, int a, ll b) { if (!t) return; Node &x = tr[t]; x.slope = a * x.slope + b; x.sumArea = a * x.sumArea + b * x.sumLen; if (a == 1) { x.minSlope += b; x.maxSlope += b; } else { ll oldMin = x.minSlope, oldMax = x.maxSlope; x.minSlope = b - oldMax; x.maxSlope = b - oldMin; } x.lazyB = a * x.lazyB + b; x.lazyA = a * x.lazyA; } void push(int t) { if (!t) return; if (tr[t].rev) { applyReverse(tr[t].l); applyReverse(tr[t].r); tr[t].rev = false; } if (tr[t].lazyA != 1 || tr[t].lazyB != 0) { int a = tr[t].lazyA; ll b = tr[t].lazyB; applyAffine(tr[t].l, a, b); applyAffine(tr[t].r, a, b); tr[t].lazyA = 1; tr[t].lazyB = 0; } } int merge(int a, int b) { if (!a) return b; if (!b) return a; if (tr[a].priority > tr[b].priority) { push(a); tr[a].r = merge(tr[a].r, b); pull(a); return a; } else { push(b); tr[b].l = merge(a, tr[b].l); pull(b); return b; } } // Split so that the first returned tree has total length k. pair<int, int> splitLen(int t, ll k) { if (!t) return {0, 0}; if (k <= 0) return {0, t}; if (k >= tr[t].sumLen) return {t, 0}; push(t); ll leftLen = getLen(tr[t].l); if (k <= leftLen) { auto p = splitLen(tr[t].l, k); tr[t].l = p.second; pull(t); return {p.first, t}; } ll rem = k - leftLen; ll nodeLen = tr[t].len; if (rem < nodeLen) { ll slope = tr[t].slope; int b = newNode(slope, nodeLen - rem); tr[b].r = tr[t].r; pull(b); tr[t].len = rem; tr[t].r = 0; pull(t); return {t, b}; } if (rem == nodeLen) { int b = tr[t].r; tr[t].r = 0; pull(t); return {t, b}; } auto p = splitLen(tr[t].r, rem - nodeLen); tr[t].r = p.first; pull(t); return {t, p.second}; } // Area under the finite segments over their first k units of length. ll areaPrefix(int t, ll k) { if (!t || k <= 0) return 0; if (k >= tr[t].sumLen) return tr[t].sumArea; push(t); int l = tr[t].l; ll leftLen = getLen(l); if (k <= leftLen) return areaPrefix(l, k); ll result = getArea(l); k -= leftLen; if (k <= tr[t].len) { result += tr[t].slope * k; return result; } result += tr[t].slope * tr[t].len; k -= tr[t].len; result += areaPrefix(tr[t].r, k); return result; } // Length before the first segment whose slope is at least c. ll lowerSlope(int t, ll c, bool &found) { push(t); int l = tr[t].l, r = tr[t].r; if (l && tr[l].maxSlope >= c) { return lowerSlope(l, c, found); } if (tr[t].slope >= c) { found = true; return getLen(l); } if (r && tr[r].maxSlope >= c) { ll base = getLen(l) + tr[t].len; return base + lowerSlope(r, c, found); } found = false; return 0; } }; struct Solver { int root = 0; ll minimum = 0; ll flatEnd = 0; ll finalSlope = 0; FunctionTreap treap; Solver(int n, ll w1, ll c1) : treap(n) { minimum = w1 * c1; flatEnd = w1; finalSlope = c1; } ll evaluate(ll x) { if (x <= flatEnd) return minimum; ll d = x - flatEnd; ll totalLen = treap.getLen(root); if (d >= totalLen) { return minimum + treap.getArea(root) + finalSlope * (d - totalLen); } return minimum + treap.areaPrefix(root, d); } void update(ll w, ll c, ll valueAtW) { ll oldFlatEnd = flatEnd; ll oldFinalSlope = finalSlope; ll crossing = 0; ll newFlatEnd = 0; ll upper = w; if (c <= oldFinalSlope) { bool found = false; ll offset = treap.lowerSlope(root, c, found); if (!found) offset = treap.getLen(root); crossing = oldFlatEnd + offset; if (w > crossing) newFlatEnd = w - crossing; upper = min(w, crossing); } ll baseValue; if (newFlatEnd > 0) baseValue = evaluate(crossing); else baseValue = valueAtW; ll newMinimum = c * newFlatEnd + baseValue; ll needed = upper - oldFlatEnd; if (needed <= 0) { root = 0; } else { ll totalLen = treap.getLen(root); if (needed < totalLen) { root = treap.splitLen(root, needed).first; } else if (needed > totalLen) { int extra = treap.newNode(needed - totalLen, oldFinalSlope); root = treap.merge(root, extra); } treap.applyReverse(root); treap.applyAffine(root, -1, c); } minimum = newMinimum; flatEnd = newFlatEnd; finalSlope = c; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; Solver solver(N, w[1], c[1]); string output; output.reserve((size_t)N * 12); for (int i = 2; i <= N; ++i) { ll answer = solver.evaluate(w[i]); output += to_string(answer); output.push_back('\n'); if (i < N) solver.update(w[i], c[i], answer); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; static const int64 INF_LEN = (1LL << 60); struct Line { int64 s, b; }; // Maps (s,b) to (a*s+k, b+e*s+f). struct Transform { int64 a, k, e, f; }; const Transform IDENTITY{1, 0, 0, 0}; bool isIdentity(const Transform &t) { return t.a == 1 && t.k == 0 && t.e == 0 && t.f == 0; } // Returns p after q. Transform compose(const Transform &p, const Transform &q) { return { p.a * q.a, p.a * q.k + p.k, q.e + p.e * q.a, q.f + p.e * q.k + p.f }; } struct Node { Node *l = nullptr, *r = nullptr; uint32_t priority; int64 len, sum; Line line; Transform tr = IDENTITY; bool rev = false; Node(int64 length, Line ln, uint32_t p) : priority(p), len(length), sum(length), line(ln) {} }; mt19937 rng(712367821); int64 lengthSum(Node *t) { return t ? t->sum : 0; } void pull(Node *t) { i128 total = (i128)lengthSum(t->l) + t->len + lengthSum(t->r); t->sum = total > INF_LEN ? INF_LEN : (int64)total; } Line getLine(const Node *t) { const Transform &p = t->tr; int64 s = (int64)((i128)p.a * t->line.s + p.k); int64 b = (int64)((i128)t->line.b + (i128)p.e * t->line.s + p.f); return {s, b}; } int64 evaluate(const Node *t, int64 x) { Line ln = getLine(t); return (int64)((i128)ln.s * x + ln.b); } void applyTransform(Node *t, const Transform &p) { if (t) t->tr = compose(p, t->tr); } void applyReverse(Node *t) { if (!t) return; swap(t->l, t->r); t->rev ^= true; } void push(Node *t) { if (!t) return; if (t->rev) { applyReverse(t->l); applyReverse(t->r); t->rev = false; } if (!isIdentity(t->tr)) { Transform p = t->tr; Line cur = getLine(t); applyTransform(t->l, p); applyTransform(t->r, p); t->line = cur; t->tr = IDENTITY; } } Node* mergeTrees(Node *a, Node *b) { if (!a) return b; if (!b) return a; if (a->priority < b->priority) { push(a); a->r = mergeTrees(a->r, b); pull(a); return a; } else { push(b); b->l = mergeTrees(a, b->l); pull(b); return b; } } // Keeps exactly the first d units of length and discards the remainder. Node* cutPrefix(Node *t, int64 d) { push(t); int64 left = lengthSum(t->l); if (d < left) return cutPrefix(t->l, d); if (d == left) return t->l; int64 rem = d - left; if (rem <= t->len) { t->len = rem; t->r = nullptr; pull(t); return t; } t->r = cutPrefix(t->r, rem - t->len); pull(t); return t; } Node* firstNode(Node *t) { while (true) { push(t); if (!t->l) return t; t = t->l; } } Node* lastNode(Node *t) { while (true) { push(t); if (!t->r) return t; t = t->r; } } Node* popFirst(Node *t) { push(t); if (!t->l) return t->r; t->l = popFirst(t->l); pull(t); return t; } // Finds the segment containing the point at distance d from the branch start. Node* locate(Node *t, int64 d) { push(t); int64 left = lengthSum(t->l); if (d <= left) return locate(t->l, d); int64 rem = d - left; if (rem <= t->len) return t; return locate(t->r, rem - t->len); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int64> w(N + 1), c(N); for (int i = 1; i <= N; ++i) cin >> w[i]; for (int i = 1; i < N; ++i) cin >> c[i]; Node *root = new Node(INF_LEN, {c[1], 0}, rng()); int64 minimizer = w[1]; int64 minimumValue = c[1] * w[1]; for (int i = 2; i < N; ++i) { int64 answer; int64 d = w[i] - minimizer; if (d <= 0) { answer = minimumValue; root = new Node(INF_LEN, {c[i], minimumValue}, rng()); minimizer = 0; } else { root = cutPrefix(root, d); answer = evaluate(lastNode(root), w[i]); applyReverse(root); applyTransform(root, Transform{-1, c[i], w[i], 0}); root = mergeTrees( root, new Node(INF_LEN, {c[i], minimumValue}, rng()) ); int64 newMinimizer = 0; while (getLine(firstNode(root)).s < 0) { Node *f = firstNode(root); newMinimizer += f->len; root = popFirst(root); } minimizer = newMinimizer; minimumValue = evaluate(firstNode(root), minimizer); } cout << answer << '\n'; } int64 answer; if (w[N] <= minimizer) { answer = minimumValue; } else { answer = evaluate(locate(root, w[N] - minimizer), w[N]); } cout << answer << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 500005; struct Node { ll x, v; ll sumV, sumVX; ll add; int l, r, sz; int mul; uint32_t pri; bool rev; Node() : x(0), v(0), sumV(0), sumVX(0), add(0), l(0), r(0), sz(0), mul(1), pri(0), rev(false) {} }; Node tr[MAXN]; int ptrNode = 0; uint32_t rngState = 2463534242u; inline uint32_t rng() { rngState ^= rngState << 13; rngState ^= rngState >> 17; rngState ^= rngState << 5; return rngState; } inline void pull(int t) { if (!t) return; tr[t].sz = 1 + tr[tr[t].l].sz + tr[tr[t].r].sz; tr[t].sumV = tr[t].v + tr[tr[t].l].sumV + tr[tr[t].r].sumV; tr[t].sumVX = tr[t].v * tr[t].x + tr[tr[t].l].sumVX + tr[tr[t].r].sumVX; } inline void applyAffine(int t, int mul, ll add) { if (!t) return; tr[t].x = (ll)mul * tr[t].x + add; tr[t].sumVX = (ll)mul * tr[t].sumVX + add * tr[t].sumV; tr[t].add = (ll)mul * tr[t].add + add; tr[t].mul = mul * tr[t].mul; } inline void applyReverse(int t) { if (!t) return; swap(tr[t].l, tr[t].r); tr[t].rev ^= 1; } inline void push(int t) { if (!t) return; if (tr[t].mul != 1 || tr[t].add != 0) { applyAffine(tr[t].l, tr[t].mul, tr[t].add); applyAffine(tr[t].r, tr[t].mul, tr[t].add); tr[t].mul = 1; tr[t].add = 0; } if (tr[t].rev) { applyReverse(tr[t].l); applyReverse(tr[t].r); tr[t].rev = false; } } int newNode(ll x, ll v) { int t = ++ptrNode; tr[t].x = x; tr[t].v = v; tr[t].sumV = v; tr[t].sumVX = v * x; tr[t].l = tr[t].r = 0; tr[t].sz = 1; tr[t].mul = 1; tr[t].add = 0; tr[t].pri = rng(); tr[t].rev = false; return t; } void splitByCoord(int t, ll key, int &a, int &b) { if (!t) { a = b = 0; return; } push(t); if (tr[t].x < key) { int a1, b1; splitByCoord(tr[t].r, key, a1, b1); tr[t].r = a1; pull(t); a = t; b = b1; } else { int a1, b1; splitByCoord(tr[t].l, key, a1, b1); tr[t].l = b1; pull(t); a = a1; b = t; } } int mergeTreap(int a, int b) { if (!a || !b) return a ? a : b; if (tr[a].pri > tr[b].pri) { push(a); tr[a].r = mergeTreap(tr[a].r, b); pull(a); return a; } else { push(b); tr[b].l = mergeTreap(a, tr[b].l); pull(b); return b; } } struct MinInfo { ll m; ll r; ll fm; }; MinInfo getMin(int root, ll base, ll F0) { if (base >= 0) { return {0, base, F0}; } ll need = -base; int cur = root; ll accV = 0, accVX = 0; ll rem = need; while (cur) { push(cur); int l = tr[cur].l; ll lv = tr[l].sumV; ll lvx = tr[l].sumVX; if (lv >= rem) { cur = l; } else { rem -= lv; if (tr[cur].v >= rem) { ll prefV = accV + lv; ll prefVX = accVX + lvx; ll m = tr[cur].x; ll r = base + prefV + tr[cur].v; ll fm = F0 + base * m + prefV * m - prefVX; return {m, r, fm}; } else { rem -= tr[cur].v; accV += lv + tr[cur].v; accVX += lvx + tr[cur].v * tr[cur].x; cur = tr[cur].r; } } } return {0, base, F0}; } pair<ll, ll> prefixLess(int root, ll key) { ll resV = 0, resVX = 0; int cur = root; while (cur) { push(cur); if (tr[cur].x < key) { int l = tr[cur].l; resV += tr[l].sumV + tr[cur].v; resVX += tr[l].sumVX + tr[cur].v * tr[cur].x; cur = tr[cur].r; } else { cur = tr[cur].l; } } return {resV, resVX}; } ll query(int root, ll base, ll F0, ll W) { MinInfo info = getMin(root, base, F0); if (W <= info.m) { return info.fm; } auto [sv, svx] = prefixLess(root, W); return F0 + base * W + sv * W - svx; } void update(int &root, ll &F0, ll &base, ll w, ll c) { MinInfo info = getMin(root, base, F0); ll m = info.m; if (w <= m) { F0 = info.fm; base = c; root = 0; return; } int A, BC, B, C; splitByCoord(root, m + 1, A, BC); splitByCoord(BC, w, B, C); ll sumA = tr[A].sumV; ll sumAx = tr[A].sumVX; ll sumB = tr[B].sumV; ll sumBx = tr[B].sumVX; ll sumVw = sumA + sumB; ll sumVXw = sumAx + sumBx; ll Fw = F0 + base * w + sumVw * w - sumVXw; ll FprimeWminus = base + sumVw; ll baseNew = c - FprimeWminus; ll r = info.r; applyAffine(B, -1, w); applyReverse(B); if (r > 0) { int node = newNode(w - m, r); B = mergeTreap(B, node); } root = B; F0 = Fw; base = baseNew; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<ll> w(N + 1), c(N + 1); for (int i = 1; i <= N; i++) cin >> w[i]; for (int i = 1; i <= N - 1; i++) cin >> c[i]; ll ans2 = c[1] * max(w[1], w[2]); cout << ans2 << '\n'; if (N == 2) return 0; ll F0 = ans2; ll base; int root = 0; if (w[2] > w[1]) { base = c[2] - c[1]; ll a = w[2] - w[1]; root = newNode(a, c[1]); } else { base = c[2]; root = 0; } for (int i = 3; i <= N; i++) { ll ans = query(root, base, F0, w[i]); cout << ans << '\n'; if (i <= N - 1) { update(root, F0, base, w[i], c[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { long long pos, delta; long long sumD, sumP; long long mul, add; int l, r, sz; uint32_t pri; bool rev; }; vector<Node> tr; int tot = 0; uint32_t rng_state = 712367821; uint32_t rng32() { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17; rng_state ^= rng_state << 5; return rng_state; } int getSz(int x) { return x ? tr[x].sz : 0; } long long getSumD(int x) { return x ? tr[x].sumD : 0; } long long getSumP(int x) { return x ? tr[x].sumP : 0; } void pull(int x) { if (!x) return; tr[x].sz = 1 + getSz(tr[x].l) + getSz(tr[x].r); tr[x].sumD = tr[x].delta + getSumD(tr[x].l) + getSumD(tr[x].r); tr[x].sumP = tr[x].delta * tr[x].pos + getSumP(tr[x].l) + getSumP(tr[x].r); } void applyAff(int x, long long A, long long B) { if (!x) return; tr[x].pos = A * tr[x].pos + B; tr[x].sumP = A * tr[x].sumP + B * tr[x].sumD; tr[x].mul = A * tr[x].mul; tr[x].add = A * tr[x].add + B; } void applyRev(int x) { if (!x) return; swap(tr[x].l, tr[x].r); tr[x].rev ^= 1; } void push(int x) { if (!x) return; if (tr[x].rev) { applyRev(tr[x].l); applyRev(tr[x].r); tr[x].rev = false; } if (tr[x].mul != 1 || tr[x].add != 0) { long long A = tr[x].mul; long long B = tr[x].add; applyAff(tr[x].l, A, B); applyAff(tr[x].r, A, B); tr[x].mul = 1; tr[x].add = 0; } } int newNode(long long pos, long long delta) { ++tot; tr[tot].pos = pos; tr[tot].delta = delta; tr[tot].sumD = delta; tr[tot].sumP = delta * pos; tr[tot].mul = 1; tr[tot].add = 0; tr[tot].l = tr[tot].r = 0; tr[tot].sz = 1; tr[tot].pri = rng32(); tr[tot].rev = false; return tot; } int mergeTreap(int a, int b) { if (!a || !b) return a ? a : b; if (tr[a].pri > tr[b].pri) { push(a); tr[a].r = mergeTreap(tr[a].r, b); pull(a); return a; } else { push(b); tr[b].l = mergeTreap(a, tr[b].l); pull(b); return b; } } void splitTreap(int x, int k, int &a, int &b) { if (!x) { a = b = 0; return; } push(x); if (getSz(tr[x].l) >= k) { splitTreap(tr[x].l, k, a, tr[x].l); b = x; pull(b); } else { splitTreap(tr[x].r, k - getSz(tr[x].l) - 1, tr[x].r, b); a = x; pull(a); } } int countLess(int root, long long x) { int cur = root; int res = 0; while (cur) { push(cur); if (tr[cur].pos < x) { res += getSz(tr[cur].l) + 1; cur = tr[cur].r; } else { cur = tr[cur].l; } } return res; } long long findCross(int root, long long target) { int cur = root; long long acc = 0; while (cur) { push(cur); long long leftSum = getSumD(tr[cur].l); if (acc + leftSum + tr[cur].delta >= target) { if (acc + leftSum >= target) { cur = tr[cur].l; } else { return tr[cur].pos; } } else { acc += leftSum + tr[cur].delta; cur = tr[cur].r; } } return (1LL << 60); } pair<long long, long long> prefixInfo(int root, long long x) { int cur = root; long long sumD = 0, sumP = 0; while (cur) { push(cur); if (tr[cur].pos < x) { int l = tr[cur].l; sumD += getSumD(l) + tr[cur].delta; sumP += getSumP(l) + tr[cur].delta * tr[cur].pos; cur = tr[cur].r; } else { cur = tr[cur].l; } } return {sumD, sumP}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<long long> w(N + 1); for (int i = 1; i <= N; i++) cin >> w[i]; vector<long long> c(N); for (int i = 1; i <= N - 1; i++) cin >> c[i]; tr.assign(N + 10, Node()); int root = 0; long long minimizer = w[1]; long long bestValue = c[1] * w[1]; long long initialSlope = c[1]; auto evalD = [&](long long t) -> long long { if (t <= 0) return 0; auto [sumD, sumP] = prefixInfo(root, t); return initialSlope * t + t * sumD - sumP; }; auto query = [&](long long x) -> long long { if (x <= minimizer) return bestValue; long long s = x - minimizer; return bestValue + evalD(s); }; auto update = [&](long long demand, long long cost) { long long A = demand - minimizer; if (A <= 0) { root = 0; minimizer = 0; initialSlope = cost; return; } if (initialSlope >= cost) { bestValue += cost * A; minimizer = A; initialSlope = cost; root = 0; return; } long long target = cost - initialSlope; long long totalJump = root ? tr[root].sumD : 0; long long crossPos; if (totalJump < target) { crossPos = (1LL << 60); } else { crossPos = findCross(root, target); } long long t = min(A, crossPos); int k = countLess(root, t); int L, R; splitTreap(root, k, L, R); long long sumD = L ? tr[L].sumD : 0; long long sumP = L ? tr[L].sumP : 0; long long rLeft = initialSlope + sumD; long long D = initialSlope * t + t * sumD - sumP; bestValue = bestValue + D + cost * (A - t); minimizer = A - t; long long oldInitial = initialSlope; initialSlope = cost - rLeft; if (L) { applyRev(L); applyAff(L, -1, t); } if (oldInitial > 0) { int node = newNode(t, oldInitial); L = mergeTreap(L, node); } root = L; // R is discarded. }; for (int i = 2; i <= N; i++) { long long ans = query(w[i]); cout << ans << '\n'; if (i < N) { update(w[i], c[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Node { int l, r, val, len, sz; uint32_t pri; long long sum; long long lazyAdd; int lazyMul; bool rev; }; vector<Node> tr; uint32_t rng_state = 2463534242u; uint32_t rng32() { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17; rng_state ^= rng_state << 5; return rng_state; } int newNode(int v, int len) { Node nd; nd.l = nd.r = 0; nd.val = v; nd.len = len; nd.sz = len; nd.pri = rng32(); nd.sum = 1LL * v * len; nd.lazyAdd = 0; nd.lazyMul = 1; nd.rev = false; tr.push_back(nd); return (int)tr.size() - 1; } inline int getSz(int t) { return t ? tr[t].sz : 0; } inline long long getSum(int t) { return t ? tr[t].sum : 0; } inline void pull(int t) { if (!t) return; tr[t].sz = tr[t].len + getSz(tr[t].l) + getSz(tr[t].r); tr[t].sum = 1LL * tr[t].val * tr[t].len + getSum(tr[t].l) + getSum(tr[t].r); } inline void applyRev(int t) { if (!t) return; swap(tr[t].l, tr[t].r); tr[t].rev ^= 1; } inline void applyAffine(int t, int mul, long long add) { if (!t) return; tr[t].val = (int)(1LL * mul * tr[t].val + add); tr[t].sum = 1LL * mul * tr[t].sum + add * (long long)tr[t].sz; long long oldAdd = tr[t].lazyAdd; int oldMul = tr[t].lazyMul; tr[t].lazyMul = mul * oldMul; tr[t].lazyAdd = 1LL * mul * oldAdd + add; } inline void push(int t) { if (!t) return; if (tr[t].rev) { applyRev(tr[t].l); applyRev(tr[t].r); tr[t].rev = false; } if (tr[t].lazyMul != 1 || tr[t].lazyAdd != 0) { int m = tr[t].lazyMul; long long a = tr[t].lazyAdd; applyAffine(tr[t].l, m, a); applyAffine(tr[t].r, m, a); tr[t].lazyMul = 1; tr[t].lazyAdd = 0; } } int mergeTreap(int a, int b) { if (!a) return b; if (!b) return a; if (tr[a].pri > tr[b].pri) { push(a); tr[a].r = mergeTreap(tr[a].r, b); pull(a); return a; } else { push(b); tr[b].l = mergeTreap(a, tr[b].l); pull(b); return b; } } void splitTreap(int t, int k, int &a, int &b) { if (!t) { a = b = 0; return; } if (k <= 0) { a = 0; b = t; return; } if (k >= tr[t].sz) { a = t; b = 0; return; } push(t); int lsz = getSz(tr[t].l); int len = tr[t].len; if (k < lsz) { splitTreap(tr[t].l, k, a, tr[t].l); pull(t); b = t; } else if (k > lsz + len) { splitTreap(tr[t].r, k - lsz - len, tr[t].r, b); pull(t); a = t; } else if (k == lsz) { a = tr[t].l; tr[t].l = 0; pull(t); b = t; } else if (k == lsz + len) { b = tr[t].r; tr[t].r = 0; pull(t); a = t; } else { int take = k - lsz; int oldL = tr[t].l; int oldR = tr[t].r; int leftPart = newNode(tr[t].val, take); int rightPart = newNode(tr[t].val, len - take); a = mergeTreap(oldL, leftPart); b = mergeTreap(rightPart, oldR); } } void splitByValue(int t, int c, int &a, int &b) { if (!t) { a = b = 0; return; } push(t); if (tr[t].val <= c) { int ra, rb; splitByValue(tr[t].r, c, ra, rb); tr[t].r = ra; pull(t); a = t; b = rb; } else { int la, lb; splitByValue(tr[t].l, c, la, lb); tr[t].l = lb; pull(t); a = la; b = t; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> w(N + 1); for (int i = 1; i <= N; i++) cin >> w[i]; vector<int> c(N); for (int i = 1; i < N; i++) cin >> c[i]; tr.reserve(2500005); tr.push_back({0, 0, 0, 0, 0, 0, 0, 0, 1, false}); int root = 0; int L = w[1]; if (L > 0) { root = newNode(0, L); } int tail = c[1]; long long h0 = 1LL * c[1] * w[1]; for (int j = 2; j <= N; j++) { int wj = w[j]; int curLen = getSz(root); int take = min(wj, curLen); int Sroot, rest; splitTreap(root, take, Sroot, rest); int appended = wj - take; if (appended > 0) { int node = newNode(tail, appended); Sroot = mergeTreap(Sroot, node); } long long sumS = getSum(Sroot); long long ans = h0 + sumS; cout << ans << '\n'; if (j == N) break; int cc = c[j]; int A, Z; splitByValue(Sroot, cc, A, Z); int cntZ = getSz(Z); long long sumZ = getSum(Z); long long h0_new = h0 + sumS + 1LL * cntZ * cc - sumZ; applyAffine(A, -1, cc); applyRev(A); int B = 0; if (cntZ > 0) { B = newNode(0, cntZ); } B = mergeTreap(B, A); root = B; tail = cc; h0 = h0_new; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; /* Maintains the vertices of a concave piecewise-linear function. A stored point (X, Y) represents the actual point x = sign * X + shiftX y = Y + coefX * X + shiftY The sign of the x-coordinate can change when the graph is reflected, so the order of the vertices is also stored in reversed order. */ class ConcaveDeque { private: struct Point { int64 x; i128 y; }; deque<Point> dq; bool reversed = false; int64 sign = 1; int64 shiftX = 0; i128 coefX = 0; i128 shiftY = 0; Point decode(const Point& p) const { return { sign * p.x + shiftX, p.y + coefX * (i128)p.x + shiftY }; } Point encode(int64 x, i128 y) const { int64 X = sign * (x - shiftX); i128 Y = y - coefX * (i128)X - shiftY; return {X, Y}; } size_t size() const { return dq.size(); } Point back() const { if (!reversed) return decode(dq.back()); return decode(dq.front()); } Point secondBack() const { if (!reversed) return decode(dq[dq.size() - 2]); return decode(dq[1]); } void popBack() { if (!reversed) dq.pop_back(); else dq.pop_front(); } void pushFront(int64 x, i128 y) { Point p = encode(x, y); if (!reversed) dq.push_front(p); else dq.push_back(p); } void pushBack(int64 x, i128 y) { Point p = encode(x, y); if (!reversed) dq.push_back(p); else dq.push_front(p); } public: /* Reflects the graph about x = C / 2 and adds v*x: (x, y) -> (C-x, y + v*(C-x)). This is the transformation needed when adding a new plant. */ void reflectAndShift(int64 C, int64 v) { int64 oldSign = sign; int64 oldShiftX = shiftX; coefX = coefX - (i128)v * oldSign; shiftY = shiftY + (i128)v * ((i128)C - oldShiftX); sign = -oldSign; shiftX = C - oldShiftX; reversed = !reversed; } /* Restricts the graph to x in [0, C], inserting a vertex if C lies inside a segment. */ void cutAt(int64 C) { while (size() >= 2 && secondBack().x > C) { popBack(); } if (size() >= 2 && back().x > C) { Point left = secondBack(); Point right = back(); popBack(); if (left.x < C) { // Every segment has an integer slope. i128 slope = (right.y - left.y) / (i128)(right.x - left.x); i128 y = left.y + slope * (i128)(C - left.x); pushBack(C, y); } } } /* The graph is concave. Remove its descending right-hand suffix. The remaining right endpoint is the rightmost maximum. Returns {position of maximum, maximum value}. */ pair<int64, i128> trimToMaximum() { while (size() >= 2) { Point left = secondBack(); Point right = back(); if (right.y < left.y) { popBack(); } else { break; } } Point p = back(); return {p.x, p.y}; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int64> w(N); vector<int64> c(N - 1); for (int i = 0; i < N; ++i) cin >> w[i]; for (int i = 0; i < N - 1; ++i) cin >> c[i]; string output; output.reserve((size_t)N * 20); auto emit = [&](i128 answer) { // The answer is at most N * 10^6 * 10^6 <= 5 * 10^17. output += to_string((int64)answer); output.push_back('\n'); }; ConcaveDeque graph; /* For the first two plants, with y_2 = x: F_2(x) = w_1 * (c_1 - x) + w_2 * x = w_1*c_1 + (w_2-w_1)*x, for 0 <= x <= c_1. */ graph.pushBack(0, (i128)w[0] * c[0]); graph.pushBack(c[0], (i128)w[1] * c[0]); auto state = graph.trimToMaximum(); int64 maximumPosition = state.first; i128 maximumValue = state.second; emit(maximumValue); for (int plant = 2; plant < N; ++plant) { int64 C = c[plant - 1]; int64 v = w[plant]; if (C >= maximumPosition) { /* Add the linear segment M + v*x on [0, C-p], then reflect the old graph from [0,p] onto [C-p,C]. */ int64 length = C - maximumPosition; graph.reflectAndShift(C, v); if (length > 0) { graph.pushFront(0, maximumValue); } } else { /* Every queried argument is below the old maximum. Keep only the old graph on [0,C], then reflect it. */ graph.cutAt(C); graph.reflectAndShift(C, v); } state = graph.trimToMaximum(); maximumPosition = state.first; maximumValue = state.second; emit(maximumValue); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; class FastInput { static constexpr int BUFFER_SIZE = 1 << 16; char buffer[BUFFER_SIZE]; int pos = 0, len = 0; int getChar() { if (pos == len) { len = static_cast<int>( fread(buffer, 1, BUFFER_SIZE, stdin) ); pos = 0; if (len == 0) return EOF; } return static_cast<unsigned char>(buffer[pos++]); } public: bool readInt(int& value) { int ch = getChar(); while (ch != EOF && ch <= ' ') ch = getChar(); if (ch == EOF) return false; value = 0; while (ch >= '0' && ch <= '9') { value = value * 10 + (ch - '0'); ch = getChar(); } return true; } }; class SlopeDeque { struct Block { int64 len; int64 raw; }; deque<Block> blocks; // Logical order is the reverse of physical order when flipped. bool flipped = false; // Actual value of a block = mul * raw + add. // mul is always either 1 or -1. int64 mul = 1; int64 add = 0; int64 totalLength = 0; int64 totalArea = 0; int64 actualValue(const Block& block) const { return mul * block.raw + add; } Block& logicalBack() { return flipped ? blocks.front() : blocks.back(); } void popLogicalBack() { if (flipped) blocks.pop_front(); else blocks.pop_back(); } // Append a block whose actual value is zero. void appendLogicalBackZero(int64 length) { if (length == 0) return; // mul * raw + add = 0, and mul^2 = 1. Block block{length, -mul * add}; if (flipped) blocks.push_front(block); else blocks.push_back(block); totalLength += length; // Its contribution to totalArea is zero. } public: SlopeDeque(int64 initialValue, int64 initialLength) { blocks.push_back({initialLength, initialValue}); totalLength = initialLength; totalArea = initialValue * initialLength; } // Returns: // I = integral of the old h over [0, c] // K = integral of max(0, w - old_h(x)) over [0, c] pair<int64, int64> transition(int64 c, int64 w) { // Retain only the prefix of length c. while (totalLength > c) { Block& block = logicalBack(); int64 value = actualValue(block); int64 removed = min(block.len, totalLength - c); totalArea -= value * removed; totalLength -= removed; block.len -= removed; if (block.len == 0) { popLogicalBack(); } } const int64 I = totalArea; // If the stored function ends before c, its remaining part is zero. if (totalLength < c) { appendLogicalBackZero(c - totalLength); } // Reflect: h(c-u). flipped = !flipped; // After reflection, values are nondecreasing. // Pop the suffix whose values are greater than w. int64 highLength = 0; while (!blocks.empty()) { Block block = logicalBack(); int64 value = actualValue(block); if (value <= w) break; highLength += block.len; totalLength -= block.len; totalArea -= value * block.len; popLogicalBack(); } const int64 lowLength = totalLength; const int64 lowArea = totalArea; const int64 K = w * lowLength - lowArea; // Apply value -> w - value to every remaining block. mul = -mul; add = w - add; totalArea = K; // The popped high-valued suffix becomes one zero-valued suffix. appendLogicalBackZero(highLength); return {I, K}; } }; int main() { FastInput input; int N; if (!input.readInt(N)) return 0; vector<int> w(N); vector<int> c(N - 1); for (int& x : w) input.readInt(x); int maxCost = 0; for (int& x : c) { input.readInt(x); maxCost = max(maxCost, x); } // h_1(x) = w_1. Store enough length for every coordinate queried. SlopeDeque slope(w[0], maxCost); // base = H_j(0) for the current prefix. int64 base = 0; string output; output.reserve(static_cast<size_t>(N - 1) * 22); for (int i = 1; i < N; ++i) { auto [I, K] = slope.transition(c[i - 1], w[i]); const int64 answer = base + I + K; output += to_string(answer); output.push_back('\n'); base += I; } fwrite(output.data(), 1, output.size(), stdout); return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; /* Represents the increasing part of Q. Q consists of: 1. A flat initial segment [0, r], whose value is maintained separately. 2. A sequence of positive-slope segments with nondecreasing slopes. 3. An infinite segment with slope infiniteSlope. The finite segments are stored in a deque. Reversal and the transformation s -> c-s are performed lazily. */ class IncreasingPart { private: struct Segment { int64 len; int64 rawSlope; }; deque<Segment> segments; bool reversed = false; // Actual slope of a segment is mul * rawSlope + add, where mul is +/-1. int64 mul = 1; int64 add = 0; int64 lengthSum = 0; int64 areaSum = 0; int64 actualSlope(const Segment& s) const { return mul * s.rawSlope + add; } Segment& logicalBack() { return reversed ? segments.front() : segments.back(); } void popBack() { Segment s = logicalBack(); int64 slope = actualSlope(s); if (reversed) { segments.pop_front(); } else { segments.pop_back(); } lengthSum -= s.len; areaSum -= slope * s.len; } void trimBack(int64 newLength) { Segment& s = logicalBack(); int64 slope = actualSlope(s); int64 removed = s.len - newLength; s.len = newLength; lengthSum -= removed; areaSum -= slope * removed; } void pushBack(int64 slope, int64 len) { // slope = mul * rawSlope + add, and mul^2 = 1. int64 rawSlope = (slope - add) * mul; Segment s{len, rawSlope}; if (reversed) { segments.push_front(s); } else { segments.push_back(s); } lengthSum += len; areaSum += slope * len; } void trimToPrefix(int64 targetLength) { while (lengthSum > targetLength) { Segment s = logicalBack(); int64 excess = lengthSum - targetLength; if (s.len <= excess) { popBack(); } else { trimBack(s.len - excess); } } } public: int64 infiniteSlope = 0; void reset(int64 newInfiniteSlope) { segments.clear(); reversed = false; mul = 1; add = 0; lengthSum = 0; areaSum = 0; infiniteSlope = newInfiniteSlope; } /* Retain only the first wantedLength units of the increasing part. If necessary, a finite portion of the infinite tail is included. Returns the area (integral of the slope) of that prefix. */ int64 takePrefix(int64 wantedLength) { int64 oldFiniteLength = lengthSum; int64 keep = min(wantedLength, oldFiniteLength); trimToPrefix(keep); if (wantedLength > oldFiniteLength) { pushBack(infiniteSlope, wantedLength - oldFiniteLength); } return areaSum; } /* Slopes are nondecreasing, so all slopes >= threshold form a suffix. */ void removeSuffixWithSlopeAtLeast(int64 threshold) { while (lengthSum > 0) { Segment s = logicalBack(); if (actualSlope(s) < threshold) { break; } popBack(); } } /* Reverse the segment order and replace every slope s by c-s. */ void reverseAndComplement(int64 c) { areaSum = static_cast<int64>( static_cast<__int128>(c) * lengthSum - areaSum ); reversed = !reversed; mul = -mul; add = c - add; } int64 length() const { return lengthSum; } int64 area() const { return areaSum; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int64> w(N + 1); vector<int64> c(N + 1); for (int i = 1; i <= N; ++i) { cin >> w[i]; } for (int i = 1; i < N; ++i) { cin >> c[i]; } /* Q_1(z) = c_1 * max(z, w_1). Thus its flat part has: length = w_1, value = c_1 * w_1, followed by an infinite segment of slope c_1. */ IncreasingPart increasing; int64 flatLength = w[1]; int64 flatValue = c[1] * w[1]; increasing.infiniteSlope = c[1]; for (int i = 2; i <= N; ++i) { int64 answer; if (w[i] <= flatLength) { answer = flatValue; if (i < N) { /* P_i(x) = c_i*x + Q_{i-1}(0) for every x >= 0, so Q_i(z) = Q_{i-1}(0) + c_i*z. */ increasing.reset(c[i]); flatLength = 0; } } else { int64 d = w[i] - flatLength; // Q_{i-1}(w_i): evaluate the first d units of its increasing part. int64 prefixArea = increasing.takePrefix(d); answer = flatValue + prefixArea; if (i < N) { int64 cost = c[i]; /* Keep only the portions whose old slope is < cost. The removed portions become the new flat part. */ increasing.removeSuffixWithSlopeAtLeast(cost); int64 lowLength = increasing.length(); int64 lowArea = increasing.area(); int64 highLength = d - lowLength; int64 newFlatValue = static_cast<int64>( static_cast<__int128>(flatValue) + lowArea + static_cast<__int128>(cost) * highLength ); increasing.reverseAndComplement(cost); increasing.infiniteSlope = cost; flatLength = highLength; flatValue = newFlatValue; } } cout << answer << '\n'; } return 0; }