题面
来源:USACO 官网。模型收到的是同一份题面的文字。
**Note: The time limit for this problem is 3s, 1.5x the default. The memory limit for this problem is 512MB, twice the default.**
Bessie is taking an $N$-question true or false test ($1\le N\le 2\cdot 10^5$). For the $i$-th question, she will gain $a_i$ points if she gets it correct, lose $b_i$ points if she gets it incorrect, or remain even if she does not answer it ($0<a_i,b_i\le 10^9$).
Bessie knows all the answers because she is a smart cow, but worries that Elsie (who is administering the test) will retroactively change up to $k$ of the questions after the test such that Bessie does not get those questions correct.
Given $Q$ ($1\le Q\le N+1$) candidate values of $k$ ($0\le k\le N$), determine the number of points Bessie can guarantee for each $k$, given that she must answer at least $k$ questions.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$ and $Q$.The next $N$ lines each contain $a_i$ and $b_i$.
The next $Q$ lines each contain a value of $k$. No value of $k$ appears more than once.
OUTPUT FORMAT (print output to the terminal / stdout):
The answer for each $k$ on a separate line.SAMPLE INPUT:
2 3 3 1 4 2 2 1 0
SAMPLE OUTPUT:
-3 1 7
For each value of $k$, it is optimal for Bessie to answer all of the questions.
SCORING:
- Inputs 2-4: $N\le 100$
- Inputs 5-7: $Q\le 10$, $N\le 2\cdot 10^5$
- Inputs 7-20: No additional constraints.
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 11.8k平均输出 token(含思考) ↓
- 26.8输出速度 tokens/s ↑
- 464 秒延迟 ↓
- 0.34×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
Kimi K3.1 Pro(high)Kimi high
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 11.8k平均输出 token(含思考) ↓
- 26.8输出速度 tokens/s ↑
- 464 秒延迟 ↓
- 0.34×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 39.7k平均输出 token(含思考) ↓
- 129.8输出速度 tokens/s ↑
- 301 秒延迟 ↓
- 0.22×CPU 时间 / 官方解 ↓
- 更优 2、同阶 1时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 47.1k平均输出 token(含思考) ↓
- 162.5输出速度 tokens/s ↑
- 269 秒延迟 ↓
- 0.32×CPU 时间 / 官方解 ↓
- 更优 2、同阶 1时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { int l = 0, r = 0, cnt = 0; ll sum = 0; }; int N, M; vector<ll> bValues; vector<Node> seg; int update(int prev, int lo, int hi, int pos, ll value) { int cur = (int)seg.size(); seg.push_back(seg[prev]); seg[cur].cnt++; seg[cur].sum += value; if (lo < hi) { int mid = (lo + hi) >> 1; if (pos <= mid) { seg[cur].l = update(seg[prev].l, lo, mid, pos, value); } else { seg[cur].r = update(seg[prev].r, mid + 1, hi, pos, value); } } return cur; } ll querySumOfKSmallest(int node, int k) { ll result = 0; int lo = 1, hi = M; while (lo < hi) { int mid = (lo + hi) >> 1; int left = seg[node].l; int leftCount = seg[left].cnt; if (k <= leftCount) { node = left; hi = mid; } else { result += seg[left].sum; k -= leftCount; node = seg[node].r; lo = mid + 1; } } result += 1LL * k * bValues[lo - 1]; return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> N >> Q; vector<ll> a(N), b(N); for (int i = 0; i < N; i++) { cin >> a[i] >> b[i]; } vector<int> queries(Q); for (int &k : queries) { cin >> k; } vector<int> order(N); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { return a[i] + b[i] > a[j] + b[j]; }); bValues = b; sort(bValues.begin(), bValues.end()); bValues.erase(unique(bValues.begin(), bValues.end()), bValues.end()); M = (int)bValues.size(); seg.reserve((size_t)N * 20 + 5); seg.push_back(Node()); vector<int> roots(N + 1, 0); vector<ll> prefixA(N + 1, 0), prefixB(N + 1, 0); for (int r = 1; r <= N; r++) { int id = order[r - 1]; int pos = (int)(lower_bound(bValues.begin(), bValues.end(), b[id]) - bValues.begin()) + 1; roots[r] = update(roots[r - 1], 1, M, pos, b[id]); prefixA[r] = prefixA[r - 1] + a[id]; prefixB[r] = prefixB[r - 1] + b[id]; } vector<ll> suffixA(N + 1); for (int r = 0; r <= N; r++) { suffixA[r] = prefixA[N] - prefixA[r]; } vector<ll> answer(N + 1, 0); function<void(int, int, int, int)> solve = [&](int l, int r, int optL, int optR) { if (l > r) return; int mid = (l + r) >> 1; int start = max(mid, optL); int bestPos = start; ll bestValue = numeric_limits<ll>::min(); for (int pos = start; pos <= optR; pos++) { ll penalty; if (mid == 0) { penalty = 0; } else if (mid == pos) { penalty = prefixB[pos]; } else { penalty = querySumOfKSmallest(roots[pos], mid); } ll value = suffixA[pos] - penalty; if (value > bestValue) { bestValue = value; bestPos = pos; } } answer[mid] = bestValue; if (l < mid) { solve(l, mid - 1, optL, bestPos); } if (mid < r) { solve(mid + 1, r, bestPos, optR); } }; solve(0, N, 0, N); for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Node { int left = 0, right = 0, count = 0; int64 sum = 0; }; class PersistentSegmentTree { public: vector<Node> tree; vector<int64> values; explicit PersistentSegmentTree(const vector<int64>& vals, int n) : values(vals) { tree.reserve(static_cast<size_t>(n) * 20 + 1); tree.push_back(Node{}); } int update(int previous, int l, int r, int position, int64 value) { int current = static_cast<int>(tree.size()); tree.push_back(tree[previous]); tree[current].count++; tree[current].sum += value; if (l != r) { int mid = (l + r) / 2; if (position <= mid) { tree[current].left = update(tree[previous].left, l, mid, position, value); } else { tree[current].right = update(tree[previous].right, mid + 1, r, position, value); } } return current; } int64 sumOfKSmallest(int node, int k) const { if (k == 0) return 0; int l = 0; int r = static_cast<int>(values.size()) - 1; int64 result = 0; while (l < r) { if (tree[node].count == k) { return result + tree[node].sum; } int leftNode = tree[node].left; int leftCount = tree[leftNode].count; int mid = (l + r) / 2; if (k <= leftCount) { node = leftNode; r = mid; } else { result += tree[leftNode].sum; k -= leftCount; node = tree[node].right; l = mid + 1; } } return result + static_cast<int64>(k) * values[l]; } }; struct Question { int64 a, b, c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Question> questions(N); vector<int64> bValues; bValues.reserve(N); for (auto& question : questions) { cin >> question.a >> question.b; question.c = question.a + question.b; bValues.push_back(question.b); } sort(questions.begin(), questions.end(), [](const Question& x, const Question& y) { return x.c > y.c; }); sort(bValues.begin(), bValues.end()); bValues.erase(unique(bValues.begin(), bValues.end()), bValues.end()); PersistentSegmentTree pst(bValues, N); vector<int> roots(N + 1, 0); for (int i = 1; i <= N; i++) { int64 b = questions[i - 1].b; int position = static_cast<int>( lower_bound(bValues.begin(), bValues.end(), b) - bValues.begin()); roots[i] = pst.update(roots[i - 1], 0, static_cast<int>(bValues.size()) - 1, position, b); } // suffixA[p] is the sum of a_i at positions p+1 through N. vector<int64> suffixA(N + 1, 0); for (int p = N - 1; p >= 0; p--) { suffixA[p] = suffixA[p + 1] + questions[p].a; } vector<int64> answer(N + 1, 0); answer[0] = suffixA[0]; function<void(int, int, int, int)> divideAndConquer = [&](int kLeft, int kRight, int pLeft, int pRight) { if (kLeft > kRight) return; int kMid = (kLeft + kRight) / 2; int start = max(kMid, pLeft); int bestP = start; int64 bestValue = numeric_limits<int64>::min(); for (int p = start; p <= pRight; p++) { int64 value = suffixA[p] - pst.sumOfKSmallest(roots[p], kMid); if (value > bestValue) { bestValue = value; bestP = p; } } answer[kMid] = bestValue; divideAndConquer(kLeft, kMid - 1, pLeft, bestP); divideAndConquer(kMid + 1, kRight, bestP, pRight); }; divideAndConquer(1, N, 1, N); while (Q--) { int k; cin >> k; cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Item { ll w, a, b; int cb; }; struct Node { int l = 0, r = 0, cnt = 0; ll sum = 0; }; struct PersistentSegTree { vector<Node> tr; explicit PersistentSegTree(int n) { tr.reserve(n * 20 + 5); tr.push_back(Node{}); } int update(int prev, int l, int r, int pos, ll val) { int u = (int)tr.size(); tr.push_back(tr[prev]); tr[u].cnt++; tr[u].sum += val; if (l != r) { int mid = (l + r) >> 1; if (pos <= mid) { tr[u].l = update(tr[prev].l, l, mid, pos, val); } else { tr[u].r = update(tr[prev].r, mid + 1, r, pos, val); } } return u; } ll sumSmallest(int u, int k, const vector<ll>& coords) const { ll result = 0; int l = 0, r = (int)coords.size() - 1; while (l < r) { if (k == tr[u].cnt) return result + tr[u].sum; int mid = (l + r) >> 1; int left = tr[u].l; int leftCnt = tr[left].cnt; if (k <= leftCnt) { u = left; r = mid; } else { k -= leftCnt; result += tr[left].sum; u = tr[u].r; l = mid + 1; } } return result + 1LL * k * coords[l]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); vector<ll> coords; coords.reserve(N); for (int i = 0; i < N; i++) { ll a, b; cin >> a >> b; items[i] = {a + b, a, b, 0}; coords.push_back(b); } vector<int> queries(Q); for (int& k : queries) cin >> k; sort(coords.begin(), coords.end()); coords.erase(unique(coords.begin(), coords.end()), coords.end()); for (Item& it : items) { it.cb = (int)(lower_bound(coords.begin(), coords.end(), it.b) - coords.begin()); } sort(items.begin(), items.end(), [](const Item& x, const Item& y) { return x.w > y.w; }); int M = (int)coords.size(); PersistentSegTree pst(N); vector<int> roots(N + 1, 0); vector<ll> prefixA(N + 1, 0); for (int i = 1; i <= N; i++) { const Item& it = items[i - 1]; roots[i] = pst.update(roots[i - 1], 0, M - 1, it.cb, it.b); prefixA[i] = prefixA[i - 1] + it.a; } const ll totalA = prefixA[N]; vector<ll> answer(N + 1, 0); answer[0] = totalA; auto valueAt = [&](int r, int k) -> ll { ll safe = totalA - prefixA[r]; ll flipped = pst.sumSmallest(roots[r], k, coords); return safe - flipped; }; vector<int> positiveK; positiveK.reserve(Q); for (int k : queries) { if (k > 0) positiveK.push_back(k); } sort(positiveK.begin(), positiveK.end()); function<void(int, int, int, int)> solve = [&](int ql, int qr, int rl, int rr) { if (ql > qr) return; int qm = (ql + qr) >> 1; int k = positiveK[qm]; int lo = max(rl, k); int bestR = lo; ll bestValue = valueAt(lo, k); for (int r = lo + 1; r <= rr; r++) { ll cur = valueAt(r, k); if (cur > bestValue) { bestValue = cur; bestR = r; } } answer[k] = bestValue; solve(ql, qm - 1, rl, bestR); solve(qm + 1, qr, bestR, rr); }; if (!positiveK.empty()) { solve(0, (int)positiveK.size() - 1, 1, N); } for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Node { int l = 0, r = 0, cnt = 0; ll sum = 0; }; int N, M; vector<ll> bValues; vector<Node> seg; int update(int prev, int lo, int hi, int pos, ll value) { int cur = (int)seg.size(); seg.push_back(seg[prev]); seg[cur].cnt++; seg[cur].sum += value; if (lo < hi) { int mid = (lo + hi) >> 1; if (pos <= mid) { seg[cur].l = update(seg[prev].l, lo, mid, pos, value); } else { seg[cur].r = update(seg[prev].r, mid + 1, hi, pos, value); } } return cur; } ll querySumOfKSmallest(int node, int k) { ll result = 0; int lo = 1, hi = M; while (lo < hi) { int mid = (lo + hi) >> 1; int left = seg[node].l; int leftCount = seg[left].cnt; if (k <= leftCount) { node = left; hi = mid; } else { result += seg[left].sum; k -= leftCount; node = seg[node].r; lo = mid + 1; } } result += 1LL * k * bValues[lo - 1]; return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> N >> Q; vector<ll> a(N), b(N); for (int i = 0; i < N; i++) { cin >> a[i] >> b[i]; } vector<int> queries(Q); for (int &k : queries) { cin >> k; } vector<int> order(N); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { return a[i] + b[i] > a[j] + b[j]; }); bValues = b; sort(bValues.begin(), bValues.end()); bValues.erase(unique(bValues.begin(), bValues.end()), bValues.end()); M = (int)bValues.size(); seg.reserve((size_t)N * 20 + 5); seg.push_back(Node()); vector<int> roots(N + 1, 0); vector<ll> prefixA(N + 1, 0), prefixB(N + 1, 0); for (int r = 1; r <= N; r++) { int id = order[r - 1]; int pos = (int)(lower_bound(bValues.begin(), bValues.end(), b[id]) - bValues.begin()) + 1; roots[r] = update(roots[r - 1], 1, M, pos, b[id]); prefixA[r] = prefixA[r - 1] + a[id]; prefixB[r] = prefixB[r - 1] + b[id]; } vector<ll> suffixA(N + 1); for (int r = 0; r <= N; r++) { suffixA[r] = prefixA[N] - prefixA[r]; } vector<ll> answer(N + 1, 0); function<void(int, int, int, int)> solve = [&](int l, int r, int optL, int optR) { if (l > r) return; int mid = (l + r) >> 1; int start = max(mid, optL); int bestPos = start; ll bestValue = numeric_limits<ll>::min(); for (int pos = start; pos <= optR; pos++) { ll penalty; if (mid == 0) { penalty = 0; } else if (mid == pos) { penalty = prefixB[pos]; } else { penalty = querySumOfKSmallest(roots[pos], mid); } ll value = suffixA[pos] - penalty; if (value > bestValue) { bestValue = value; bestPos = pos; } } answer[mid] = bestValue; if (l < mid) { solve(l, mid - 1, optL, bestPos); } if (mid < r) { solve(mid + 1, r, bestPos, optR); } }; solve(0, N, 0, N); for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Node { int left = 0, right = 0, count = 0; int64 sum = 0; }; class PersistentSegmentTree { public: vector<Node> tree; vector<int64> values; explicit PersistentSegmentTree(const vector<int64>& vals, int n) : values(vals) { tree.reserve(static_cast<size_t>(n) * 20 + 1); tree.push_back(Node{}); } int update(int previous, int l, int r, int position, int64 value) { int current = static_cast<int>(tree.size()); tree.push_back(tree[previous]); tree[current].count++; tree[current].sum += value; if (l != r) { int mid = (l + r) / 2; if (position <= mid) { tree[current].left = update(tree[previous].left, l, mid, position, value); } else { tree[current].right = update(tree[previous].right, mid + 1, r, position, value); } } return current; } int64 sumOfKSmallest(int node, int k) const { if (k == 0) return 0; int l = 0; int r = static_cast<int>(values.size()) - 1; int64 result = 0; while (l < r) { if (tree[node].count == k) { return result + tree[node].sum; } int leftNode = tree[node].left; int leftCount = tree[leftNode].count; int mid = (l + r) / 2; if (k <= leftCount) { node = leftNode; r = mid; } else { result += tree[leftNode].sum; k -= leftCount; node = tree[node].right; l = mid + 1; } } return result + static_cast<int64>(k) * values[l]; } }; struct Question { int64 a, b, c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Question> questions(N); vector<int64> bValues; bValues.reserve(N); for (auto& question : questions) { cin >> question.a >> question.b; question.c = question.a + question.b; bValues.push_back(question.b); } sort(questions.begin(), questions.end(), [](const Question& x, const Question& y) { return x.c > y.c; }); sort(bValues.begin(), bValues.end()); bValues.erase(unique(bValues.begin(), bValues.end()), bValues.end()); PersistentSegmentTree pst(bValues, N); vector<int> roots(N + 1, 0); for (int i = 1; i <= N; i++) { int64 b = questions[i - 1].b; int position = static_cast<int>( lower_bound(bValues.begin(), bValues.end(), b) - bValues.begin()); roots[i] = pst.update(roots[i - 1], 0, static_cast<int>(bValues.size()) - 1, position, b); } // suffixA[p] is the sum of a_i at positions p+1 through N. vector<int64> suffixA(N + 1, 0); for (int p = N - 1; p >= 0; p--) { suffixA[p] = suffixA[p + 1] + questions[p].a; } vector<int64> answer(N + 1, 0); answer[0] = suffixA[0]; function<void(int, int, int, int)> divideAndConquer = [&](int kLeft, int kRight, int pLeft, int pRight) { if (kLeft > kRight) return; int kMid = (kLeft + kRight) / 2; int start = max(kMid, pLeft); int bestP = start; int64 bestValue = numeric_limits<int64>::min(); for (int p = start; p <= pRight; p++) { int64 value = suffixA[p] - pst.sumOfKSmallest(roots[p], kMid); if (value > bestValue) { bestValue = value; bestP = p; } } answer[kMid] = bestValue; divideAndConquer(kLeft, kMid - 1, pLeft, bestP); divideAndConquer(kMid + 1, kRight, bestP, pRight); }; divideAndConquer(1, N, 1, N); while (Q--) { int k; cin >> k; cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Item { ll w, a, b; int cb; }; struct Node { int l = 0, r = 0, cnt = 0; ll sum = 0; }; struct PersistentSegTree { vector<Node> tr; explicit PersistentSegTree(int n) { tr.reserve(n * 20 + 5); tr.push_back(Node{}); } int update(int prev, int l, int r, int pos, ll val) { int u = (int)tr.size(); tr.push_back(tr[prev]); tr[u].cnt++; tr[u].sum += val; if (l != r) { int mid = (l + r) >> 1; if (pos <= mid) { tr[u].l = update(tr[prev].l, l, mid, pos, val); } else { tr[u].r = update(tr[prev].r, mid + 1, r, pos, val); } } return u; } ll sumSmallest(int u, int k, const vector<ll>& coords) const { ll result = 0; int l = 0, r = (int)coords.size() - 1; while (l < r) { if (k == tr[u].cnt) return result + tr[u].sum; int mid = (l + r) >> 1; int left = tr[u].l; int leftCnt = tr[left].cnt; if (k <= leftCnt) { u = left; r = mid; } else { k -= leftCnt; result += tr[left].sum; u = tr[u].r; l = mid + 1; } } return result + 1LL * k * coords[l]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); vector<ll> coords; coords.reserve(N); for (int i = 0; i < N; i++) { ll a, b; cin >> a >> b; items[i] = {a + b, a, b, 0}; coords.push_back(b); } vector<int> queries(Q); for (int& k : queries) cin >> k; sort(coords.begin(), coords.end()); coords.erase(unique(coords.begin(), coords.end()), coords.end()); for (Item& it : items) { it.cb = (int)(lower_bound(coords.begin(), coords.end(), it.b) - coords.begin()); } sort(items.begin(), items.end(), [](const Item& x, const Item& y) { return x.w > y.w; }); int M = (int)coords.size(); PersistentSegTree pst(N); vector<int> roots(N + 1, 0); vector<ll> prefixA(N + 1, 0); for (int i = 1; i <= N; i++) { const Item& it = items[i - 1]; roots[i] = pst.update(roots[i - 1], 0, M - 1, it.cb, it.b); prefixA[i] = prefixA[i - 1] + it.a; } const ll totalA = prefixA[N]; vector<ll> answer(N + 1, 0); answer[0] = totalA; auto valueAt = [&](int r, int k) -> ll { ll safe = totalA - prefixA[r]; ll flipped = pst.sumSmallest(roots[r], k, coords); return safe - flipped; }; vector<int> positiveK; positiveK.reserve(Q); for (int k : queries) { if (k > 0) positiveK.push_back(k); } sort(positiveK.begin(), positiveK.end()); function<void(int, int, int, int)> solve = [&](int ql, int qr, int rl, int rr) { if (ql > qr) return; int qm = (ql + qr) >> 1; int k = positiveK[qm]; int lo = max(rl, k); int bestR = lo; ll bestValue = valueAt(lo, k); for (int r = lo + 1; r <= rr; r++) { ll cur = valueAt(r, k); if (cur > bestValue) { bestValue = cur; bestR = r; } } answer[k] = bestValue; solve(ql, qm - 1, rl, bestR); solve(qm + 1, qr, bestR, rr); }; if (!positiveK.empty()) { solve(0, (int)positiveK.size() - 1, 1, N); } for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long NEG = -(1LL << 60); struct Item { long long a, b, c; }; struct Group { long long t; long long P; int E; int highRoot; int rankT; int maxK; }; struct Node { int lc = 0, rc = 0, cnt = 0; long long sum = 0; }; vector<Node> seg; vector<long long> coord; int update(int prev, int l, int r, int pos, long long val) { int cur = (int)seg.size(); seg.push_back(seg[prev]); seg[cur].cnt++; seg[cur].sum += val; if (l == r) return cur; int mid = (l + r) >> 1; if (pos <= mid) { seg[cur].lc = update(seg[prev].lc, l, mid, pos, val); } else { seg[cur].rc = update(seg[prev].rc, mid + 1, r, pos, val); } return cur; } // Sum of k smallest elements in: // persistent tree rooted at node, plus q extra copies of value with rank qrank. long long queryKSmallest(int node, int l, int r, int q, int qrank, int k) { if (k <= 0) return 0; if (l == r) { return 1LL * k * coord[l - 1]; } int mid = (l + r) >> 1; int lnode = seg[node].lc; int extraLeft = 0; if (q > 0 && qrank >= l && qrank <= mid) extraLeft = q; int leftCnt = seg[lnode].cnt + extraLeft; if (k <= leftCnt) { int nq = extraLeft ? q : 0; return queryKSmallest(lnode, l, mid, nq, qrank, k); } long long leftSum = seg[lnode].sum; if (extraLeft) { leftSum += 1LL * extraLeft * coord[qrank - 1]; } int extraRight = 0; if (q > 0 && qrank > mid && qrank <= r) extraRight = q; int rnode = seg[node].rc; return leftSum + queryKSmallest(rnode, mid + 1, r, extraRight, qrank, k - leftCnt); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); vector<long long> vals; vals.reserve(2 * N); long long sumA = 0; for (int i = 0; i < N; i++) { long long a, b; cin >> a >> b; long long c = a + b; items[i] = {a, b, c}; vals.push_back(b); vals.push_back(c); sumA += a; } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); coord = vals; int M = (int)vals.size(); sort(items.begin(), items.end(), [](const Item& x, const Item& y) { return x.c < y.c; }); vector<long long> pref(N + 1, 0); for (int i = 0; i < N; i++) { pref[i + 1] = pref[i] + items[i].a; } seg.reserve((N + 5) * 22); seg.push_back(Node()); vector<int> root(N + 1, 0); root[N] = 0; for (int i = N - 1; i >= 0; i--) { int pos = int(lower_bound(vals.begin(), vals.end(), items[i].b) - vals.begin()) + 1; root[i] = update(root[i + 1], 1, M, pos, items[i].b); } vector<Group> groups; groups.reserve(N); int i = 0; while (i < N) { int j = i; while (j < N && items[j].c == items[i].c) j++; long long t = items[i].c; int E = j - i; long long P = pref[j]; int highRoot = root[j]; int rankT = int(lower_bound(vals.begin(), vals.end(), t) - vals.begin()) + 1; int highCount = N - j; int maxK = highCount + E; groups.push_back({t, P, E, highRoot, rankT, maxK}); i = j; } int G = (int)groups.size(); vector<long long> ans(N + 1, NEG); ans[0] = sumA; auto eval = [&](int g, int k) -> long long { const Group& gr = groups[g]; if (k < 1 || k > gr.maxK) return NEG; int r = k - 1; long long sumSmall = queryKSmallest( gr.highRoot, 1, M, gr.E - 1, gr.rankT, r ); return gr.P - gr.t - sumSmall; }; function<void(int, int, int, int)> solve = [&](int kL, int kR, int gL, int gR) { if (kL > kR) return; int mid = (kL + kR) >> 1; long long bestVal = NEG; int bestG = gL; for (int g = gL; g <= gR; g++) { long long cur = eval(g, mid); if (cur > bestVal) { bestVal = cur; bestG = g; } } ans[mid] = bestVal; // As k increases, the optimal group index is non-increasing. solve(kL, mid - 1, bestG, gR); solve(mid + 1, kR, gL, bestG); }; if (N >= 1) { solve(1, N, 0, G - 1); } while (Q--) { int k; cin >> k; cout << ans[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct PersistentSegTree { int M, tot; vector<int> lc, rc, cnt; vector<long long> sum, coord; PersistentSegTree() {} PersistentSegTree(int m, int maxNodes, const vector<long long>& c) : M(m), tot(0), lc(maxNodes), rc(maxNodes), cnt(maxNodes), sum(maxNodes), coord(c) {} int update(int prev, int l, int r, int pos, long long val) { int cur = ++tot; lc[cur] = lc[prev]; rc[cur] = rc[prev]; cnt[cur] = cnt[prev] + 1; sum[cur] = sum[prev] + val; if (l == r) return cur; int mid = (l + r) >> 1; if (pos <= mid) { lc[cur] = update(lc[prev], l, mid, pos, val); } else { rc[cur] = update(rc[prev], mid + 1, r, pos, val); } return cur; } long long query(int node, int k) { if (k <= 0 || node == 0) return 0; int l = 1, r = M; long long res = 0; while (l < r) { if (cnt[node] <= k) { res += sum[node]; return res; } int mid = (l + r) >> 1; int left = lc[node]; if (cnt[left] >= k) { node = left; r = mid; } else { res += sum[left]; k -= cnt[left]; node = rc[node]; l = mid + 1; } } if (k > 0 && node != 0) { res += coord[l] * 1LL * k; } return res; } }; struct Item { long long a, b, c; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); for (int i = 0; i < N; i++) { cin >> items[i].a >> items[i].b; items[i].c = items[i].a + items[i].b; } sort(items.begin(), items.end(), [](const Item& x, const Item& y) { if (x.c != y.c) return x.c > y.c; if (x.b != y.b) return x.b < y.b; return x.a > y.a; }); vector<long long> a(N), b(N), sufA(N + 1, 0); for (int i = 0; i < N; i++) { a[i] = items[i].a; b[i] = items[i].b; } for (int i = N - 1; i >= 0; i--) { sufA[i] = sufA[i + 1] + a[i]; } vector<long long> comp = b; sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); int M = (int)comp.size(); vector<long long> coord(M + 1); for (int i = 0; i < M; i++) { coord[i + 1] = comp[i]; } int maxNodes = N * 20 + 5; PersistentSegTree pst(M, maxNodes, coord); vector<int> roots(N + 1, 0); for (int i = 0; i < N; i++) { int pos = int(lower_bound(comp.begin(), comp.end(), b[i]) - comp.begin()) + 1; roots[i + 1] = pst.update(roots[i], 1, M, pos, b[i]); } vector<long long> ans(N + 1); ans[0] = sufA[0]; function<void(int, int, int, int)> solve = [&](int l, int r, int optL, int optR) { if (l > r) return; int mid = (l + r) >> 1; int need = mid - 1; int start = max(optL, need); int end = optR; long long bestVal = LLONG_MIN / 4; int bestIdx = start; for (int j = start; j <= end; j++) { long long penalty = pst.query(roots[j], need); long long val = sufA[j + 1] - b[j] - penalty; if (val > bestVal) { bestVal = val; bestIdx = j; } } ans[mid] = bestVal; solve(l, mid - 1, optL, bestIdx); solve(mid + 1, r, bestIdx, optR); }; solve(1, N, 0, N - 1); for (int i = 0; i < Q; i++) { int k; cin >> k; cout << ans[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXNODE = 5000005; int lc[MAXNODE], rc[MAXNODE], cntArr[MAXNODE]; long long sumArr[MAXNODE]; int ptrNode = 0; vector<long long> valsGlobal; int Mglobal; int update(int prev, int l, int r, int pos, long long val) { int cur = ++ptrNode; lc[cur] = lc[prev]; rc[cur] = rc[prev]; cntArr[cur] = cntArr[prev] + 1; sumArr[cur] = sumArr[prev] + val; if (l != r) { int mid = (l + r) >> 1; if (pos <= mid) lc[cur] = update(lc[prev], l, mid, pos, val); else rc[cur] = update(rc[prev], mid + 1, r, pos, val); } return cur; } long long querySum(int node, int l, int r, int t, int extraPos, int extraCnt) { if (t <= 0) return 0; if (l == r) { return 1LL * t * valsGlobal[l]; } int mid = (l + r) >> 1; int lnode = lc[node]; int leftCnt = cntArr[lnode]; long long leftSum = sumArr[lnode]; int eLeft = 0, eRight = 0; if (extraCnt > 0) { if (extraPos <= mid) { eLeft = extraCnt; leftCnt += extraCnt; leftSum += 1LL * extraCnt * valsGlobal[extraPos]; } else { eRight = extraCnt; } } if (t <= leftCnt) { return querySum(lnode, l, mid, t, extraPos, eLeft); } else { int rnode = rc[node]; return leftSum + querySum(rnode, mid + 1, r, t - leftCnt, extraPos, eRight); } } struct Item { long long a, b, c; }; struct Group { long long c; long long base; int g; int rootBefore; int extraPos; int extraCnt; int cntGe; }; vector<Group> groups; vector<long long> ans; int Nglobal; long long totalA; inline long long evaluate(int idx, int k) { const Group &gr = groups[idx]; int t = k - 1; long long cost = 0; if (t > 0) { cost = querySum( gr.rootBefore, 0, Mglobal - 1, t, gr.extraPos, gr.extraCnt ); } return gr.base - gr.c - cost; } void solveDC(int lk, int rk, int optL, int optR) { if (lk > rk) return; int mid = (lk + rk) >> 1; int best = -1; long long bestVal = LLONG_MIN / 4; for (int idx = optL; idx <= optR; idx++) { if (groups[idx].cntGe < mid) continue; long long val = evaluate(idx, mid); if (val > bestVal) { bestVal = val; best = idx; } } ans[mid] = bestVal; if (lk <= mid - 1) solveDC(lk, mid - 1, optL, best); if (mid + 1 <= rk) solveDC(mid + 1, rk, best, optR); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); vector<long long> vals; vals.reserve(2 * N); totalA = 0; for (int i = 0; i < N; i++) { long long a, b; cin >> a >> b; long long c = a + b; items[i] = {a, b, c}; totalA += a; vals.push_back(b); vals.push_back(c); } vector<int> queries(Q); for (int i = 0; i < Q; i++) { cin >> queries[i]; } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); valsGlobal = vals; Mglobal = (int)vals.size(); sort(items.begin(), items.end(), [](const Item &x, const Item &y) { return x.c > y.c; }); groups.reserve(N); int rootCurrent = 0; long long sumAHigh = 0; int processed = 0; for (int i = 0; i < N; ) { int j = i; long long c = items[i].c; long long sumAg = 0; while (j < N && items[j].c == c) { sumAg += items[j].a; j++; } int g = j - i; Group gr; gr.c = c; gr.g = g; gr.rootBefore = rootCurrent; gr.extraPos = (int)(lower_bound(vals.begin(), vals.end(), c) - vals.begin()); gr.extraCnt = g - 1; gr.base = totalA - sumAHigh; gr.cntGe = processed + g; groups.push_back(gr); for (int t = i; t < j; t++) { long long b = items[t].b; int pos = (int)(lower_bound(vals.begin(), vals.end(), b) - vals.begin()); rootCurrent = update(rootCurrent, 0, Mglobal - 1, pos, b); } sumAHigh += sumAg; processed += g; i = j; } Nglobal = N; ans.assign(N + 1, 0); ans[0] = totalA; if (N >= 1) { solveDC(1, N, 0, (int)groups.size() - 1); } for (int k : queries) { cout << ans[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Item { int a, b; int64 danger; }; class PersistentSegmentTree { struct Node { int left = 0; int right = 0; int count = 0; int64 sum = 0; }; vector<Node> tree; vector<int> values; int m; int insertImpl(int previous, int lo, int hi, int position, int value) { int current = (int)tree.size(); Node copy = tree[previous]; tree.push_back(copy); ++tree[current].count; tree[current].sum += value; if (lo != hi) { int mid = (lo + hi) / 2; if (position <= mid) { int child = insertImpl(tree[previous].left, lo, mid, position, value); tree[current].left = child; } else { int child = insertImpl(tree[previous].right, mid + 1, hi, position, value); tree[current].right = child; } } return current; } public: PersistentSegmentTree(const vector<int>& compressedValues, int n) : values(compressedValues), m((int)compressedValues.size()) { int height = 0; while ((1LL << height) < m) ++height; tree.reserve(1 + (size_t)n * (height + 1)); tree.push_back(Node{}); } int insert(int previous, int position, int value) { return insertImpl(previous, 0, m - 1, position, value); } // Sum of the k smallest values in this version. int64 sumSmallest(int root, int k) const { if (k == 0) return 0; int node = root; int lo = 0, hi = m - 1; int64 result = 0; while (lo < hi) { if (tree[node].count == k) { return result + tree[node].sum; } int mid = (lo + hi) / 2; int left = tree[node].left; int leftCount = tree[left].count; if (k <= leftCount) { node = left; hi = mid; } else { result += tree[left].sum; k -= leftCount; node = tree[node].right; lo = mid + 1; } } return result + 1LL * k * values[lo]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); for (auto& item : items) { cin >> item.a >> item.b; item.danger = 1LL * item.a + item.b; } vector<int> queries(Q); for (int& k : queries) cin >> k; sort(items.begin(), items.end(), [](const Item& x, const Item& y) { if (x.danger != y.danger) return x.danger < y.danger; if (x.b != y.b) return x.b < y.b; return x.a < y.a; }); // prefix[p] = sum of a_i among the first p items. vector<int64> prefix(N + 1, 0); for (int i = 0; i < N; ++i) { prefix[i + 1] = prefix[i] + items[i].a; } vector<int> compressedValues; compressedValues.reserve(N); for (const auto& item : items) { compressedValues.push_back(item.b); } sort(compressedValues.begin(), compressedValues.end()); compressedValues.erase( unique(compressedValues.begin(), compressedValues.end()), compressedValues.end() ); vector<int> rank(N); for (int i = 0; i < N; ++i) { rank[i] = lower_bound(compressedValues.begin(), compressedValues.end(), items[i].b) - compressedValues.begin(); } PersistentSegmentTree pst(compressedValues, N); // root[p] contains the b-values of the suffix [p, N). vector<int> root(N + 1, 0); for (int p = N - 1; p >= 0; --p) { root[p] = pst.insert(root[p + 1], rank[p], items[p].b); } const int64 NEG_INF = numeric_limits<int64>::min() / 4; /* Matrix row = k, column c represents cut p = N - c. Columns are therefore in reverse order of cuts. M(k, p) = prefix[p] - sum of the k smallest b-values in suffix [p, N). */ auto getValue = [&](int row, int col) -> int64 { int p = N - col; if (p > N - row) return NEG_INF; return prefix[p] - pst.sumSmallest(root[p], row); }; vector<int64> answer(N + 1); vector<int> optimumColumn(N + 1, -1); // SMAWK for row maxima of a totally monotone matrix. auto smawk = [&](auto&& self, const vector<int>& rows, const vector<int>& columns) -> void { if (rows.empty()) return; vector<int> reduced; reduced.reserve(min(rows.size(), columns.size())); // Column reduction. for (int col : columns) { while (!reduced.empty()) { int row = rows[(int)reduced.size() - 1]; if (getValue(row, col) >= getValue(row, reduced.back())) { reduced.pop_back(); } else { break; } } if (reduced.size() < rows.size()) { reduced.push_back(col); } } vector<int> oddRows; oddRows.reserve(rows.size() / 2); for (int i = 1; i < (int)rows.size(); i += 2) { oddRows.push_back(rows[i]); } self(self, oddRows, reduced); // Interpolate the even-indexed rows. int start = 0; for (int i = 0; i < (int)rows.size(); i += 2) { int stop; if (i + 1 < (int)rows.size()) { int target = optimumColumn[rows[i + 1]]; stop = lower_bound(reduced.begin(), reduced.end(), target) - reduced.begin(); } else { stop = (int)reduced.size() - 1; } int row = rows[i]; int bestColumn = reduced[start]; int64 bestValue = getValue(row, bestColumn); for (int j = start + 1; j <= stop; ++j) { int64 candidate = getValue(row, reduced[j]); if (candidate >= bestValue) { bestValue = candidate; bestColumn = reduced[j]; } } optimumColumn[row] = bestColumn; answer[row] = bestValue; start = stop; } }; vector<int> rows(N + 1), columns(N + 1); iota(rows.begin(), rows.end(), 0); iota(columns.begin(), columns.end(), 0); smawk(smawk, rows, columns); for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Question { int64 a, b, c; }; /* * Wavelet matrix over compressed b-values. * sumSmallest(p, k) returns the sum of the k smallest b-values * among the first p elements. */ class WaveletMatrix { private: int levels; vector<int> values; vector<vector<int>> prefixZeroCount; vector<vector<int64>> prefixZeroSum; vector<int> zeroCount; vector<int64> prefixTotal; public: WaveletMatrix(const vector<int>& coordinates, const vector<int>& data) : values(coordinates) { int n = (int)data.size(); int m = (int)values.size(); levels = 0; while ((1LL << levels) < m) ++levels; vector<int> current(n), next(n); for (int i = 0; i < n; ++i) { current[i] = int(lower_bound(values.begin(), values.end(), data[i]) - values.begin()); } prefixTotal.assign(n + 1, 0); for (int i = 0; i < n; ++i) { prefixTotal[i + 1] = prefixTotal[i] + data[i]; } prefixZeroCount.resize(levels); prefixZeroSum.resize(levels); zeroCount.resize(levels); for (int level = 0; level < levels; ++level) { int bit = levels - 1 - level; prefixZeroCount[level].assign(n + 1, 0); prefixZeroSum[level].assign(n + 1, 0); for (int i = 0; i < n; ++i) { bool isZero = ((current[i] >> bit) & 1) == 0; prefixZeroCount[level][i + 1] = prefixZeroCount[level][i] + (isZero ? 1 : 0); prefixZeroSum[level][i + 1] = prefixZeroSum[level][i] + (isZero ? (int64)values[current[i]] : 0LL); } int totalZero = prefixZeroCount[level][n]; zeroCount[level] = totalZero; int zeroPos = 0; int onePos = totalZero; for (int i = 0; i < n; ++i) { if (((current[i] >> bit) & 1) == 0) { next[zeroPos++] = current[i]; } else { next[onePos++] = current[i]; } } current.swap(next); } } int64 sumSmallest(int prefixLength, int k) const { if (k == 0) return 0; if (k == prefixLength) return prefixTotal[prefixLength]; int left = 0; int right = prefixLength; int rank = 0; int64 result = 0; for (int level = 0; level < levels; ++level) { const auto& counts = prefixZeroCount[level]; int zerosLeft = counts[left]; int zerosRight = counts[right]; int count = zerosRight - zerosLeft; if (k <= count) { // All selected values are in the zero branch. left = zerosLeft; right = zerosRight; } else { // Take every value in the zero branch, then continue in the // one branch for the remaining values. result += prefixZeroSum[level][right] - prefixZeroSum[level][left]; k -= count; rank |= (1 << (levels - 1 - level)); int totalZero = zeroCount[level]; left = totalZero + left - zerosLeft; right = totalZero + right - zerosRight; } } return result + (int64)k * values[rank]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Question> questions(N); for (int i = 0; i < N; ++i) { int64 a, b; cin >> a >> b; questions[i] = {a, b, a + b}; } vector<int> queries(Q); for (int& k : queries) cin >> k; // Elsie sabotages questions with the largest a+b values. sort(questions.begin(), questions.end(), [](const Question& x, const Question& y) { if (x.c != y.c) return x.c > y.c; if (x.b != y.b) return x.b < y.b; return x.a > y.a; }); vector<int> bValues(N); for (int i = 0; i < N; ++i) { bValues[i] = (int)questions[i].b; } vector<int> coordinates = bValues; sort(coordinates.begin(), coordinates.end()); coordinates.erase(unique(coordinates.begin(), coordinates.end()), coordinates.end()); WaveletMatrix wavelet(coordinates, bValues); // suffixA[p] = sum of a_i for positions i >= p. vector<int64> suffixA(N + 2, 0); for (int i = N; i >= 1; --i) { suffixA[i] = suffixA[i + 1] + questions[i - 1].a; } vector<int64> answer(N + 1); answer[0] = suffixA[1]; /* * For each k, find the best prefix length p. * The optimal prefix lengths are nondecreasing in k, so divide and * conquer over k while restricting the possible prefix lengths. */ auto solve = [&](auto&& self, int kLeft, int kRight, int pLeft, int pRight) -> void { if (kLeft > kRight) return; int kMid = (kLeft + kRight) / 2; int firstPrefix = max(pLeft, kMid); int64 bestValue = LLONG_MIN; int bestPrefix = firstPrefix; for (int p = firstPrefix; p <= pRight; ++p) { int64 value = suffixA[p + 1] - wavelet.sumSmallest(p, kMid); // Scanning in increasing order keeps the smallest maximizing p. if (value > bestValue) { bestValue = value; bestPrefix = p; } } answer[kMid] = bestValue; if (kLeft < kMid) { self(self, kLeft, kMid - 1, pLeft, bestPrefix); } if (kMid < kRight) { self(self, kMid + 1, kRight, bestPrefix, pRight); } }; solve(solve, 1, N, 1, N); for (int k : queries) { cout << answer[k] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Item { int64 a, b, c; }; class PersistentOrderStats { struct Node { int left = 0; int right = 0; int count = 0; int64 sum = 0; }; vector<Node> nodes; vector<int> values; int m; int cloneNode(int from) { Node copy = nodes[from]; nodes.push_back(copy); return (int)nodes.size() - 1; } public: PersistentOrderStats(const vector<int>& compressedValues, int updates) : values(compressedValues), m((int)compressedValues.size()) { int height = 0; while ((1LL << height) < m) ++height; nodes.reserve(1 + (size_t)updates * (height + 1)); nodes.push_back(Node{}); // Empty node. } int insert(int oldRoot, int position, int value) { int newRoot = cloneNode(oldRoot); int current = newRoot; int lo = 0, hi = m - 1; while (true) { ++nodes[current].count; nodes[current].sum += value; if (lo == hi) break; int mid = (lo + hi) / 2; if (position <= mid) { int oldChild = nodes[current].left; int newChild = cloneNode(oldChild); nodes[current].left = newChild; current = newChild; hi = mid; } else { int oldChild = nodes[current].right; int newChild = cloneNode(oldChild); nodes[current].right = newChild; current = newChild; lo = mid + 1; } } return newRoot; } // Sum of the k smallest values in this version. int64 sumSmallest(int root, int k) const { if (k == 0) return 0; int current = root; int lo = 0, hi = m - 1; int64 result = 0; while (true) { if (k == nodes[current].count) { return result + nodes[current].sum; } if (lo == hi) { return result + (int64)k * values[lo]; } int left = nodes[current].left; int leftCount = nodes[left].count; int mid = (lo + hi) / 2; if (k <= leftCount) { current = left; hi = mid; } else { result += nodes[left].sum; k -= leftCount; current = nodes[current].right; lo = mid + 1; } } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; vector<Item> items(N); for (auto& item : items) { cin >> item.a >> item.b; item.c = item.a + item.b; } vector<int> queries(Q); for (int& k : queries) cin >> k; // Elsie prefers to change questions with the largest a_i + b_i. sort(items.begin(), items.end(), [](const Item& x, const Item& y) { if (x.c != y.c) return x.c > y.c; if (x.b != y.b) return x.b < y.b; return x.a > y.a; }); vector<int> compressed; compressed.reserve(N); for (const auto& item : items) { compressed.push_back((int)item.b); } sort(compressed.begin(), compressed.end()); compressed.erase(unique(compressed.begin(), compressed.end()), compressed.end()); PersistentOrderStats pst(compressed, N); // roots[t] contains the b-values of the first t sorted questions. vector<int> roots(N + 1, 0); for (int i = 0; i < N; ++i) { int position = lower_bound(compressed.begin(), compressed.end(), (int)items[i].b) - compressed.begin(); roots[i + 1] = pst.insert(roots[i], position, (int)items[i].b); } // suffix[t] is the sum of a_i for questions after the first t questions. vector<int64> suffix(N + 1, 0); for (int i = N - 1; i >= 0; --i) { suffix[i] = suffix[i + 1] + items[i].a; } vector<int> sortedQueries = queries; sort(sortedQueries.begin(), sortedQueries.end()); sortedQueries.erase(unique(sortedQueries.begin(), sortedQueries.end()), sortedQueries.end()); vector<int64> answer(N + 1); // The leftmost optimal cut is nondecreasing as k increases. auto solve = [&](auto&& self, int ql, int qr, int optLeft, int optRight) -> void { if (ql > qr) return; int mid = (ql + qr) / 2; int k = sortedQueries[mid]; int bestCut = -1; int64 bestValue = LLONG_MIN; int firstCut = max(k, optLeft); for (int t = firstCut; t <= optRight; ++t) { int64 chosenPenalty = pst.sumSmallest(roots[t], k); int64 value = suffix[t] - chosenPenalty; // Scanning in increasing order and using strict > keeps // the leftmost maximizing cut. if (value > bestValue) { bestValue = value; bestCut = t; } } answer[k] = bestValue; if (ql < mid) { self(self, ql, mid - 1, optLeft, bestCut); } if (mid < qr) { self(self, mid + 1, qr, bestCut, optRight); } }; solve(solve, 0, (int)sortedQueries.size() - 1, 0, N); for (int k : queries) { cout << answer[k] << '\n'; } return 0; }