Submission #977497

# Submission time Handle Problem Language Result Execution time Memory
977497 2024-05-08T05:23:44 Z GrindMachine Distributing Candies (IOI21_candies) C++17
100 / 100
1497 ms 46376 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://codeforces.com/blog/entry/92083?#comment-807860

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "candies.h"

template<typename T>
struct lazysegtree {
    /*=======================================================*/

    struct data {
        ll mn,mx;
    };

    struct lazy {
        ll a;
    };

    data d_neutral = {inf2,-inf2};
    lazy l_neutral = {0};

    void merge(data &curr, data &left, data &right) {
        curr.mn = min(left.mn,right.mn);
        curr.mx = max(left.mx,right.mx);
    }

    void create(int x, int lx, int rx, T v) {
        tr[x].mn = tr[x].mx = 0;
    }

    void modify(int x, int lx, int rx, T v) {
        lz[x].a = v;
    }

    void propagate(int x, int lx, int rx) {
        ll v = lz[x].a;
        if(!v) return;

        tr[x].mn += v, tr[x].mx += v;

        if(rx-lx > 1){
            lz[2*x+1].a += v;
            lz[2*x+2].a += v;
        }

        lz[x] = l_neutral;
    }

    /*=======================================================*/

    int siz = 1;
    vector<data> tr;
    vector<lazy> lz;

    lazysegtree() {

    }

    lazysegtree(int n) {
        while (siz < n) siz *= 2;
        tr.assign(2 * siz, d_neutral);
        lz.assign(2 * siz, l_neutral);
    }

    void build(int n, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < n) {
                create(x, lx, rx, 0);
            }

            return;
        }

        int mid = (lx + rx) / 2;

        build(n, 2 * x + 1, lx, mid);
        build(n, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void build(int n) {
        build(n, 0, 0, siz);
    }

    void rupd(int l, int r, T v, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return;
        if (lx >= l and rx <= r) {
            modify(x, lx, rx, v);
            propagate(x, lx, rx);
            return;
        }

        int mid = (lx + rx) / 2;

        rupd(l, r, v, 2 * x + 1, lx, mid);
        rupd(l, r, v, 2 * x + 2, mid, rx);

        merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
    }

    void rupd(int l, int r, T v) {
        rupd(l, r + 1, v, 0, 0, siz);
    }

    data query(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);

        if (lx >= r or rx <= l) return d_neutral;
        if (lx >= l and rx <= r) return tr[x];

        int mid = (lx + rx) / 2;

        data curr;
        data left = query(l, r, 2 * x + 1, lx, mid);
        data right = query(l, r, 2 * x + 2, mid, rx);

        merge(curr, left, right);
        return curr;
    }

    data query(int l, int r) {
        return query(l, r + 1, 0, 0, siz);
    }
};

std::vector<int> distribute_candies(std::vector<int> b, std::vector<int> L,
                                    std::vector<int> R, std::vector<int> V) {
    ll n = sz(b), q = sz(L);
    vector<pll> enter[n+5], leave[n+5];

    rep(i,q){
        ll l = L[i], r = R[i], v = V[i];
        enter[l].pb({v,i+2});
        leave[r+1].pb({v,i+2});
    }

    enter[0].pb({inf2,0});
    enter[0].pb({-inf2,1});
    lazysegtree<ll> st(q+5);
    st.build(q+2);
    
    vector<int> ans(n);

    rep(i,n){
        for(auto [v,id] : enter[i]){
            st.rupd(id,q+1,v);
        }
        for(auto [v,id] : leave[i]){
            st.rupd(id,q+1,-v);
        }

        ll l = 0, r = q+1;
        ll pos = -1;

        while(l <= r){
            ll mid = (l+r) >> 1;
            auto [mn,mx] = st.query(mid,q+1);
            if(mx-mn >= b[i]){
                pos = mid;
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }

        assert(pos != -1);

        auto [suffmn,suffmx] = st.query(pos+1,q+1);
        ll lower_wall = -1;

        if(st.query(pos,pos).mn > suffmx){
            // pos = upper wall touch
            lower_wall = suffmn;
        }
        else{
            // pos = lower wall touch
            lower_wall = suffmx-b[i];
        }

        ans[i] = st.query(q+1,q+1).mn-lower_wall;
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 6 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1345 ms 44348 KB Output is correct
2 Correct 1435 ms 43756 KB Output is correct
3 Correct 1449 ms 43600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 298 ms 31312 KB Output is correct
3 Correct 501 ms 15680 KB Output is correct
4 Correct 1452 ms 45592 KB Output is correct
5 Correct 1497 ms 46000 KB Output is correct
6 Correct 1348 ms 46376 KB Output is correct
7 Correct 1241 ms 45652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 131 ms 27628 KB Output is correct
4 Correct 352 ms 13604 KB Output is correct
5 Correct 1048 ms 40216 KB Output is correct
6 Correct 1052 ms 40152 KB Output is correct
7 Correct 912 ms 40248 KB Output is correct
8 Correct 1038 ms 39848 KB Output is correct
9 Correct 1154 ms 40524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 6 ms 604 KB Output is correct
6 Correct 1345 ms 44348 KB Output is correct
7 Correct 1435 ms 43756 KB Output is correct
8 Correct 1449 ms 43600 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 298 ms 31312 KB Output is correct
11 Correct 501 ms 15680 KB Output is correct
12 Correct 1452 ms 45592 KB Output is correct
13 Correct 1497 ms 46000 KB Output is correct
14 Correct 1348 ms 46376 KB Output is correct
15 Correct 1241 ms 45652 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 131 ms 27628 KB Output is correct
19 Correct 352 ms 13604 KB Output is correct
20 Correct 1048 ms 40216 KB Output is correct
21 Correct 1052 ms 40152 KB Output is correct
22 Correct 912 ms 40248 KB Output is correct
23 Correct 1038 ms 39848 KB Output is correct
24 Correct 1154 ms 40524 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 392 ms 13648 KB Output is correct
27 Correct 261 ms 30804 KB Output is correct
28 Correct 1394 ms 44380 KB Output is correct
29 Correct 1352 ms 44624 KB Output is correct
30 Correct 1337 ms 44884 KB Output is correct
31 Correct 1296 ms 45076 KB Output is correct
32 Correct 1247 ms 45220 KB Output is correct