Submission #435812

# Submission time Handle Problem Language Result Execution time Memory
435812 2021-06-23T18:26:06 Z Dormi Distributing Candies (IOI21_candies) C++17
100 / 100
664 ms 32000 KB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
int sz(const T &a){return int(a.size());}
const int MN=2e5+3;
int n,q;
struct seg{
    struct node{
        ll mi,ma,lazy;
        node(){
            mi=0,ma=0,lazy=0;
        }
        void apply(ll v){
            lazy+=v;
            mi+=v;
            ma+=v;
        }
    }t[2*MN];
    void prop(int ind, int le, int ri){
        int mid=(le+ri)/2;
        int left=ind+1,right=ind+(mid-le+1)*2;
        t[left].apply(t[ind].lazy);
        t[right].apply(t[ind].lazy);
        t[ind].lazy=0;
    }
    void update(int ind, int le, int ri, int l, int r, ll val){
        if(l>ri||r<le)return;
        if(le>=l&&ri<=r){
            t[ind].apply(val);
            return;
        }
        prop(ind,le,ri);
        int mid=(le+ri)/2;
        int left=ind+1,right=ind+(mid-le+1)*2;
        update(left,le,mid,l,r,val),update(right,mid+1,ri,l,r,val);
        t[ind].mi=min(t[left].mi,t[right].mi);
        t[ind].ma=max(t[left].ma,t[right].ma);
    }
    ll query(int ind, int le, int ri, int l, int r){
        if(l>ri||r<le)return LLONG_MAX;
        if(le>=l&&ri<=r)return t[ind].mi;
        prop(ind,le,ri);
        int mid=(le+ri)/2;
        int left=ind+1,right=ind+(mid-le+1)*2;
        return min(query(left,le,mid,l,r),query(right,mid+1,ri,l,r));
    }
    ll walk(int ind, int le, int ri, ll c, ll mi, ll ma){
        if(le==ri){
            if(t[ind].mi<mi){
                return query(0,0,q+1,q+1,q+1)-(ma-c);
            }
            else{
                return query(0,0,q+1,q+1,q+1)-mi;
            }
        }
        prop(ind,le,ri);
        int mid=(le+ri)/2;
        int left=ind+1,right=ind+(mid-le+1)*2;
        if(max(t[right].ma,ma)-min(t[right].mi,mi)>=c)return walk(right,mid+1,ri,c,mi,ma);
        return walk(left,le,mid,c,min(mi,t[right].mi),max(ma,t[right].ma));
    }
}tree;
vector<pair<int,ll>> updates[MN];
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
    n=sz(c),q=sz(l);
    tree.update(0,0,q+1,0,q+1,2e9);
    tree.update(0,0,q+1,1,q+1,-2e9);
    for(int i=0;i<q;i++){
        updates[l[i]].push_back({i+2,v[i]});
        if(r[i]+1<n)updates[r[i]+1].push_back({i+2,-v[i]});
    }
    vector<int> ans(n);
    for(int i=0;i<n;i++){
        for(auto x:updates[i])tree.update(0,0,q+1,x.first,q+1,x.second);
        ans[i]=tree.walk(0,0,q+1,c[i],LLONG_MAX,LLONG_MIN);
    }
    return ans;
}


//int main(){
//    cin.tie(NULL);
//    ios_base::sync_with_stdio(false);
//    vector<int> ans=distribute_candies({10, 15, 13}, {0, 0}, {2, 1}, {20, -11});
//    for(auto x:ans)printf("%d ",x);
//    return 0;
//}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 10 ms 14284 KB Output is correct
3 Correct 10 ms 14540 KB Output is correct
4 Correct 9 ms 14540 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 617 ms 32000 KB Output is correct
2 Correct 625 ms 31896 KB Output is correct
3 Correct 595 ms 31892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 349 ms 27868 KB Output is correct
3 Correct 119 ms 18032 KB Output is correct
4 Correct 605 ms 31896 KB Output is correct
5 Correct 630 ms 31992 KB Output is correct
6 Correct 649 ms 31916 KB Output is correct
7 Correct 586 ms 31904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14284 KB Output is correct
2 Correct 9 ms 14284 KB Output is correct
3 Correct 157 ms 23244 KB Output is correct
4 Correct 104 ms 16948 KB Output is correct
5 Correct 271 ms 24868 KB Output is correct
6 Correct 269 ms 24884 KB Output is correct
7 Correct 276 ms 24872 KB Output is correct
8 Correct 262 ms 24880 KB Output is correct
9 Correct 304 ms 24868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14284 KB Output is correct
2 Correct 10 ms 14284 KB Output is correct
3 Correct 10 ms 14540 KB Output is correct
4 Correct 9 ms 14540 KB Output is correct
5 Correct 11 ms 14540 KB Output is correct
6 Correct 617 ms 32000 KB Output is correct
7 Correct 625 ms 31896 KB Output is correct
8 Correct 595 ms 31892 KB Output is correct
9 Correct 9 ms 14412 KB Output is correct
10 Correct 349 ms 27868 KB Output is correct
11 Correct 119 ms 18032 KB Output is correct
12 Correct 605 ms 31896 KB Output is correct
13 Correct 630 ms 31992 KB Output is correct
14 Correct 649 ms 31916 KB Output is correct
15 Correct 586 ms 31904 KB Output is correct
16 Correct 8 ms 14284 KB Output is correct
17 Correct 9 ms 14284 KB Output is correct
18 Correct 157 ms 23244 KB Output is correct
19 Correct 104 ms 16948 KB Output is correct
20 Correct 271 ms 24868 KB Output is correct
21 Correct 269 ms 24884 KB Output is correct
22 Correct 276 ms 24872 KB Output is correct
23 Correct 262 ms 24880 KB Output is correct
24 Correct 304 ms 24868 KB Output is correct
25 Correct 8 ms 14284 KB Output is correct
26 Correct 107 ms 16904 KB Output is correct
27 Correct 362 ms 27872 KB Output is correct
28 Correct 593 ms 31920 KB Output is correct
29 Correct 664 ms 31884 KB Output is correct
30 Correct 612 ms 31972 KB Output is correct
31 Correct 626 ms 31884 KB Output is correct
32 Correct 625 ms 31888 KB Output is correct