Submission #777210

# Submission time Handle Problem Language Result Execution time Memory
777210 2023-07-08T19:07:58 Z dxz05 Distributing Candies (IOI21_candies) C++17
100 / 100
1287 ms 49424 KB
#include "candies.h"
#include <bits/stdc++.h>

using namespace std;

struct SegmentTree{
    struct node{
        long long Min, Max; // min-max suffix
        long long lazy;
        node(): Min(0), Max(0), lazy(0) {};
        node(long long x): Min(x), Max(x), lazy(0) {};
    };

    void combine(node &par, const node &lc, const node &rc){
        par.Min = min(lc.Min, rc.Min);
        par.Max = max(lc.Max, rc.Max);
    }

    node neutral_element;

    int n;
    vector<node> tree;

    SegmentTree(int _n){
        n = _n;
        tree.assign(4 * n + 5, node());
        neutral_element.Min = (long long)1e16;
        neutral_element.Max = (long long)-1e16;
    }

    void upd(int v, long long lazy){
        tree[v].Min += lazy;
        tree[v].Max += lazy;
        tree[v].lazy += lazy;
    }

    void push(int v, int tl, int tr){
        if (tl == tr || tree[v].lazy == 0) return;
        upd(v << 1, tree[v].lazy);
        upd(v << 1 | 1, tree[v].lazy);
        tree[v].lazy = 0;
    }

    void update(int v, int tl, int tr, int l, int r, long long val){
        push(v, tl, tr);
        if (l <= tl && tr <= r){
            upd(v, val);
            return;
        }
        if (tl > r || tr < l) return;

        int tm = (tl + tr) >> 1;
        update(v << 1, tl, tm, l, r, val);
        update(v << 1 | 1, tm + 1, tr, l, r, val);
        combine(tree[v], tree[v << 1], tree[v << 1 | 1]);
    }

    void update(int l, int r, long long val){
        update(1, 0, n - 1, l, r, val);
    }

    node get(int v, int tl, int tr, int l, int r){
        push(v, tl, tr);
        if (l <= tl && tr <= r) return tree[v];
        if (tl > r || tr < l) return neutral_element;
        int tm = (tl + tr) >> 1;
        node res;
        combine(res, get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
        return res;
    }

    long long get_value(int pos){
        return get(1, 0, n - 1, pos, pos).Max;
    }

    long long get_diff(int l, int r){
        node res = get(1, 0, n - 1, l, r);
        return res.Max - res.Min;
    }

};

vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V) {
    int n = (int)C.size(), q = (int)V.size();

    V.insert(V.begin(), (int) -2e9), L.insert(L.begin(), 0), R.insert(R.begin(), n - 1);
    V.insert(V.begin(), (int) 2e9),  L.insert(L.begin(), 0), R.insert(R.begin(), n - 1);
    q += 2;

    vector<vector<int>> opening(n), closing(n);
    for (int i = 0; i < q; i++){
        opening[L[i]].push_back(i);
        closing[R[i]].push_back(i);
    }

    vector<int> ans(n, 0);

    SegmentTree st(q);
    long long sum = 0;

    for (int i = 0; i < n; i++){
        for (int k : opening[i]){
            st.update(k, q - 1, V[k]);
            sum += V[k];
        }

        int j = -1;
        int l = 0, r = q - 1;
        while (l <= r){
            int m = (l + r) >> 1;
            if (st.get_diff(m, q - 1) >= C[i]){
                j = m;
                l = m + 1;
            } else {
                r = m - 1;
            }
        }

        assert(j != -1);

        long long diff = st.get_diff(j, q - 1) - C[i];

        long long Y = st.get_value(j);
        if (V[j + 1] > 0){ /// full
            Y += diff;
            ans[i] = sum - Y;
        } else { /// empty
            Y -= diff;
            ans[i] = C[i] + sum - Y;
        }

        assert(0 <= ans[i] && ans[i] <= C[i]);

        for (int k : closing[i]){
            st.update(k, q - 1, -V[k]);
            sum -= V[k];
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 6 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1158 ms 43104 KB Output is correct
2 Correct 1287 ms 46804 KB Output is correct
3 Correct 1207 ms 46632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 228 ms 29732 KB Output is correct
3 Correct 308 ms 14508 KB Output is correct
4 Correct 1157 ms 48648 KB Output is correct
5 Correct 1211 ms 49028 KB Output is correct
6 Correct 1211 ms 49424 KB Output is correct
7 Correct 1180 ms 48764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 153 ms 29468 KB Output is correct
4 Correct 339 ms 13528 KB Output is correct
5 Correct 902 ms 41256 KB Output is correct
6 Correct 878 ms 41888 KB Output is correct
7 Correct 861 ms 42464 KB Output is correct
8 Correct 905 ms 41108 KB Output is correct
9 Correct 971 ms 42584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 6 ms 724 KB Output is correct
6 Correct 1158 ms 43104 KB Output is correct
7 Correct 1287 ms 46804 KB Output is correct
8 Correct 1207 ms 46632 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 228 ms 29732 KB Output is correct
11 Correct 308 ms 14508 KB Output is correct
12 Correct 1157 ms 48648 KB Output is correct
13 Correct 1211 ms 49028 KB Output is correct
14 Correct 1211 ms 49424 KB Output is correct
15 Correct 1180 ms 48764 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 153 ms 29468 KB Output is correct
19 Correct 339 ms 13528 KB Output is correct
20 Correct 902 ms 41256 KB Output is correct
21 Correct 878 ms 41888 KB Output is correct
22 Correct 861 ms 42464 KB Output is correct
23 Correct 905 ms 41108 KB Output is correct
24 Correct 971 ms 42584 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 288 ms 13616 KB Output is correct
27 Correct 212 ms 29344 KB Output is correct
28 Correct 1139 ms 47272 KB Output is correct
29 Correct 1218 ms 47664 KB Output is correct
30 Correct 1256 ms 47860 KB Output is correct
31 Correct 1191 ms 48056 KB Output is correct
32 Correct 1239 ms 48264 KB Output is correct