QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#671568 | #7512. Almost Prefix Concatenation | Forever_Young# | WA | 0ms | 3756kb | C++20 | 1.9kb | 2024-10-24 13:26:04 | 2024-10-24 13:26:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pii = pair<int, int>;
int n;
pii v[200010];
int max_v;
LL ans[200010];
struct Node {
int cnt;
int ls, rs;
LL sum;
} T[10000010];
int M = 1;
int root[200010];
int insert(int x, int l, int r, int p) {
int y = ++M;
T[y] = T[x];
T[y].cnt++;
T[y].sum += p;
if (l == r) return y;
int mid = (l + r) / 2;
if (p <= mid) {
T[y].ls = insert(T[x].ls, l, mid, p);
} else {
T[y].rs = insert(T[x].rs, mid + 1, r, p);
}
return y;
}
LL query(int x, int l, int r, int k) {
// printf("query %d %d %d\n", x, l, r, k);
assert(k <= T[x].cnt);
if (l == r) return T[x].sum - 1LL * (T[x].cnt - k) * l;
int mid = (l + r) / 2;
if (k <= T[T[x].ls].cnt) return query(T[x].ls, l, mid, k);
return T[T[x].ls].sum + query(T[x].rs, mid + 1, r, k - T[T[x].ls].cnt);
}
void solve(int l, int r, int vl, int vr) {
if (l > r) return;
int mid = (l + r) / 2;
LL mid_ans = 1e18;
int best_idx = -1;
for (int i = vl; i <= vr; i++) {
if (i < mid) continue;
LL cur = query(root[i], 1, max_v, mid) + v[i].first;
if (cur < mid_ans) {
best_idx = i;
mid_ans = cur;
}
}
ans[mid] = mid_ans;
solve(l, mid - 1, vl, best_idx);
solve(mid + 1, r, best_idx, vr);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &v[i].second, &v[i].first);
max_v = max(max_v, v[i].second);
}
sort(v + 1, v + n + 1);
root[0] = 1;
for (int i = 1; i <= n; i++) {
root[i] = insert(root[i - 1], 1, max_v, v[i].second);
}
/*
for (int i = 1; i <= M; i++) {
printf("%d: cnt=%d ls=%d rs=%d sum=%lld\n", i, T[i].cnt, T[i].ls, T[i].rs,
T[i].sum);
}
for (int i = 1; i <= n; i++) {
printf("root %d = %d\n", i, root[i]);
}
*/
solve(1, n, 1, n);
for (int i = 1; i <= n; i++) {
printf("%lld\n", ans[i]);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3756kb
input:
ababaab aba
output:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements