QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#549567 | #7523. Partially Free Meal | ccsurzw | TL | 1ms | 9860kb | C++20 | 2.9kb | 2024-09-06 17:41:08 | 2024-09-06 17:41:08 |
Judging History
answer
#include <array>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 3;
const int M = 1e7;
const ll INF = 5e18;
array<int, 2> a[N];
namespace persistentWeightSegmentTree
{
int tot, q, rt[N], C[N];
struct node
{
int l, r, num;
ll sum;
} st[M];
void modify(int& id, int pre, int l, int r, int pos, int val)
{
id = ++tot;
st[id] = st[pre];
++st[id].num;
st[id].sum += val;
if (l == r)
return;
int mid = (l + r) / 2;
if (pos <= mid)
modify(st[id].l, st[id].l, l, mid, pos, val);
else
modify(st[id].r, st[id].r, mid + 1, r, pos, val);
}
void init(int n)
{
tot = 0;
for (int i = 1; i <= n; ++i)
C[i] = a[i][1];
sort(C + 1, C + n + 1);
q = unique(C + 1, C + n + 1) - C - 1;
for (int i = 1; i <= n; ++i)
modify(rt[i], rt[i - 1], 1, q, lower_bound(C + 1, C + q + 1, a[i][1]) - C, a[i][1]);
assert(tot < M);
}
ll query(int u, int v, int l, int r, int k)
{
if (st[v].num - st[u].num <= k)
return st[v].sum - st[u].sum;
else if (l == r)
return (st[v].sum - st[u].sum) / (ll)(st[v].num - st[u].num) * (ll)k;
int mid = (l + r) / 2, x = st[st[v].l].num - st[st[u].l].num;
ll res = query(st[u].l, st[v].l, l, mid, k);
if (x < k)
res += query(st[u].r, st[v].r, mid + 1, r, k - x);
return res;
}
ll ask(int l, int r, int k)
{
return query(rt[l - 1], rt[r], 1, q, k);
}
};
ll ans[N];
struct T {
ll v, i;
bool operator<(const T b) {
return v < b.v;
}
}b[N];
bool cmp(array<int,2> &a, array<int,2> &b) {
if (a[0] == b[0])return a[1] > b[1];
return a[0] < b[0];
}
void solve()
{
int n, hash[N];
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i][1] >> a[i][0];
hash[i] = 10;
ans[i] = 1e18;
}
sort(a + 1, a + n + 1,cmp);
for (int i = 1; i <= n; ++i)
{
// cout << a[i][1] << ' ' << a[i][0] << endl;
b[i].v = a[i][1] + a[i][0];
b[i].i = i;
}
sort(b + 1, b + 1 + n);
persistentWeightSegmentTree::init(n);
int l = 1, r = 1;
for (int i = 1; i <= n; i++) {
r = b[i].i;
for (int k = 1; k <= r; ++k)
{
ll val = persistentWeightSegmentTree::ask(1, k - 1, r - 1) + b[i].v;
ans[k] = min(ans[k], val);
hash[k]--;
}
while (hash[l] <= 0)l++;
}
for (int i = 1; i <= n; ++i)
cout << ans[i] << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 9860kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
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...