Submission #834817

# Submission time Handle Problem Language Result Execution time Memory
834817 2023-08-22T20:21:57 Z JakobZorz Distributing Candies (IOI21_candies) C++17
0 / 100
424 ms 31408 KB
#include"candies.h"
#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;
typedef long long ll;
 
int n,days;
const int 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(int 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(int 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(int node,int l,int r,int pos,int val){
    //cout<<l<<" "<<r<<endl;
    if(r<=pos)
        return;
    
    if(pos<=l){
        lazy[node]+=val;
        update_node(node);
        return;
    }
    
    int 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(int pos,int val){
    tree_add(1,0,TREE_SIZE,pos,val);
}
 
pair<pair<ll,ll>,int>tree_get2(int node,int l,int r,ll p_max,ll p_min,int 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";
    int 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);
    }
}
 
int tree_get(int bound){
    /*for(int i=0;i<days;i++)
        cout<<mn[TREE_SIZE+i]<<" ";
    cout<<endl;*/
    update_node(1);
    if(mx[1]-mn[1]<bound)
        return mn[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";=
    
    if(res.first.first==mn[TREE_SIZE+res.second])
        return mn[TREE_SIZE+days-1]-res.first.second;
    else
        return bound-(res.first.first-mn[TREE_SIZE+days-1]);
    
}
 
vector<int>distribute_candies(vector<int>c,vector<int>l,vector<int>r,vector<int>v){
    n=(int)c.size();
    days=(int)v.size()+1;
    vector<vector<pair<int,int>>>events;
    events.resize(n+1);
    
    for(int 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(int i=0;i<n;i++){
        for(auto event:events[i])
            tree_add(event.first,event.second);
        s[i]=tree_get(c[i]);
    }
    
    return s;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12572 KB Output is correct
2 Incorrect 5 ms 12628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 424 ms 31408 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 12628 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12624 KB Output is correct
2 Correct 5 ms 12628 KB Output is correct
3 Incorrect 119 ms 20752 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12572 KB Output is correct
2 Incorrect 5 ms 12628 KB Output isn't correct
3 Halted 0 ms 0 KB -