Submission #998372

# Submission time Handle Problem Language Result Execution time Memory
998372 2024-06-13T18:12:21 Z vladilius Food Court (JOI21_foodcourt) C++17
0 / 100
730 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
const ll inf = numeric_limits<ll> :: max();

struct STB{
    struct data{
        ll min, min2, sum;
        int cnt;
    };
    vector<data> t;
    vector<ll> pa, pm;
    // a[i] += x (l <= i <= r)
    // a[i] = max(a[i], x) (l <= i <= r)
    // ? a[i]
    int n;
    STB(int ns){
        n = ns;
        t.resize(4 * n);
        for (int i = 1; i < t.size(); i++) t[i].min2 = inf;
        pa.resize(4 * n);
        pm.assign(4 * n, -inf);
    }
    void merge(int& v, int& vv){
        t[v].min = min(t[vv].min, t[vv + 1].min);
        if (t[vv].min == t[vv + 1].min){
            t[v].min2 = min(t[vv].min2, t[vv + 1].min2);
        }
        else {
            if (t[vv].min < t[vv + 1].min){
                t[v].min2 = min(t[vv + 1].min, t[vv].min2);
            }
            else {
                t[v].min2 = min(t[vv].min, t[vv + 1].min2);
            }
        }
        t[v].cnt = 0;
        if (t[vv].min == t[v].min) t[v].cnt += t[vv].cnt;
        if (t[vv + 1].min == t[v].min) t[v].cnt += t[vv + 1].cnt;
        t[v].sum = t[vv].sum + t[vv + 1].sum;
    }
    void apply_a(int v, ll& x, int len){
        if (!x) return;
        t[v].sum += x * len;
        t[v].min += x;
        if (t[v].min2 != inf) t[v].min2 += x;
        pa[v] += x;
    }
    void apply_m(int v, ll& x){
        if (x == -inf || t[v].min >= x) return;
        t[v].sum += (x - t[v].min) * t[v].cnt;
        t[v].min = x;
        pm[v] = max(pm[v], x);
    }
    void push(int& v, int& vv, int& tl, int& tm, int& tr){
        apply_a(vv, pa[v], tm - tl + 1);
        apply_a(vv + 1, pa[v], tr - tm);
        pa[v] = 0;
        
        apply_m(vv, pm[v]);
        apply_m(vv + 1, pm[v]);
        pm[v] = -inf;
    }
    void add(int v, int tl, int tr, int& l, int& r, ll& x){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            apply_a(v, x, tr - tl + 1);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        push(v, vv, tl, tm, tr);
        add(vv, tl, tm, l, r, x);
        add(vv + 1, tm + 1, tr, l, r, x);
        merge(v, vv);
    }
    void add(int l, int r, ll x){
        add(1, 1, n, l, r, x);
    }
    void chmax(int v, int tl, int tr, int& l, int& r, ll& x){
        if (l > tr || r < tl || t[v].min >= x) return;
        if (l <= tl && tr <= r && t[v].min2 > x){
            apply_m(v, x);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        push(v, vv, tl, tm, tr);
        chmax(vv, tl, tm, l, r, x);
        chmax(vv + 1, tm + 1, tr, l, r, x);
        merge(v, vv);
    }
    void chmax(int l, int r, ll x){
        chmax(1, 1, n, l, r, x);
    }
    ll get(int v, int tl, int tr, int& p){
        if (tl == tr){
            return t[v].min;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        push(v, vv, tl, tm, tr);
        if (p <= tm){
            return get(vv, tl, tm, p);
        }
        return get(vv + 1, tm + 1, tr, p);
    }
    ll get(int p){
        return get(1, 1, n, p);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m, q; cin>>n>>m>>q;
    STB C(n);
    const int S = n;
    const int sz = ceil(1.0 * n / S) - 1;
    vector<int> st[n + 1], st1[sz + 5];
    vector<ll> pr[n + 1], pr1[sz + 5];
    
    vector<int> bc(n + 1);
    int i = 1, j = S, k = 0;
    while (i <= n){
        while (i <= j) bc[i++] = k;
        j = min(n, j + S);
        k++;
    }

    vector<int> col(q + 1);
    for (int tt = 1; tt <= q; tt++){
        int type; cin>>type;
        if (type == 1){
            int l, r, c, k; cin>>l>>r>>c>>k;
            col[tt] = c;
            C.add(l, r, k);
            int bl = bc[l] + 1, br = bc[r] - 1;
            if (bl > br){
                for (int i = l; i <= r; i++){
                    st[i].pb(tt);
                    if (pr[i].empty()) pr[i].pb(k);
                    else pr[i].pb(pr[i].back() + k);
                }
                continue;
            }
            for (int i = bl; i <= br; i++){
                st1[i].pb(tt);
                if (pr1[i].empty()) pr1[i].pb(k);
                else pr1[i].pb(pr1[i].back() + k);
            }
            for (int i = l; i <= S * bl; i++){
                st[i].pb(tt);
                if (pr[i].empty()) pr[i].pb(k);
                else pr[i].pb(pr[i].back() + k);
            }
            for (int i = S * (br + 1) + 1; i <= r; i++){
                st[i].pb(tt);
                if (pr[i].empty()) pr[i].pb(k);
                else pr[i].pb(pr[i].back() + k);
            }
        }
        else if (type == 2){
            int l, r, k; cin>>l>>r>>k;
            C.add(l, r, -k);
            C.chmax(1, n, 0);
        }
        else {
            int a; ll b; cin>>a>>b;
            ll v = C.get(a);
            if (b > v){
                cout<<0<<"\n";
                continue;
            }
            b -= v;
            if (!pr1[bc[a]].empty()) b += pr1[bc[a]].back();
            if (!pr[a].empty()) b += pr[a].back();
            
            auto low = [&](vector<int>& x, int t){
                if (x.empty() || x[0] > t) return -1;
                int l = 0, r = (int) x.size() - 1;
                while (l + 1 < r){
                    int m = (l + r) / 2;
                    if (x[m] <= t){
                        l = m;
                    }
                    else {
                        r = m - 1;
                    }
                }
                if (x[r] <= t) l = r;
                return l;
            };
            
            auto check = [&](int m){
                int j1 = low(st1[bc[a]], m);
                int j2 = low(st[a], m);
                ll val = 0;
                if (j1 >= 0) val += pr1[bc[a]][j1];
                if (j2 >= 0) val += pr[a][j2];
                return (val >= b);
            };
            
            int l = 1, r = tt;
            while (l + 1 < r){
                int m = (l + r) / 2;
                if (check(m)){
                    r = m;
                }
                else {
                    l = m + 1;
                }
            }
            if (check(l)) r = l;
            cout<<col[r]<<"\n";
        }
    }
}

Compilation message

foodcourt.cpp: In constructor 'STB::STB(int)':
foodcourt.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<STB::data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for (int i = 1; i < t.size(); i++) t[i].min2 = inf;
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 93 ms 20212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 730 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 533 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -