Submission #940966

# Submission time Handle Problem Language Result Execution time Memory
940966 2024-03-08T03:29:53 Z 7mody Distributing Candies (IOI21_candies) C++17
100 / 100
2612 ms 51284 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 = st.getMIN(1, m + 1);
 
        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){
            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;
}
# 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 3 ms 604 KB Output is correct
5 Correct 8 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2037 ms 44408 KB Output is correct
2 Correct 2336 ms 48672 KB Output is correct
3 Correct 2306 ms 48516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 470 ms 28400 KB Output is correct
3 Correct 808 ms 14688 KB Output is correct
4 Correct 2564 ms 50468 KB Output is correct
5 Correct 2533 ms 50904 KB Output is correct
6 Correct 1860 ms 51284 KB Output is correct
7 Correct 1808 ms 50640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 496 KB Output is correct
3 Correct 325 ms 27584 KB Output is correct
4 Correct 497 ms 12436 KB Output is correct
5 Correct 1779 ms 39360 KB Output is correct
6 Correct 1715 ms 39216 KB Output is correct
7 Correct 1629 ms 39220 KB Output is correct
8 Correct 1783 ms 39212 KB Output is correct
9 Correct 2612 ms 39212 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 3 ms 604 KB Output is correct
5 Correct 8 ms 860 KB Output is correct
6 Correct 2037 ms 44408 KB Output is correct
7 Correct 2336 ms 48672 KB Output is correct
8 Correct 2306 ms 48516 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 470 ms 28400 KB Output is correct
11 Correct 808 ms 14688 KB Output is correct
12 Correct 2564 ms 50468 KB Output is correct
13 Correct 2533 ms 50904 KB Output is correct
14 Correct 1860 ms 51284 KB Output is correct
15 Correct 1808 ms 50640 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 496 KB Output is correct
18 Correct 325 ms 27584 KB Output is correct
19 Correct 497 ms 12436 KB Output is correct
20 Correct 1779 ms 39360 KB Output is correct
21 Correct 1715 ms 39216 KB Output is correct
22 Correct 1629 ms 39220 KB Output is correct
23 Correct 1783 ms 39212 KB Output is correct
24 Correct 2612 ms 39212 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 510 ms 13716 KB Output is correct
27 Correct 426 ms 31228 KB Output is correct
28 Correct 1941 ms 49152 KB Output is correct
29 Correct 1884 ms 49748 KB Output is correct
30 Correct 1924 ms 49732 KB Output is correct
31 Correct 1817 ms 49928 KB Output is correct
32 Correct 1764 ms 50392 KB Output is correct