Submission #548552

# Submission time Handle Problem Language Result Execution time Memory
548552 2022-04-13T19:13:00 Z FEDIKUS Distributing Candies (IOI21_candies) C++17
100 / 100
376 ms 34848 KB
#include "candies.h"

#include<iostream>
#include <vector>

using namespace std;

typedef long long ll;

const ll maxn=2e5+10;
const ll inf=1e9+10;

struct node{
    ll min,max,lazy;
};

ll n,q,val=0;
vector<pair<ll,ll> > events[maxn];
node segt[4*maxn];

node comb(node a,node b){
    node ret={0,0,0};
    ret.max=max(a.max,b.max);
    ret.min=min(a.min,b.min);
    return ret;
}

void push(ll ind,ll l,ll r){
    if(l!=r){
        segt[2*ind].min+=segt[ind].lazy; segt[2*ind].max+=segt[ind].lazy;
        segt[2*ind+1].min+=segt[ind].lazy; segt[2*ind+1].max+=segt[ind].lazy;
        segt[2*ind].lazy+=segt[ind].lazy;
        segt[2*ind+1].lazy+=segt[ind].lazy;
    }
    segt[ind].lazy=0;
}

void update(ll tl,ll tr,ll v,ll ind=1,ll l=0,ll r=q-1){
    push(ind,l,r);
    if(tl<=l && r<=tr){
        segt[ind].lazy+=v;
        segt[ind].min+=v;
        segt[ind].max+=v;
        return;
    }
    ll mid=l+(r-l)/2;
    if(tl<=mid) update(tl,tr,v,2*ind,l,mid);
    if(tr>mid) update(tl,tr,v,2*ind+1,mid+1,r);
    segt[ind]=comb(segt[2*ind],segt[2*ind+1]);
}

ll query(ll c,vector<int> &v){
    ll ind=1,l=0,r=q-1,res=-1;
    ll tmax=-inf*inf,tmin=inf*inf;
    while(l!=r){ //trazim prvi na kome je razlika <=c
        push(ind,l,r);
        if(max(tmax,segt[2*ind+1].max)-min(tmin,segt[2*ind+1].min)<=c){
            r=l+(r-l)/2;
            tmax=max(tmax,segt[2*ind+1].max);
            tmin=min(tmin,segt[2*ind+1].min);
            ind=2*ind;
        }else{
            ind=2*ind+1;
            l=l+(r-l)/2+1;
        }
    }
    if(v[l+1]>0){
        return val-(tmax-c);
    }else{
        return val-tmin;
    }
}

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
    n=c.size();
    q=l.size()+2;
    l.insert(l.begin(),0); l.insert(l.begin(),0);
    r.insert(r.begin(),n-1); r.insert(r.begin(),n-1);
    v.insert(v.begin(),-inf); v.insert(v.begin(),0);
    for(ll i=0;i<q;i++){
        events[l[i]].push_back({i,v[i]});
        events[r[i]+1].push_back({i,-v[i]});
    }
    vector<int> ans(n);
    for(ll i=0;i<n;i++){
        for(auto event:events[i]){
            val+=event.second;
            update(event.first,q-1,event.second);
        }
        ans[i]=query(c[i],v);
    }
    return ans;
}

Compilation message

candies.cpp: In function 'll query(ll, std::vector<int>&)':
candies.cpp:53:24: warning: unused variable 'res' [-Wunused-variable]
   53 |     ll ind=1,l=0,r=q-1,res=-1;
      |                        ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 362 ms 34836 KB Output is correct
2 Correct 367 ms 34764 KB Output is correct
3 Correct 357 ms 34800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 236 ms 30876 KB Output is correct
3 Correct 70 ms 8736 KB Output is correct
4 Correct 362 ms 34844 KB Output is correct
5 Correct 376 ms 34712 KB Output is correct
6 Correct 373 ms 34840 KB Output is correct
7 Correct 370 ms 34836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 119 ms 29616 KB Output is correct
4 Correct 64 ms 7692 KB Output is correct
5 Correct 185 ms 31228 KB Output is correct
6 Correct 192 ms 31240 KB Output is correct
7 Correct 190 ms 31316 KB Output is correct
8 Correct 181 ms 31112 KB Output is correct
9 Correct 185 ms 31192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 5 ms 5204 KB Output is correct
6 Correct 362 ms 34836 KB Output is correct
7 Correct 367 ms 34764 KB Output is correct
8 Correct 357 ms 34800 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 236 ms 30876 KB Output is correct
11 Correct 70 ms 8736 KB Output is correct
12 Correct 362 ms 34844 KB Output is correct
13 Correct 376 ms 34712 KB Output is correct
14 Correct 373 ms 34840 KB Output is correct
15 Correct 370 ms 34836 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 119 ms 29616 KB Output is correct
19 Correct 64 ms 7692 KB Output is correct
20 Correct 185 ms 31228 KB Output is correct
21 Correct 192 ms 31240 KB Output is correct
22 Correct 190 ms 31316 KB Output is correct
23 Correct 181 ms 31112 KB Output is correct
24 Correct 185 ms 31192 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 65 ms 7636 KB Output is correct
27 Correct 209 ms 30908 KB Output is correct
28 Correct 371 ms 34848 KB Output is correct
29 Correct 352 ms 34820 KB Output is correct
30 Correct 363 ms 34712 KB Output is correct
31 Correct 359 ms 34824 KB Output is correct
32 Correct 364 ms 34712 KB Output is correct