#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
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();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3840 KB |
Output is correct |
3 |
Correct |
3 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3840 KB |
Output is correct |
3 |
Correct |
3 ms |
3840 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
3 ms |
3968 KB |
Output is correct |
6 |
Correct |
3 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3840 KB |
Output is correct |
3 |
Correct |
3 ms |
3840 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
3 ms |
3968 KB |
Output is correct |
6 |
Correct |
3 ms |
3840 KB |
Output is correct |
7 |
Correct |
362 ms |
5752 KB |
Output is correct |
8 |
Correct |
452 ms |
6136 KB |
Output is correct |
9 |
Correct |
689 ms |
8824 KB |
Output is correct |
10 |
Correct |
782 ms |
8824 KB |
Output is correct |
11 |
Correct |
903 ms |
8812 KB |
Output is correct |
12 |
Correct |
1027 ms |
9080 KB |
Output is correct |
13 |
Correct |
1117 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3840 KB |
Output is correct |
3 |
Correct |
3 ms |
3840 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
3 ms |
3968 KB |
Output is correct |
6 |
Correct |
3 ms |
3840 KB |
Output is correct |
7 |
Correct |
362 ms |
5752 KB |
Output is correct |
8 |
Correct |
452 ms |
6136 KB |
Output is correct |
9 |
Correct |
689 ms |
8824 KB |
Output is correct |
10 |
Correct |
782 ms |
8824 KB |
Output is correct |
11 |
Correct |
903 ms |
8812 KB |
Output is correct |
12 |
Correct |
1027 ms |
9080 KB |
Output is correct |
13 |
Correct |
1117 ms |
8824 KB |
Output is correct |
14 |
Correct |
570 ms |
6392 KB |
Output is correct |
15 |
Correct |
622 ms |
6696 KB |
Output is correct |
16 |
Correct |
1552 ms |
9024 KB |
Output is correct |
17 |
Correct |
1864 ms |
10796 KB |
Output is correct |
18 |
Correct |
2041 ms |
10784 KB |
Output is correct |
19 |
Correct |
1960 ms |
10628 KB |
Output is correct |
20 |
Correct |
1839 ms |
10872 KB |
Output is correct |
21 |
Correct |
1811 ms |
10892 KB |
Output is correct |
22 |
Correct |
1747 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3840 KB |
Output is correct |
3 |
Correct |
3 ms |
3840 KB |
Output is correct |
4 |
Correct |
2 ms |
3968 KB |
Output is correct |
5 |
Correct |
3 ms |
3968 KB |
Output is correct |
6 |
Correct |
3 ms |
3840 KB |
Output is correct |
7 |
Correct |
362 ms |
5752 KB |
Output is correct |
8 |
Correct |
452 ms |
6136 KB |
Output is correct |
9 |
Correct |
689 ms |
8824 KB |
Output is correct |
10 |
Correct |
782 ms |
8824 KB |
Output is correct |
11 |
Correct |
903 ms |
8812 KB |
Output is correct |
12 |
Correct |
1027 ms |
9080 KB |
Output is correct |
13 |
Correct |
1117 ms |
8824 KB |
Output is correct |
14 |
Correct |
570 ms |
6392 KB |
Output is correct |
15 |
Correct |
622 ms |
6696 KB |
Output is correct |
16 |
Correct |
1552 ms |
9024 KB |
Output is correct |
17 |
Correct |
1864 ms |
10796 KB |
Output is correct |
18 |
Correct |
2041 ms |
10784 KB |
Output is correct |
19 |
Correct |
1960 ms |
10628 KB |
Output is correct |
20 |
Correct |
1839 ms |
10872 KB |
Output is correct |
21 |
Correct |
1811 ms |
10892 KB |
Output is correct |
22 |
Correct |
1747 ms |
10576 KB |
Output is correct |
23 |
Correct |
5180 ms |
18628 KB |
Output is correct |
24 |
Correct |
5600 ms |
18624 KB |
Output is correct |
25 |
Correct |
4372 ms |
18620 KB |
Output is correct |
26 |
Correct |
4806 ms |
18700 KB |
Output is correct |
27 |
Correct |
6559 ms |
18652 KB |
Output is correct |
28 |
Correct |
830 ms |
9056 KB |
Output is correct |
29 |
Correct |
835 ms |
9036 KB |
Output is correct |
30 |
Correct |
840 ms |
9080 KB |
Output is correct |
31 |
Correct |
821 ms |
9080 KB |
Output is correct |
32 |
Correct |
5449 ms |
23144 KB |
Output is correct |
33 |
Correct |
3210 ms |
22460 KB |
Output is correct |
34 |
Correct |
4241 ms |
23336 KB |
Output is correct |
35 |
Correct |
3120 ms |
23672 KB |
Output is correct |
36 |
Correct |
2005 ms |
23164 KB |
Output is correct |
37 |
Correct |
5745 ms |
23964 KB |
Output is correct |
38 |
Correct |
5910 ms |
22344 KB |
Output is correct |
39 |
Correct |
6868 ms |
23416 KB |
Output is correct |
40 |
Correct |
6054 ms |
22392 KB |
Output is correct |
41 |
Correct |
7467 ms |
23352 KB |
Output is correct |
42 |
Correct |
7532 ms |
23584 KB |
Output is correct |