QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#471601 | #7523. Partially Free Meal | 333zhan | RE | 14ms | 116220kb | C++20 | 2.9kb | 2024-07-10 22:59:38 | 2024-07-10 22:59:38 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2E5 + 10;
const int inf = 1E18;
inline int read () {
int w = 1, s = 0; char ch = getchar ();
for (; ! isdigit (ch); ch = getchar ()) if (ch == '-') w = -1;
for (; isdigit (ch); ch = getchar ()) s = (s << 1) + (s << 3) + (ch ^ 48);
return s * w;
}
struct Tree {
int l, r;
int sum = 0;
int cnt = 0;
} tr[N * 18];
int idx = 0;
int root[N];
vector <int> all;
// int build (int l, int r) {
// int q = ++ idx;
// if (l == r) {
// return q;
// }
// int mid = (l + r) / 2;
// tr[q].l = build (l, mid);
// tr[q].r = build (mid + 1, r);
// return q;
// }
int insert (int p, int l, int r, int x, int y) {
int q = ++ idx;
tr[q] = tr[p];
tr[q].sum += y;
tr[q].cnt ++;
if (l == r) {
return q;
}
int mid = (l + r) / 2;
if (x <= mid) {
tr[q].l = insert (tr[p].l, l, mid, x, y);
} else {
tr[q].r = insert (tr[p].r, mid + 1, r, x, y);
}
return q;
}
int query (int v1, int v2, int l, int r, int k) {
if (l == r) {
return all[l] * k;
}
int cnt = tr[tr[v2].l].cnt - tr[tr[v1].l].cnt;
int mid = (l + r) / 2;
if (k <= cnt) {
return query (tr[v1].l, tr[v2].l, l, mid, k);
} else {
return tr[tr[v2].l].sum - tr[tr[v1].l].sum + query (tr[v1].r, tr[v2].r, mid + 1, r, k - cnt);
}
}
void solve () {
int n = read ();
vector <pair <int, int>> p (n + 1);
for (int i = 1; i <= n; i ++) {
p[i].first = read ();
p[i].second = read ();
all.push_back (p[i].first);
}
sort (p.begin (), p.end (),
[&] (pair <int, int> x, pair <int, int> y) {
return x.second < y.second;
}
);
sort (all.begin (), all.end ());
all.erase (unique (all.begin (), all.end ()), all.end ());
const int N = all.size ();
auto find = [&] (int x) -> int {
return lower_bound (all.begin (), all.end (), x) - all.begin ();// + 1;
};
// root[0] = build (0, N - 1);
for (int i = 1; i <= n; i ++) {
root[i] = insert (root[i - 1], 0, N - 1, find (p[i].first), p[i].first);
}
vector <int> ans (n + 1);
auto solve = [&] (auto &&self, int xl, int xr, int yl, int yr) -> void {
if (xl > xr) {
return;
}
int mid = (xl + xr) / 2;
int minn = inf;
int id;
for (int i = max (mid, yl); i <= yr; i ++) {
int sum = query (root[0], root[i - 1], 0, N - 1, mid - 1);
sum += p[i].first + p[i].second;
if (sum < minn) {
id = i;
minn = sum;
}
}
ans[mid] = minn;
self (self, xl, mid - 1, yl, id);
self (self, mid + 1, xr, id, yr);
};
solve (solve, 1, n, 1, n);
for (int i = 1; i <= n; i ++) {
printf ("%lld\n", ans[i]);
}
}
signed main () {
// ios::sync_with_stdio (false);
// cin.tie (nullptr);
int T = 1;
// cin >> T;
// T = read ();
while (T --) {
solve ();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 14ms
memory: 116220kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Runtime Error
input:
200000 466436993 804989151 660995237 756645598 432103296 703610564 6889895 53276988 873617076 822481192 532911431 126844295 623111499 456772252 937464699 762157133 708503076 786039753 78556972 5436013 582960979 398984169 786333369 325119902 930705057 615928139 924915828 506145001 164984329 208212435...