#include "elephants.h"
#include <vector>
#include <algorithm>
#include <cstdio>
const int INF=1e9+7;
const int BLK_SIZE=400;
const int PERIOD=400;
int n;
int l;
int xs[150005];
int ys[150005];
struct Block{
std::vector<int> pos;
std::vector<int> reach;
std::vector<int> jumps;
void recalc(){
reach.resize(pos.size());
jumps.resize(pos.size());
int j=pos.size();
for(int i=(int)pos.size()-1;i>=0;i--){
while(pos[j-1]>pos[i]+l) j--;
if(j==(int)pos.size()){
reach[i]=pos[i]+l;
jumps[i]=1;
}else{
reach[i]=reach[j];
jumps[i]=jumps[j]+1;
}
}
/*
fprintf(stderr,"BLOCK\n");
for(int i=0;i<pos.size();i++){
fprintf(stderr,"%d: reach %d, jump %d\n",pos[i],reach[i],jumps[i]);
}
*/
}
void apply(int& x,int& cnt){
auto it=std::upper_bound(pos.begin(),pos.end(),x);
if(it==pos.end()) return;
int i=it-pos.begin();
cnt+=jumps[i];
x=reach[i];
}
};
std::vector<Block> blocks;
void rebuild(){
std::copy(xs,xs+n,ys);
std::sort(ys,ys+n);
blocks.clear();
for(int i=0;i<n;i++){
if(i%BLK_SIZE==0){
blocks.emplace_back();
}
blocks.back().pos.push_back(ys[i]);
}
for(int i=0;i<blocks.size();i++){
blocks[i].recalc();
}
}
void init(int N, int L, int X[])
{
n=N;
l=L;
for(int i=0;i<N;i++){
xs[i]=X[i];
}
rebuild();
}
int current_query=0;
int update(int index, int new_val)
{
int old_val=xs[index];
xs[index]=new_val;
//erase old_val
for(int i=0;i<blocks.size();i++){
if(blocks[i].pos.back()>=old_val){
blocks[i].pos.erase(std::lower_bound(blocks[i].pos.begin(),blocks[i].pos.end(),old_val));
if(!blocks[i].pos.empty()){
blocks[i].recalc();
}else{
blocks.erase(blocks.begin()+i);
}
break;
}
}
//insert new_val
for(int i=0;i<blocks.size();i++){
if(i==blocks.size()-1||blocks[i+1].pos.front()>new_val){
blocks[i].pos.insert(std::lower_bound(blocks[i].pos.begin(),blocks[i].pos.end(),new_val),new_val);
blocks[i].recalc();
break;
}
}
if(++current_query%PERIOD==0){
rebuild();
}
//rebuild();
int pos=-INF,cnt=0;
for(int i=0;i<blocks.size();i++){
blocks[i].apply(pos,cnt);
}
return cnt;
}
Compilation message
elephants.cpp: In function 'void rebuild()':
elephants.cpp:60:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<blocks.size();i++){
~^~~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:83:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<blocks.size();i++){
~^~~~~~~~~~~~~~
elephants.cpp:95:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<blocks.size();i++){
~^~~~~~~~~~~~~~
elephants.cpp:96:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i==blocks.size()-1||blocks[i+1].pos.front()>new_val){
~^~~~~~~~~~~~~~~~~
elephants.cpp:107:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<blocks.size();i++){
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
183 ms |
1292 KB |
Output is correct |
8 |
Correct |
213 ms |
1464 KB |
Output is correct |
9 |
Correct |
366 ms |
2360 KB |
Output is correct |
10 |
Correct |
505 ms |
2420 KB |
Output is correct |
11 |
Correct |
716 ms |
2548 KB |
Output is correct |
12 |
Correct |
844 ms |
2736 KB |
Output is correct |
13 |
Correct |
538 ms |
2408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
183 ms |
1292 KB |
Output is correct |
8 |
Correct |
213 ms |
1464 KB |
Output is correct |
9 |
Correct |
366 ms |
2360 KB |
Output is correct |
10 |
Correct |
505 ms |
2420 KB |
Output is correct |
11 |
Correct |
716 ms |
2548 KB |
Output is correct |
12 |
Correct |
844 ms |
2736 KB |
Output is correct |
13 |
Correct |
538 ms |
2408 KB |
Output is correct |
14 |
Correct |
340 ms |
1920 KB |
Output is correct |
15 |
Correct |
485 ms |
1916 KB |
Output is correct |
16 |
Correct |
1352 ms |
2960 KB |
Output is correct |
17 |
Correct |
1579 ms |
5596 KB |
Output is correct |
18 |
Correct |
1669 ms |
5536 KB |
Output is correct |
19 |
Correct |
974 ms |
5332 KB |
Output is correct |
20 |
Correct |
1554 ms |
5436 KB |
Output is correct |
21 |
Correct |
1573 ms |
5444 KB |
Output is correct |
22 |
Correct |
968 ms |
4760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
183 ms |
1292 KB |
Output is correct |
8 |
Correct |
213 ms |
1464 KB |
Output is correct |
9 |
Correct |
366 ms |
2360 KB |
Output is correct |
10 |
Correct |
505 ms |
2420 KB |
Output is correct |
11 |
Correct |
716 ms |
2548 KB |
Output is correct |
12 |
Correct |
844 ms |
2736 KB |
Output is correct |
13 |
Correct |
538 ms |
2408 KB |
Output is correct |
14 |
Correct |
340 ms |
1920 KB |
Output is correct |
15 |
Correct |
485 ms |
1916 KB |
Output is correct |
16 |
Correct |
1352 ms |
2960 KB |
Output is correct |
17 |
Correct |
1579 ms |
5596 KB |
Output is correct |
18 |
Correct |
1669 ms |
5536 KB |
Output is correct |
19 |
Correct |
974 ms |
5332 KB |
Output is correct |
20 |
Correct |
1554 ms |
5436 KB |
Output is correct |
21 |
Correct |
1573 ms |
5444 KB |
Output is correct |
22 |
Correct |
968 ms |
4760 KB |
Output is correct |
23 |
Correct |
3583 ms |
11240 KB |
Output is correct |
24 |
Correct |
4050 ms |
11160 KB |
Output is correct |
25 |
Correct |
3117 ms |
11220 KB |
Output is correct |
26 |
Correct |
4198 ms |
11188 KB |
Output is correct |
27 |
Correct |
4787 ms |
11120 KB |
Output is correct |
28 |
Correct |
1221 ms |
5112 KB |
Output is correct |
29 |
Correct |
1156 ms |
5240 KB |
Output is correct |
30 |
Correct |
1215 ms |
5208 KB |
Output is correct |
31 |
Correct |
1138 ms |
5328 KB |
Output is correct |
32 |
Correct |
4582 ms |
10748 KB |
Output is correct |
33 |
Correct |
4786 ms |
9984 KB |
Output is correct |
34 |
Correct |
4501 ms |
10928 KB |
Output is correct |
35 |
Correct |
3907 ms |
11160 KB |
Output is correct |
36 |
Correct |
3361 ms |
10692 KB |
Output is correct |
37 |
Correct |
6612 ms |
11372 KB |
Output is correct |
38 |
Correct |
4822 ms |
9928 KB |
Output is correct |
39 |
Correct |
4372 ms |
10900 KB |
Output is correct |
40 |
Correct |
4541 ms |
9872 KB |
Output is correct |
41 |
Correct |
6569 ms |
11256 KB |
Output is correct |
42 |
Correct |
6657 ms |
11476 KB |
Output is correct |