QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#202414#7523. Partially Free Mealucup-team859RE 0ms3872kbC++173.8kb2023-10-06 01:27:002023-10-06 01:27:00

Judging History

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

  • [2023-10-06 01:27:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3872kb
  • [2023-10-06 01:27:00]
  • 提交

answer

#include <bits/stdc++.h>

#define lsb(x) (x & (-x))

using ull = unsigned long long;
using ll = long long;

using namespace std;

constexpr int INF = (int) 1e9;

struct SegTree {
    struct Node {
        int l = -1, r = -1;
        ll sum = 0;
        int count = 0;
    };

    SegTree() {
        makeNode();
    }

    inline int makeNode(int oldId) {
        if (oldId == -1) {
            return makeNode();
        }
        data.push_back(data[oldId]);
        return (int)data.size() - 1;
    }

    inline int makeNode() {
        data.emplace_back();
        return (int)data.size() - 1;
    }

    int update(int id, int l, int r, int val) {
        int newId = makeNode(id);

        if (l == r) {
            data[newId].sum += val;
            data[newId].count++;
        } else {
            int mid = (l + r) / 2;

            if (val <= mid) {
                int idl = update(data[newId].l, l, mid, val);
                data[newId].l = idl;
            } else {
                int idr = update(data[newId].r, mid + 1, r, val);
                data[newId].r = idr;
            }

            data[newId].sum += val;
            data[newId].count++;
        }
 
        return newId;
    }

    ll query(int id, int k) {
        ll answer = query(id, 1, INF, k);
        if (k != 0) {
            return 1e18;
        }
        return answer;
    }

private:
    ll query(int id, int l, int r, int& k) {
        if (k <= 0) return 0LL;

        if (data[id].count <= k) {
            k -= data[id].count;
            return data[id].sum;
        } else {
            int mid = (l + r) / 2;
            ll answer = query(data[id].l, l, mid, k);
            answer += query(data[id].r, mid + 1, r, k);
            return answer;
        }
    }

public:
    vector<Node> data;
};

struct Dish {
    int a, b;
    bool operator<(const Dish& dish) const {
        if (b == dish.b)
            return a > dish.a;
        return b < dish.b;
    }
};



int main() {
#ifdef HOME
    ifstream cin("input.in");
    ofstream cout("output.out");
#endif
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int n;
    cin >> n;

    vector<Dish> dishes(n);
    for (auto& dish : dishes) {
        cin >> dish.a >> dish.b;
    }
    sort(dishes.begin(), dishes.end());

    SegTree st;
    vector<int> roots(n + 1);

    int nrm = 0;
    vector<int> norm(n);
    vector<int> stk;

    auto Get = [&](int p1, int p2) {
        int nrm1 = norm[p1], nrm2 = norm[p2];
        int root1 = roots[nrm1], root2 = roots[nrm2];
        int k = 0;
        for (int step = 1 << 17; step; step >>= 1) {
            if (k + step <= st.data[root2].count) {
                ll s1 = st.query(root1, k + step) + dishes[p1].b;
                ll s2 = st.query(root2, k + step) + dishes[p2].b;
                if (s1 < s2) {
                    k += step;
                }
            }
        }
        return k;
    };  

    int i = 0;
    while (i < n) {
        nrm++;
        roots[nrm] = roots[nrm - 1];

        int j = i;
        while (j < n && dishes[i].b == dishes[j].b) {
            roots[nrm] = st.update(roots[nrm], 1, INF, dishes[j].a);
            norm[j++] = nrm;
        }
        i = j;

        int pos = i - 1;
        while (stk.size() > 1 && Get(stk[stk.size() - 2], stk.back()) >= Get(stk.back(), pos)) {
            stk.pop_back();
        }
        stk.push_back(pos);
    }

    int k = 1;
    for (int i = 0; i < (int)stk.size(); i++) {
        int curr = (i == (int)stk.size() - 1 ? n : Get(stk[i], stk[i + 1]));
        while (k <= curr) {
            cout << st.query(roots[norm[stk[i]]], k) + dishes[stk[i]].b << "\n";
            k++;
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: