Submission #1105572

# Submission time Handle Problem Language Result Execution time Memory
1105572 2024-10-26T18:51:55 Z Zicrus Nile (IOI24_nile) C++17
100 / 100
116 ms 25136 KB
#include <bits/stdc++.h>
#include "nile.h"
using namespace std;

typedef long long ll;

ll n, q, sumA, sumRun;
vector<pair<ll, ll>> art; // W, B-A
vector<pair<ll, ll>> d; // D, id
priority_queue<pair<pair<ll, bool>, pair<ll, ll>>> gaps; // { -dist, jump }, { id1, id2 }

vector<ll> lnk, sz, sumDiff, mxJmpEven, mxJmpOdd, mxEven, mxOdd;

ll find(ll a) {
    if (lnk[a] != a) lnk[a] = find(lnk[a]);
    return lnk[a];
}

void unite(ll a, ll b) {
    a = find(a); b = find(b);
    if (a == b) return;
    if (a < b) swap(a, b);
    lnk[a] = b;
    if (sz[b]&1) { // odd
        mxJmpEven[b] = max(mxJmpEven[b], mxJmpOdd[a]);
        mxJmpOdd[b] = max(mxJmpOdd[b], mxJmpEven[a]);
        mxEven[b] = max(mxEven[b], mxOdd[a]);
        mxOdd[b] = max(mxOdd[b], mxEven[a]);
    }
    else { // even
        mxJmpEven[b] = max(mxJmpEven[b], mxJmpEven[a]);
        mxJmpOdd[b] = max(mxJmpOdd[b], mxJmpOdd[a]);
        mxEven[b] = max(mxEven[b], mxEven[a]);
        mxOdd[b] = max(mxOdd[b], mxOdd[a]);
    }
    sz[b] += sz[a];
    sumDiff[b] += sumDiff[a];
}

ll getContrib(ll a) {
    a = find(a);
    ll res = sumDiff[a];
    if (sz[a]&1) {
        res -= max(mxEven[a], mxJmpOdd[a]);
    }
    return res;
}

vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
    n = W.size();
    q = E.size();
    sumA = 0;
    vector<ll> res(q);
    art = vector<pair<ll, ll>>(n);
    d = vector<pair<ll, ll>>(q);
    for (int i = 0; i < n; i++) {
        art[i] = {W[i], B[i]-A[i]};
        sumA += A[i];
    }
    for (int i = 0; i < q; i++) {
        d[i] = {E[i], i};
    }
    sort(art.begin(), art.end());
    sort(d.begin(), d.end());
    sumRun = sumA;

    for (int i = 0; i < n-1; i++) {
        gaps.push({{-(art[i+1].first - art[i].first), false}, {i, i+1}});
    }
    for (int i = 0; i < n-2; i++) {
        gaps.push({{-(art[i+2].first - art[i].first), true}, {i, i+2}});
    }

    // Union-find setup
    lnk = sumDiff = mxEven = vector<ll>(n);
    for (int i = 0; i < n; i++) {
        lnk[i] = i;
        sumDiff[i] = art[i].second;
        mxEven[i] = art[i].second;
    }
    sz = vector<ll>(n, 1);
    mxJmpEven = vector<ll>(n, -(1ll << 62ll));
    mxJmpOdd = vector<ll>(n, -(1ll << 62ll));
    mxOdd = vector<ll>(n, -(1ll << 62ll));

    // Solve
    for (int i = 0; i < q; i++) {
        ll D = d[i].first;
        while (!gaps.empty() && -gaps.top().first.first <= D) {
            ll dist = -gaps.top().first.first;
            bool jmp = gaps.top().first.second;
            pair<ll, ll> ids = gaps.top().second;
            gaps.pop();
            if (jmp) {
                ll id = (ids.first + ids.second) / 2;
                ll comp = find(id);
                ll relPos = id - comp;
                sumRun -= getContrib(comp);
                if (relPos&1) { // odd
                    mxJmpOdd[comp] = max(mxJmpOdd[comp], art[id].second);
                }
                else { // even
                    mxJmpEven[comp] = max(mxJmpEven[comp], art[id].second);
                }
                sumRun += getContrib(comp);
            }
            else {
                ids = {find(ids.first), find(ids.second)};
                if (ids.first != ids.second) {
                    sumRun -= getContrib(ids.first) + getContrib(ids.second);
                    unite(ids.first, ids.second);
                    sumRun += getContrib(ids.first);
                }
            }
        }
        res[d[i].second] = sumRun;
    }

    return res;
}

#ifdef TEST
#include "grader.cpp"
#endif

