Submission #834845

# Submission time Handle Problem Language Result Execution time Memory
834845 2023-08-22T21:03:23 Z JakobZorz Distributing Candies (IOI21_candies) C++17
100 / 100
552 ms 41676 KB
#include"candies.h"
#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;
typedef long long ll;
 
ll n,days;
const ll TREE_SIZE=1<<18;
 
vector<ll>mn;
vector<ll>mx;
vector<ll>lazy;
void tree_init(){
    mn.resize(TREE_SIZE*2);
    mx.resize(TREE_SIZE*2);
    lazy.resize(TREE_SIZE*2);
}
 
void update_lazy(ll node){
    if(lazy[node]==0)
        return;
    mx[node]+=lazy[node];
    mn[node]+=lazy[node];
    
    if(node<TREE_SIZE){
        lazy[2*node]+=lazy[node];
        lazy[2*node+1]+=lazy[node];
    }
    lazy[node]=0;
}
 
void update_node(ll node){
    update_lazy(node);
    if(node>=TREE_SIZE){
        mn[node]=mx[node];
        return;
    }
    
    update_lazy(2*node);
    update_lazy(2*node+1);
    mn[node]=min(mn[2*node],mn[2*node+1]);
    mx[node]=max(mx[2*node],mx[2*node+1]);
}
 
void tree_add(ll node,ll l,ll r,ll pos,ll val){
    //cout<<l<<" "<<r<<endl;
    if(r<=pos)
        return;
    
    if(pos<=l){
    //if(node>=TREE_SIZE){
        lazy[node]+=val;
        update_node(node);
        return;
    }
    
    ll m=(l+r)/2;
    tree_add(2*node,l,m,pos,val);
    tree_add(2*node+1,m,r,pos,val);
    update_node(node);
}
 
void tree_add(ll pos,ll val){
    tree_add(1,0,TREE_SIZE,pos,val);
}
 
pair<pair<ll,ll>,ll>tree_get2(ll node,ll l,ll r,ll p_max,ll p_min,ll bound){
    //cout<<l<<" "<<r<<"\n";
    update_node(node);
    if(node>=TREE_SIZE)
        return {{max(p_max,mx[node]),min(p_min,mn[node])},l};
    
    ll p_max2=max(p_max,mx[2*node+1]);
    ll p_min2=min(p_min,mn[2*node+1]);
    //cout<<"pmax2 "<<p_max2<<" pmin2 "<<p_min2<<"\n";
    ll m=(l+r)/2;
    
    if(p_max2-p_min2<bound){
        return tree_get2(2*node,l,m,p_max2,p_min2,bound);
    }else{
        return tree_get2(2*node+1,m,r,p_max,p_min,bound);
    }
}

ll get_idx(ll node,ll l,ll r,ll idx){
    update_node(node);
    if(node>=TREE_SIZE){
        //cout<<"node "<<node<<endl;
        return mn[node];
    }
    
    ll m=(l+r)/2;
    if(idx>=m)
        return get_idx(2*node+1,m,r,idx);
    else
        return get_idx(2*node,l,m,idx);
}
 
ll tree_get(ll bound){
    /*for(int i=0;i<days;i++)
        cout<<mn[TREE_SIZE+i]<<" ";
    cout<<endl;*/
    update_node(1);
    if(mx[1]-mn[1]<bound){
        //cout<<"a"<<endl;
        return get_idx(1,0,TREE_SIZE,days-1)-mn[1];
    }
    
    auto res=tree_get2(1,0,TREE_SIZE,LLONG_MIN,LLONG_MAX,bound);
    //cout<<res.first.first<<" "<<res.first.second<<" "<<res.second<<"\n";=
    
    /*cout<<"res.second "<<TREE_SIZE+res.second<<endl;
    cout<<get_idx(1,0,TREE_SIZE,res.second)<<endl;
    cout<<mn[TREE_SIZE+res.second]<<endl;
    cout<<endl;*/
    if(res.first.first==get_idx(1,0,TREE_SIZE,res.second)){
        //cout<<"b"<<endl;
        return get_idx(1,0,TREE_SIZE,days-1)-res.first.second;
    }else{
        //cout<<"c "<<bound<<" "<<bound-(res.first.first-get_idx(1,0,TREE_SIZE,days-1))<<endl;
        return bound-(res.first.first-get_idx(1,0,TREE_SIZE,days-1));
    }
}
 
