Submission #531248

# Submission time Handle Problem Language Result Execution time Memory
531248 2022-02-28T08:47:50 Z alextodoran Distributing Candies (IOI21_candies) C++17
100 / 100
386 ms 43020 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>
#include "candies.h"

using namespace std;

typedef long long ll;

const int N_MAX = 200000;

struct SGTNode {
    ll delta;
    ll minVal;
    ll maxVal;
    void setVal (const int &v) {
        delta = v;
        minVal = min((ll) 0, -delta);
        maxVal = max((ll) 0, -delta);
    }
};
SGTNode operator + (const SGTNode &u, const SGTNode &v) {
    SGTNode w;
    w.delta = u.delta + v.delta;
    w.minVal = min(v.minVal, -v.delta + u.minVal);
    w.maxVal = max(v.maxVal, -v.delta + u.maxVal);
    return w;
}

int N;
SGTNode SGT[N_MAX * 4 + 2];

void build (int node, int l, int r) {
    if (l == r) {
        SGT[node].setVal(0);
        return;
    }
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    build(lSon, l, mid);
    build(rSon, mid + 1, r);
    SGT[node] = SGT[lSon] + SGT[rSon];
}
void build (int n) {
    N = n;
    build(1, 1, N);
}
void update (int node, int l, int r, int pos, int val) {
    if (l == r) {
        SGT[node].setVal(val);
        return;
    }
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    if (pos <= mid) {
        update(lSon, l, mid, pos, val);
    } else {
        update(rSon, mid + 1, r, pos, val);
    }
    SGT[node] = SGT[lSon] + SGT[rSon];
}
void update (int pos, int val) {
    update(1, 1, N, pos + 1, val);
}
ll query (int node, int l, int r, ll currMin, ll currMax, int diff) {
    if (l == r) {
        ll excess = max((ll) 0, (max(currMax, SGT[node].maxVal) - min(currMin, SGT[node].minVal)) - diff);
        if (SGT[node].delta > 0) {
            return +excess;
        } else {
            return -excess - diff;
        }
    }
    int mid = (l + r) / 2;
    int lSon = node * 2, rSon = node * 2 + 1;
    ll minr = min(currMin, SGT[rSon].minVal);
    ll maxr = max(currMax, SGT[rSon].maxVal);
    if (maxr - minr >= diff) {
        return SGT[lSon].delta + query(rSon, mid + 1, r, currMin, currMax, diff);
    } else {
        return query(lSon, l, mid, minr + SGT[rSon].delta, maxr + SGT[rSon].delta, diff);
    }
}
ll query (int diff) {
    return query(1, 1, N, 0, 0, diff);
}

vector <int> distribute_candies (vector <int> c, vector <int> l,
                                 vector <int> r, vector <int> v) {
    int n = (int) c.size();
    int q = (int) v.size();

    vector <int> evAdd[n], evDel[n];
    for (int i = 0; i < q; i++) {
        evAdd[l[i]].push_back(i);
        evDel[r[i]].push_back(i);
    }

    build(2 + q);
    update(0, + (int) 1e9);
    update(1, - (int) 1e9);
    vector <int> answer (n);
    for (int i = 0; i < n; i++) {
        for (int j : evAdd[i]) {
            update(2 + j, v[j]);
        }
        answer[i] = SGT[1].delta - query(c[i]);
        for (int j : evDel[i]) {
            update(2 + j, 0);
        }
    }
    return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 3 ms 628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 342 ms 36300 KB Output is correct
2 Correct 342 ms 40320 KB Output is correct
3 Correct 385 ms 40160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 217 ms 23268 KB Output is correct
3 Correct 62 ms 15632 KB Output is correct
4 Correct 361 ms 42164 KB Output is correct
5 Correct 368 ms 42436 KB Output is correct
6 Correct 386 ms 43020 KB Output is correct
7 Correct 334 ms 42280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 126 ms 21740 KB Output is correct
4 Correct 56 ms 13408 KB Output is correct
5 Correct 201 ms 34248 KB Output is correct
6 Correct 188 ms 35012 KB Output is correct
7 Correct 208 ms 35620 KB Output is correct
8 Correct 220 ms 34256 KB Output is correct
9 Correct 181 ms 35744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 2 ms 428 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 3 ms 628 KB Output is correct
6 Correct 342 ms 36300 KB Output is correct
7 Correct 342 ms 40320 KB Output is correct
8 Correct 385 ms 40160 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 217 ms 23268 KB Output is correct
11 Correct 62 ms 15632 KB Output is correct
12 Correct 361 ms 42164 KB Output is correct
13 Correct 368 ms 42436 KB Output is correct
14 Correct 386 ms 43020 KB Output is correct
15 Correct 334 ms 42280 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 126 ms 21740 KB Output is correct
19 Correct 56 ms 13408 KB Output is correct
20 Correct 201 ms 34248 KB Output is correct
21 Correct 188 ms 35012 KB Output is correct
22 Correct 208 ms 35620 KB Output is correct
23 Correct 220 ms 34256 KB Output is correct
24 Correct 181 ms 35744 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 60 ms 13488 KB Output is correct
27 Correct 216 ms 22876 KB Output is correct
28 Correct 367 ms 40792 KB Output is correct
29 Correct 375 ms 41088 KB Output is correct
30 Correct 378 ms 41376 KB Output is correct
31 Correct 356 ms 41568 KB Output is correct
32 Correct 364 ms 41812 KB Output is correct