Submission #524517

# Submission time Handle Problem Language Result Execution time Memory
524517 2022-02-09T11:41:50 Z Aldas25 Distributing Candies (IOI21_candies) C++17
100 / 100
1621 ms 77456 KB
#include "candies.h"

#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int MAXN = 800100;
const ll INF = 1e15;

ll n, q;
ll treeMn[MAXN], treeMx[MAXN], lazy[MAXN];

void shift (int id) {
    ll d = lazy[id];
    lazy[id] = 0;

    treeMn[2*id] += d;
    treeMn[2*id+1] += d;

    treeMx[2*id] += d;
    treeMx[2*id+1] += d;

    lazy[2*id] += d;
    lazy[2*id+1] += d;
}

void upd (int x, int y, ll d, int id = 1, int le = 0, int ri = q) {
    if (x > ri || y < le) return;
    if (x <= le && ri <= y) {
        treeMn[id] += d;
        treeMx[id] += d;
        lazy[id] += d;
        return;
    }

    shift (id);
    int m = (le+ri)/2;
    upd (x, y, d, 2*id, le, m);
    upd (x, y, d, 2*id+1, m+1, ri);

    treeMn[id] = min (treeMn[2*id], treeMn[2*id+1]);
    treeMx[id] = max (treeMx[2*id], treeMx[2*id+1]);
}

ll getMin (int x, int y, int id = 1, int le = 0, int ri = q) {
    if (x > ri || y < le) return INF;
    if (x <= le && ri <= y) return treeMn[id];
    shift (id);
    int m = (le+ri)/2;
    return min(getMin(x, y, 2*id, le, m), getMin(x, y, 2*id+1, m+1, ri));
}

ll getMax (int x, int y, int id = 1, int le = 0, int ri = q) {
    if (x > ri || y < le) return (-INF);
    if (x <= le && ri <= y) return treeMx[id];
    shift (id);
    int m = (le+ri)/2;
    return max(getMax(x, y, 2*id, le, m), getMax(x, y, 2*id+1, m+1, ri));
}

ll c[MAXN];
vi queriesAdd[MAXN], queriesRem[MAXN];
ll l[MAXN], r[MAXN], v[MAXN];

vi distribute_candies(vi _c, vi _l, vi _r, vi _v) {
    n = _c.size();
    q = _l.size();
    vi s(n);

    FOR(i, 0, n-1) c[i] = _c[i];

    l[0] = 0, r[0] = n-1, v[0] = +INF;
    l[1] = 0, r[1] = n-1, v[1] = -INF;
    FOR(i, 0, q-1) {
        l[i+2] = _l[i];
        r[i+2] = _r[i];
        v[i+2] = _v[i];
    }
    q+=2;

    FOR(i, 0, q-1) {
        queriesAdd[l[i]].pb(i);
        queriesRem[r[i]+1].pb(i);
    }

    FOR(i, 0, n-1) {
        //cout << " i = " << i << endl;

        for (int id : queriesAdd[i]) {
          //      cout << "    adding query id = " << id << " v = " << v[id] << endl;
            upd(id, q, v[id]);
        }
        for (int id : queriesRem[i]) {
            //    cout << "    removing query id = " << id << " v = " << v[id] << endl;
            upd (id, q, -v[id]);
        }

        int le = 0, ri = q;
        while (le < ri) {
            int m = (le+ri+1)/2;
            if (getMax(m, q) - getMin(m, q) >= c[i])
                le = m;
            else
                ri = m-1;
        }

        ll mx = getMax(le, q);
        ll mn = getMin(le, q);
        ll lst = getMax(q, q);

        ll mx2 = getMax(le+1, q);

        //cout << "    le = " << le << " mx = " << mx << " mn = " << mn << " lst = " << lst << " mx2 = " << mx2 << endl;

        if (mx == mx2) {
            /// the last touch was to upper wall
            s[i] =(int) (lst - (mx-c[i]));
        } else {
            s[i] = (int) (lst - mn);
        }
    }

    return s;
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 37872 KB Output is correct
2 Correct 18 ms 37836 KB Output is correct
3 Correct 21 ms 38024 KB Output is correct
4 Correct 20 ms 38092 KB Output is correct
5 Correct 25 ms 38220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1574 ms 70740 KB Output is correct
2 Correct 1621 ms 70796 KB Output is correct
3 Correct 1562 ms 70728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 37836 KB Output is correct
2 Correct 283 ms 62380 KB Output is correct
3 Correct 522 ms 45364 KB Output is correct
4 Correct 1586 ms 76656 KB Output is correct
5 Correct 1509 ms 77056 KB Output is correct
6 Correct 1572 ms 77456 KB Output is correct
7 Correct 1504 ms 76780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37892 KB Output is correct
2 Correct 19 ms 37836 KB Output is correct
3 Correct 130 ms 61256 KB Output is correct
4 Correct 495 ms 42160 KB Output is correct
5 Correct 1313 ms 65336 KB Output is correct
6 Correct 1321 ms 65328 KB Output is correct
7 Correct 1311 ms 65392 KB Output is correct
8 Correct 1324 ms 68784 KB Output is correct
9 Correct 1354 ms 70232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 37872 KB Output is correct
2 Correct 18 ms 37836 KB Output is correct
3 Correct 21 ms 38024 KB Output is correct
4 Correct 20 ms 38092 KB Output is correct
5 Correct 25 ms 38220 KB Output is correct
6 Correct 1574 ms 70740 KB Output is correct
7 Correct 1621 ms 70796 KB Output is correct
8 Correct 1562 ms 70728 KB Output is correct
9 Correct 23 ms 37836 KB Output is correct
10 Correct 283 ms 62380 KB Output is correct
11 Correct 522 ms 45364 KB Output is correct
12 Correct 1586 ms 76656 KB Output is correct
13 Correct 1509 ms 77056 KB Output is correct
14 Correct 1572 ms 77456 KB Output is correct
15 Correct 1504 ms 76780 KB Output is correct
16 Correct 19 ms 37892 KB Output is correct
17 Correct 19 ms 37836 KB Output is correct
18 Correct 130 ms 61256 KB Output is correct
19 Correct 495 ms 42160 KB Output is correct
20 Correct 1313 ms 65336 KB Output is correct
21 Correct 1321 ms 65328 KB Output is correct
22 Correct 1311 ms 65392 KB Output is correct
23 Correct 1324 ms 68784 KB Output is correct
24 Correct 1354 ms 70232 KB Output is correct
25 Correct 19 ms 37820 KB Output is correct
26 Correct 524 ms 43412 KB Output is correct
27 Correct 238 ms 65120 KB Output is correct
28 Correct 1534 ms 75292 KB Output is correct
29 Correct 1579 ms 75684 KB Output is correct
30 Correct 1554 ms 75868 KB Output is correct
31 Correct 1566 ms 76084 KB Output is correct
32 Correct 1553 ms 76196 KB Output is correct