vector<int>distribute_candies(vector<int>c,vector<int>l,vector<int>r,vector<int>v){
    n=c.size();
    days=v.size()+1;
    vector<vector<pair<ll,ll>>>events;
    events.resize(n+1);
    
    for(ll i=0;i<days-1;i++){
        events[l[i]].push_back({i+1,v[i]});
        events[r[i]+1].push_back({i+1,-v[i]});
    }
    
    vector<int>s(n);
    tree_init();
    
    for(ll i=0;i<n;i++){
        for(auto event:events[i])
            tree_add(event.first,event.second);
        s[i]=tree_get(c[i]);
    }
    
    //return {};
    return s;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12628 KB Output is correct
2 Correct 5 ms 12552 KB Output is correct
3 Correct 7 ms 12760 KB Output is correct
4 Correct 7 ms 12812 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 552 ms 34820 KB Output is correct
2 Correct 514 ms 38916 KB Output is correct
3 Correct 509 ms 38604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12628 KB Output is correct
2 Correct 271 ms 26204 KB Output is correct
3 Correct 140 ms 22020 KB Output is correct
4 Correct 523 ms 40652 KB Output is correct
5 Correct 504 ms 41144 KB Output is correct
6 Correct 511 ms 41676 KB Output is correct
7 Correct 530 ms 40896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12628 KB Output is correct
2 Correct 6 ms 12628 KB Output is correct
3 Correct 125 ms 23848 KB Output is correct
4 Correct 122 ms 20944 KB Output is correct
5 Correct 251 ms 34236 KB Output is correct
6 Correct 259 ms 34852 KB Output is correct
7 Correct 249 ms 35436 KB Output is correct
8 Correct 234 ms 34060 KB Output is correct
9 Correct 276 ms 35516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12628 KB Output is correct
2 Correct 5 ms 12552 KB Output is correct
3 Correct 7 ms 12760 KB Output is correct
4 Correct 7 ms 12812 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
6 Correct 552 ms 34820 KB Output is correct
7 Correct 514 ms 38916 KB Output is correct
8 Correct 509 ms 38604 KB Output is correct
9 Correct 5 ms 12628 KB Output is correct
10 Correct 271 ms 26204 KB Output is correct
11 Correct 140 ms 22020 KB Output is correct
12 Correct 523 ms 40652 KB Output is correct
13 Correct 504 ms 41144 KB Output is correct
14 Correct 511 ms 41676 KB Output is correct
15 Correct 530 ms 40896 KB Output is correct
16 Correct 4 ms 12628 KB Output is correct
17 Correct 6 ms 12628 KB Output is correct
18 Correct 125 ms 23848 KB Output is correct
19 Correct 122 ms 20944 KB Output is correct
20 Correct 251 ms 34236 KB Output is correct
21 Correct 259 ms 34852 KB Output is correct
22 Correct 249 ms 35436 KB Output is correct
23 Correct 234 ms 34060 KB Output is correct
24 Correct 276 ms 35516 KB Output is correct
25 Correct 5 ms 12628 KB Output is correct
26 Correct 109 ms 21040 KB Output is correct
27 Correct 258 ms 28820 KB Output is correct
28 Correct 536 ms 39412 KB Output is correct
29 Correct 519 ms 39776 KB Output is correct
30 Correct 528 ms 39956 KB Output is correct
31 Correct 523 ms 40156 KB Output is correct
32 Correct 497 ms 40364 KB Output is correct