QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#202436#7523. Partially Free Mealucup-team859WA 1411ms120556kbC++174.1kb2023-10-06 02:04:332023-10-06 02:04:34

Judging History

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

  • [2023-10-06 02:04:34]
  • 评测
  • 测评结果:WA
  • 用时:1411ms
  • 内存:120556kb
  • [2023-10-06 02:04:33]
  • 提交

answer

#include <bits/stdc++.h>

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

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

using namespace std;

constexpr int MAXA = (int) 2e5;

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 pos, int val) {
        int newId = makeNode(id);

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

            if (pos <= mid) {
                int idl = update(data[newId].l, l, mid, pos, val);
                data[newId].l = idl;
            } else {
                int idr = update(data[newId].r, mid + 1, r, pos, 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, MAXA, k);
        if (k != 0) {
            return 1e18;
        }
        return answer;
    }

private:
    ll query(int id, int l, int r, int& k) {
        if (id == -1 || 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 {
        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);
    vector<int> nrm_a;
    for (auto& dish : dishes) {
        cin >> dish.a >> dish.b;
        nrm_a.push_back(dish.a);
    }
    sort(dishes.begin(), dishes.end());
    
    sort(nrm_a.begin(), nrm_a.end());
    nrm_a.resize(unique(nrm_a.begin(), nrm_a.end()) - nrm_a.begin());

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

    vector<int> norm(n);
    vector<int> stk;
    map<pair<int, int>, int> cache;

    auto Get = [&](int p1, int p2) {
        if (cache.find({p1, p2}) != cache.end()) {
            return cache[{p1, p2}];
        }

        int root1 = roots[norm[p1]], root2 = roots[norm[p2]];

        int k = 0;
        for (int step = 1 << 17; step; step >>= 1) {
            if (k + step <= st.data[root1].count) {
                if (st.query(root1, k + step) + dishes[p1].b < st.query(root2, k + step) + dishes[p2].b) {
                    k += step;
                }
            }
        }
        return cache[{p1, p2}] = k;
    };  

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

        int j = i;
        while (j < n && dishes[i].b == dishes[j].b) {
            int norm_a = lower_bound(nrm_a.begin(), nrm_a.end(), dishes[j].a) - nrm_a.begin() + 1;
            roots[nrm] = st.update(roots[nrm], 1, MAXA, norm_a, 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: 3836kb

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 1411ms
memory: 120556kb

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:

1318594
3208018
5570526
7340845
9223347
11149865
12332210
13476823
14788280
16172895
17768627
19336633
20693779
22005236
23389851
25073157
26760338
28509336
30294967
32093959
33976461
35893858
37754030
39588384
41470886
43388283
45309771
47309654
48837539
50417767
52079411
53762717
55190044
56577630...

result:

wrong answer 25827th lines differ - expected: '894878200462', found: '894878202978'