Compilation message

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:90:16: warning: unused variable 'dist' [-Wunused-variable]
   90 |             ll dist = -gaps.top().first.first;
      |                ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 2 ms 764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 21680 KB Output is correct
2 Correct 61 ms 21668 KB Output is correct
3 Correct 56 ms 21680 KB Output is correct
4 Correct 55 ms 21408 KB Output is correct
5 Correct 56 ms 21624 KB Output is correct
6 Correct 55 ms 21536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 19764 KB Output is correct
2 Correct 57 ms 20060 KB Output is correct
3 Correct 76 ms 19384 KB Output is correct
4 Correct 71 ms 19376 KB Output is correct
5 Correct 70 ms 19636 KB Output is correct
6 Correct 99 ms 19376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 2 ms 764 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 604 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 2 ms 764 KB Output is correct
7 Correct 60 ms 21680 KB Output is correct
8 Correct 61 ms 21668 KB Output is correct
9 Correct 56 ms 21680 KB Output is correct
10 Correct 55 ms 21408 KB Output is correct
11 Correct 56 ms 21624 KB Output is correct
12 Correct 55 ms 21536 KB Output is correct
13 Correct 55 ms 19764 KB Output is correct
14 Correct 57 ms 20060 KB Output is correct
15 Correct 76 ms 19384 KB Output is correct
16 Correct 71 ms 19376 KB Output is correct
17 Correct 70 ms 19636 KB Output is correct
18 Correct 99 ms 19376 KB Output is correct
19 Correct 3 ms 592 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 804 KB Output is correct
25 Correct 61 ms 21168 KB Output is correct
26 Correct 67 ms 21424 KB Output is correct
27 Correct 79 ms 20912 KB Output is correct
28 Correct 82 ms 20912 KB Output is correct
29 Correct 74 ms 21168 KB Output is correct
30 Correct 85 ms 20920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 19764 KB Output is correct
2 Correct 57 ms 20060 KB Output is correct
3 Correct 76 ms 19384 KB Output is correct
4 Correct 71 ms 19376 KB Output is correct
5 Correct 70 ms 19636 KB Output is correct
6 Correct 99 ms 19376 KB Output is correct
7 Correct 78 ms 23472 KB Output is correct
8 Correct 81 ms 23728 KB Output is correct
9 Correct 93 ms 22948 KB Output is correct
10 Correct 93 ms 22968 KB Output is correct
11 Correct 95 ms 22964 KB Output is correct
12 Correct 100 ms 22192 KB Output is correct
13 Correct 90 ms 23068 KB Output is correct
14 Correct 89 ms 23128 KB Output is correct
15 Correct 100 ms 22960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 2 ms 592 KB Output is correct
5 Correct 2 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 764 KB Output is correct
8 Correct 60 ms 21680 KB Output is correct
9 Correct 61 ms 21668 KB Output is correct
10 Correct 56 ms 21680 KB Output is correct
11 Correct 55 ms 21408 KB Output is correct
12 Correct 56 ms 21624 KB Output is correct
13 Correct 55 ms 21536 KB Output is correct
14 Correct 55 ms 19764 KB Output is correct
15 Correct 57 ms 20060 KB Output is correct
16 Correct 76 ms 19384 KB Output is correct
17 Correct 71 ms 19376 KB Output is correct
18 Correct 70 ms 19636 KB Output is correct
19 Correct 99 ms 19376 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 2 ms 804 KB Output is correct
26 Correct 61 ms 21168 KB Output is correct
27 Correct 67 ms 21424 KB Output is correct
28 Correct 79 ms 20912 KB Output is correct
29 Correct 82 ms 20912 KB Output is correct
30 Correct 74 ms 21168 KB Output is correct
31 Correct 85 ms 20920 KB Output is correct
32 Correct 78 ms 23472 KB Output is correct
33 Correct 81 ms 23728 KB Output is correct
34 Correct 93 ms 22948 KB Output is correct
35 Correct 93 ms 22968 KB Output is correct
36 Correct 95 ms 22964 KB Output is correct
37 Correct 100 ms 22192 KB Output is correct
38 Correct 90 ms 23068 KB Output is correct
39 Correct 89 ms 23128 KB Output is correct
40 Correct 100 ms 22960 KB Output is correct
41 Correct 93 ms 25008 KB Output is correct
42 Correct 87 ms 25136 KB Output is correct
43 Correct 116 ms 24168 KB Output is correct
44 Correct 95 ms 24496 KB Output is correct
45 Correct 94 ms 24496 KB Output is correct
46 Correct 95 ms 24496 KB Output is correct
47 Correct 97 ms 24756 KB Output is correct
48 Correct 95 ms 24240 KB Output is correct
49 Correct 105 ms 24600 KB Output is correct