QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780840#7523. Partially Free MealrgnerdplayerRE 0ms3624kbC++232.5kb2024-11-25 13:33:272024-11-25 13:33:28

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 13:33:28]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3624kb
  • [2024-11-25 13:33:27]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

struct Node {
    int lc = 0, rc = 0;
    int cnt = 0;
    i64 sum = 0;
} pool[200000 * 20];
int counter = 0;

int modify(int rt, int l, int r, int p, int v) {
    int nrt = ++counter;
    pool[nrt] = pool[rt];
    rt = nrt;
    pool[rt].cnt++;
    pool[rt].sum += v;
    if (r - l > 1) {
        int m = (l + r) / 2;
        if (p < m) {
            pool[rt].lc = modify(pool[rt].lc, l, m, p, v);
        } else {
            pool[rt].rc = modify(pool[rt].rc, m, r, p, v);
        }
    }
    return rt;
}
i64 query(int rt, int l, int r, int k) {
    if (r - l == 1) {
        return k == 0 ? 0 : pool[rt].sum / pool[rt].cnt * k;
    }
    int m = (l + r) / 2;
    int lcnt = pool[pool[rt].lc].cnt;
    if (k <= lcnt) {
        return query(pool[rt].lc, l, m, k);
    } else {
        return pool[pool[rt].lc].sum + query(pool[rt].rc, m, r, k - lcnt);
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    auto solve = [&]() {
        int n;
        cin >> n;

        vector<pair<int, int>> ba(n);
        vector<int> as;
        for (auto &[b, a] : ba) {
            cin >> a >> b;
            as.push_back(a);
        }

        sort(ba.begin(), ba.end());
        sort(as.begin(), as.end());
        as.erase(unique(as.begin(), as.end()), as.end());

        vector<int> roots(n + 1);
        for (int i = 0; i < n; i++) {
            int a = ba[i].second;
            roots[i + 1] = modify(roots[i], 0, as.size(), lower_bound(as.begin(), as.end(), a) - as.begin(), a);
        }


        vector<i64> ans(n);

        auto dc = [&](auto dc, int l, int r, int ql, int qr) -> void {
            if (l > r) {
                return;
            }
            int m = (l + r) / 2;
            i64 best = 1e18;
            int at = -1;
            for (int i = ql; i <= qr; i++) {
                if (i < m) {
                    continue;
                }
                i64 res = query(roots[i], 0, n, m) + ba[i].first + ba[i].second;
                if (res < best) {
                    best = res;
                    at = i;
                }
            }
            ans[m] = best;
            dc(dc, l, m - 1, ql, at);
            dc(dc, m + 1, r, at, qr);
        };
        dc(dc, 0, n - 1, 0, n - 1);

        for (int i = 0; i < n; i++) {
            cout << ans[i] << '\n';
        }
    };
    
    solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3624kb

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...

output:


result: