This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int nmax=1.5e5+42;
int n,l,where[nmax];
set< pair<int/*position*/,int/*id*/> > active;
int BLOCK;
int which_block[nmax];
int IN=0;
vector< pair<int/*position*/,int/*id*/> > BLOCKS[nmax];
pair<int/*photos*/,int/*last from the same block*/> nxt[nmax];
pair<int/*position*/,int/*id*/> help[nmax];
int get_nxt(int who)
{
/*
cout<<"who= "<<who<<endl;
cout<<"where: ";for(int i=0;i<n;i++)cout<<where[i]<<" ";cout<<endl;
cout<<"active: ";for(auto k:active)cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
cout<<"nxt: ";for(int i=0;i<n;i++)cout<<nxt[i].first<<" "<<nxt[i].second<<"\t";cout<<endl;
cout<<"BLOCKS: "<<endl;
for(int i=0;i<(n-1)/BLOCK+1;i++)
{
cout<<i<<" -> ";for(auto k:BLOCKS[i])cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
}
*/
int t=l+where[who];
t++;
set< pair<int/*position*/,int/*id*/> >::iterator it=active.lower_bound({t,0});
if(it==active.end())return -1;
return (*it).second;
}
void build_block(int which)
{
//the last might be wrong
int pointer=BLOCKS[which].size();
if(pointer==0)return;
pointer=pointer-2;
while(pointer>=0&&BLOCKS[which][pointer].first>BLOCKS[which][pointer+1].first)
{
swap(BLOCKS[which][pointer],BLOCKS[which][pointer+1]);
pointer--;
}
pointer=BLOCKS[which].size()-1;
for(int i=BLOCKS[which].size()-1;i>=0;i--)
{
if(BLOCKS[which][i].first+l>=BLOCKS[which][BLOCKS[which].size()-1].first)nxt[BLOCKS[which][i].second]={0,BLOCKS[which][i].second};
else
{
while(BLOCKS[which][i].first+l<BLOCKS[which][pointer-1].first)pointer--;
int moved=BLOCKS[which][pointer].second;
nxt[BLOCKS[which][i].second]={nxt[moved].first+1,nxt[moved].second};
}
}
/*
cout<<"building "<<which<<endl;
for(auto k:BLOCKS[which])cout<<k.first<<" "<<k.second<<"\t";cout<<endl;
for(int i=0;i<given.size();i++)cout<<nxt[given[i].second].first<<" "<<nxt[given[i].second].second<<"\t";cout<<endl;
system("pause");
*/
}
void build()
{
for(int i=0;i<n;i++)
help[i]={where[i],i};
sort(help,help+n);
int total=(n-1)/BLOCK+1;
for(int i=0;i<total;i++)BLOCKS[i]={};
for(int i=0;i<n;i++)
{
BLOCKS[i/BLOCK].push_back({help[i].first,help[i].second});
which_block[help[i].second]=i/BLOCK;
}
for(int i=0;i<total;i++)
{
sort(BLOCKS[i].begin(),BLOCKS[i].end());
build_block(i);
}
}
void init(int N,int L,int X[])
{
n=N;
l=L;
for(int i=0;i<N;i++)
{
where[i]=X[i];
active.insert({where[i],i});
}
BLOCK=sqrt(n*log(n))+1;
build();
}
int update(int id,int val)
{
//cout<<"UPDATE "<<id<<" "<<val<<endl;
if(n==1)return 1;
active.erase({where[id],id});
for(int i=0;true;i++)
if(BLOCKS[which_block[id]][i]==make_pair(where[id],id))
{
BLOCKS[which_block[id]].erase(BLOCKS[which_block[id]].begin()+i);
break;
}
build_block(which_block[id]);
where[id]=val;
set< pair<int/*position*/,int/*id*/> >::iterator it=active.lower_bound({where[id],id});
if(it==active.end())it--;
int new_block=which_block[(*it).second];
which_block[id]=new_block;
active.insert({where[id],id});
BLOCKS[which_block[id]].push_back({where[id],id});
build_block(which_block[id]);
if(BLOCKS[which_block[id]].size()>2*BLOCK)build();
int ret=0,who=(*active.begin()).second;
while(who!=-1)
{
ret+=nxt[who].first;
who=nxt[who].second;
ret++;
who=get_nxt(who);
}
return ret;
}
/*
int N_,L_,X_[nmax];
int main()
{
cin>>N_>>L_;
for(int i=0;i<N_;i++)cin>>X_[i];
init(N_,L_,X_);
int q;
cin>>q;
for(int i=1;i<=q;i++)
{
int pos,val;
cin>>pos>>val;
cout<<update(pos,val)<<endl;
}
cout<<update(0,10)<<endl;
cout<<update(5,11)<<endl;
cout<<update(7,12)<<endl;
cout<<update(8,13)<<endl;
cout<<update(4,14)<<endl;
cout<<update(6,15)<<endl;
return 0;
}
*/
Compilation message (stderr)
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:155:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(BLOCKS[which_block[id]].size()>2*BLOCK)build();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |