Submission #940729

# Submission time Handle Problem Language Result Execution time Memory
940729 2024-03-07T14:24:10 Z 7mody Distributing Candies (IOI21_candies) C++17
100 / 100
2573 ms 51304 KB
#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 

struct seg{
    int sz = 1; vector<ll>mn,mx,sum,op,add;
    seg(int n){
        while(sz < n) sz*=2;
        mn.assign(sz*2,0ll);
        mx.assign(sz*2,0ll);
        op.assign(sz*2,0ll);
        sum.assign(sz*2,0ll);
        add.assign(sz*2,0ll);
    }
    void push(int x,int lx,int rx){
        if(rx-lx == 1) return;
        int m = (lx+rx)/2;
        add[x*2+1] += add[x];
        add[x*2+2] += add[x];
        sum[x*2+1] += add[x] * (m-lx);
        sum[x*2+2] += add[x] * (rx-m);
        sum[x] = sum[x*2+1] + sum[x*2+2];
        add[x] = 0;
    }
    void update(int l,int r,ll v,int x=0,int lx=0,int rx=-1){
        if(rx == -1){r++;rx=sz;}
        push(x,lx,rx);
        if(rx <= l || lx >= r) return;
        if(rx <= r && lx >= l){
            op[x] += v;
            mn[x] += v;
            mx[x] += v;
            add[x] += v;
            sum[x] += (rx-lx)*v;
            return;
}
        int m = (lx+rx)/2;
        update(l,r,v,x*2+1,lx,m);
        update(l,r,v,x*2+2,m,rx);
        mn[x] = min(mn[x*2+1] , mn[x*2+2]) + op[x];
        mx[x] = max(mx[x*2+1] , mx[x*2+2]) + op[x];
        sum[x] = sum[x*2+1] + sum[x*2+2];}
    ll getMIN(int l,int r,int x=0,int lx=0,int rx=-1){
        if(rx == -1){rx=sz;r++;}
        if(rx <= l || lx >= r) return 1e15;
        if(rx <= r && lx >= l) return mn[x];
        int m = (lx+rx)/2;
        return min(getMIN(l,r,x*2+1,lx,m),getMIN(l,r,x*2+2,m,rx)) + op[x];}
    ll getMAX(int l,int r,int x=0,int lx=0,int rx=-1){
        if(rx == -1){rx=sz;r++;}
        if(rx <= l || lx >= r) return -1e15;
        if(rx <= r && lx >= l) return mx[x];
        int m = (lx+rx)/2;
        return max(getMAX(l,r,x*2+1,lx,m),getMAX(l,r,x*2+2,m,rx)) + op[x];}
    ll getSUM(int l,int r,int x=0,int lx=0,int rx=-1){
        if(rx == -1){rx=sz;r++;}
        push(x,lx,rx);
        if(rx <= l || lx >= r) return 0ll;
        if(rx <= r && lx >= l) return sum[x];
        int m = (lx+rx)/2;
        return getSUM(l,r,x*2+1,lx,m)+getSUM(l,r,x*2+2,m,rx);}};

int n, m;

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

    vector<vector<int>> L(n),R(n);

    for(int i=0; i < m;i++){
        L[l[i]].push_back(i);
        R[r[i]].push_back(i);
    }

    seg st(m + 2);
    // - 0 ,
    for(int i=0; i < n;i++){

        for(int x : L[i]) st.update(x + 2, m + 1, v[x]);

        int l = 0;
        int r = m;
        int id = -1;

        ll cmx, cmn;

        while(l <= r){
            int mid = (l+r)/2;
            ll a,b;
            a = st.getMAX(mid + 1, m + 1), b = st.getMIN(mid + 1, m + 1);
            if(a - b >= c[i]){
                l = mid+1;
                id = mid;
                cmx = a;
                cmn = b;
            } else r = mid-1;
        }

        // cout << cmx << ' ' << cmn << endl;
        if(id == -1){
            l = 0;
            r = m;
            int limit = st.getMIN(1, m + 1);
            while(l <= r){
                int mid = (l + r)/2;
                if(st.getMIN(mid + 1, m + 1) == limit){
                    l = mid + 1;
                    id = mid;
                } else r = mid - 1;
            } id++;
            s[i] = st.getSUM(m+1,m+1) - st.getSUM(id,id);
            // s[i] = st.getSUM(m+1,m+1) - cmn;
            for(int x : R[i]) st.update(x + 2, m + 1, -v[x]);
            continue;
        }
        int cond = 0;
        ll curr;
        id++;
        if(cmx == st.getMAX(id + 1, m + 1)) cond = 1, curr = cmx;
        else cond = 0, curr = cmn;
        if(cond){
            int l = id - 1;
            int r = m;
            while(l <= r){
                int mid = (l+r)/2;
                if(st.getMAX(mid + 1, m + 1) == curr){
                    id = mid;
                    l = mid + 1;
                } else r = mid - 1;
            }
            id++;
            // cout << st.getSUM(id + 1, m + 1) << ' ' << id << ' ';
            s[i] = c[i] - (st.getSUM(id, id) - st.getSUM(m+1,m+1));
        } else{
            int l = id - 1;
            int r = m;
            while(l <= r){
                int mid = (l+r)/2;
                if(st.getMIN(mid + 1, m + 1) == curr){
                    id = mid;
                    l = mid + 1;
                } else r = mid - 1;
            }
            id++;
            s[i] = st.getSUM(m+1,m+1) - st.getSUM(id, id);
        }
        for(int x : R[i]) st.update(x + 2, m + 1, -v[x]);
    }
    return s;
}

// void solve(){
//     cin >> n;
//     vector<int> c(n);
//     for(int i=0; i < n;i++) cin >> c[i];
//     int q; cin >> q;
//     vector<int> l(q),r(q),v(q);
//     for(int i=0; i < q;i++) cin >> l[i];
//     for(int i=0; i < q;i++) cin >> r[i];
//     for(int i=0; i < q;i++) cin >> v[i];
//     vector<int> ans = distribute_candies(c,l,r,v); cout << endl;
//     for(int x : ans) cout << x << ' '; cout << endl;
// }

// int32_t main(){
//     ios::sync_with_stdio(false);    
//     cin.tie();
//     cout.tie();
//     int t=1;
//     // cin >> t;
//     while(t--){
//         // cout << t <<  ":\n";
//         solve();
//     }
// }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 8 ms 864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1962 ms 49084 KB Output is correct
2 Correct 2235 ms 48676 KB Output is correct
3 Correct 2326 ms 48720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 448 ms 31184 KB Output is correct
3 Correct 725 ms 14688 KB Output is correct
4 Correct 2573 ms 50512 KB Output is correct
5 Correct 2537 ms 50912 KB Output is correct
6 Correct 1811 ms 51304 KB Output is correct
7 Correct 1875 ms 50652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 329 ms 29952 KB Output is correct
4 Correct 562 ms 13648 KB Output is correct
5 Correct 1993 ms 42212 KB Output is correct
6 Correct 1799 ms 42880 KB Output is correct
7 Correct 1517 ms 43452 KB Output is correct
8 Correct 1995 ms 42104 KB Output is correct
9 Correct 2207 ms 43200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 604 KB Output is correct
5 Correct 8 ms 864 KB Output is correct
6 Correct 1962 ms 49084 KB Output is correct
7 Correct 2235 ms 48676 KB Output is correct
8 Correct 2326 ms 48720 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 448 ms 31184 KB Output is correct
11 Correct 725 ms 14688 KB Output is correct
12 Correct 2573 ms 50512 KB Output is correct
13 Correct 2537 ms 50912 KB Output is correct
14 Correct 1811 ms 51304 KB Output is correct
15 Correct 1875 ms 50652 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 436 KB Output is correct
18 Correct 329 ms 29952 KB Output is correct
19 Correct 562 ms 13648 KB Output is correct
20 Correct 1993 ms 42212 KB Output is correct
21 Correct 1799 ms 42880 KB Output is correct
22 Correct 1517 ms 43452 KB Output is correct
23 Correct 1995 ms 42104 KB Output is correct
24 Correct 2207 ms 43200 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 609 ms 13660 KB Output is correct
27 Correct 434 ms 31232 KB Output is correct
28 Correct 2211 ms 49148 KB Output is correct
29 Correct 2056 ms 49548 KB Output is correct
30 Correct 1988 ms 49736 KB Output is correct
31 Correct 1880 ms 50140 KB Output is correct
32 Correct 1759 ms 50260 KB Output is correct