Submission #777214

# Submission time Handle Problem Language Result Execution time Memory
777214 2023-07-08T19:12:32 Z dxz05 Distributing Candies (IOI21_candies) C++17
37 / 100
421 ms 43756 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;
    }

    int find(int v, int tl, int tr, long long Min, long long Max, int C){
        if (tl == tr) return tl;
        int tm = (tl + tr) >> 1;

        if (max(Max, tree[v << 1 | 1].Max) - min(Min, tree[v << 1 | 1].Min) >= C){
            return find(v << 1 | 1, tm + 1, tr, Min, Max, C);
        } else {
            Min = min(Min, tree[v << 1 | 1].Min);
            Max = max(Max, tree[v << 1 | 1].Max);
            return find(v << 1, tl, tm, Min, Max, C);
        }

    }

    int find(int C){
        return find(1, 0, n - 1, (long long)1e16, (long long)-1e16, C);
    }

};

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 = st.find(C[i]);

        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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 2 ms 596 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 421 ms 43756 KB Output is correct
2 Correct 417 ms 43496 KB Output is correct
3 Correct 412 ms 43160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 138 ms 27232 KB Output is correct
4 Correct 84 ms 12708 KB Output is correct
5 Correct 248 ms 38092 KB Output is correct
6 Correct 249 ms 37996 KB Output is correct
7 Correct 262 ms 38088 KB Output is correct
8 Correct 247 ms 38084 KB Output is correct
9 Correct 249 ms 38084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 2 ms 596 KB Output isn't correct
4 Halted 0 ms 0 KB -