#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<<11;
vector<ll>mn;
vector<ll>mx;
void tree_init(){
mn.resize(TREE_SIZE*2);
mx.resize(TREE_SIZE*2);
}
void update_node(int node){
if(node>=TREE_SIZE){
mn[node]=mx[node];
return;
}
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(node>=TREE_SIZE){
mx[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";
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;*/
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";
/*int min_i=days-1,max_i=days-1;
for(int i=days-1;i>=0;i--){
if(mn[TREE_SIZE+i]<mn[TREE_SIZE+min_i])
min_i=i;
if(mn[TREE_SIZE+i]>mn[TREE_SIZE+max_i])
max_i=i;
if(mn[TREE_SIZE+max_i]-mn[TREE_SIZE+min_i]>=bound){
if(max_i==i){
//cout<<mn[TREE_SIZE+max_i]<<" "<<mn[TREE_SIZE+min_i]<<" "<<i<<"\n";
return mn[TREE_SIZE+days-1]-mn[TREE_SIZE+min_i];
}else{
//cout<<mn[TREE_SIZE+max_i]<<" "<<mn[TREE_SIZE+min_i]<<" "<<i<<"\n";
return bound-(mn[TREE_SIZE+max_i]-mn[TREE_SIZE+days-1]);
}
}
}*/
if(res.first.first==mn[TREE_SIZE+res.second]){
//cout<<"a\n";
return mn[TREE_SIZE+days-1]-res.first.second;
}else{
//cout<<"b\n";
return bound-(res.first.first-mn[TREE_SIZE+days-1]);
}
//cout<<"?\n";
//return mn[TREE_SIZE+days-1]-mn[TREE_SIZE+min_i];
return 0;
}
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);
if(n>2000||days>2001)
return{};
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
29 ms |
496 KB |
Output is correct |
4 |
Correct |
32 ms |
436 KB |
Output is correct |
5 |
Correct |
33 ms |
572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
64 ms |
12128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
41 ms |
5400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
43 ms |
5360 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
29 ms |
496 KB |
Output is correct |
4 |
Correct |
32 ms |
436 KB |
Output is correct |
5 |
Correct |
33 ms |
572 KB |
Output is correct |
6 |
Incorrect |
64 ms |
12128 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |