#include "elephants.h"
#include <vector>
#include <algorithm>
#include <cstdio>
const int INF=1e9+7;
const int BLK_SIZE=400;
const int PERIOD=700;
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 |
3 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 |
3 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 |
168 ms |
1404 KB |
Output is correct |
8 |
Correct |
189 ms |
1492 KB |
Output is correct |
9 |
Correct |
325 ms |
2280 KB |
Output is correct |
10 |
Correct |
389 ms |
2492 KB |
Output is correct |
11 |
Correct |
398 ms |
2452 KB |
Output is correct |
12 |
Correct |
603 ms |
2712 KB |
Output is correct |
13 |
Correct |
387 ms |
2404 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 |
3 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 |
168 ms |
1404 KB |
Output is correct |
8 |
Correct |
189 ms |
1492 KB |
Output is correct |
9 |
Correct |
325 ms |
2280 KB |
Output is correct |
10 |
Correct |
389 ms |
2492 KB |
Output is correct |
11 |
Correct |
398 ms |
2452 KB |
Output is correct |
12 |
Correct |
603 ms |
2712 KB |
Output is correct |
13 |
Correct |
387 ms |
2404 KB |
Output is correct |
14 |
Correct |
268 ms |
1912 KB |
Output is correct |
15 |
Correct |
326 ms |
1988 KB |
Output is correct |
16 |
Correct |
1068 ms |
3028 KB |
Output is correct |
17 |
Correct |
1202 ms |
3676 KB |
Output is correct |
18 |
Correct |
1268 ms |
3540 KB |
Output is correct |
19 |
Correct |
720 ms |
3340 KB |
Output is correct |
20 |
Correct |
1164 ms |
3648 KB |
Output is correct |
21 |
Correct |
1280 ms |
3580 KB |
Output is correct |
22 |
Correct |
759 ms |
3408 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 |
3 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 |
168 ms |
1404 KB |
Output is correct |
8 |
Correct |
189 ms |
1492 KB |
Output is correct |
9 |
Correct |
325 ms |
2280 KB |
Output is correct |
10 |
Correct |
389 ms |
2492 KB |
Output is correct |
11 |
Correct |
398 ms |
2452 KB |
Output is correct |
12 |
Correct |
603 ms |
2712 KB |
Output is correct |
13 |
Correct |
387 ms |
2404 KB |
Output is correct |
14 |
Correct |
268 ms |
1912 KB |
Output is correct |
15 |
Correct |
326 ms |
1988 KB |
Output is correct |
16 |
Correct |
1068 ms |
3028 KB |
Output is correct |
17 |
Correct |
1202 ms |
3676 KB |
Output is correct |
18 |
Correct |
1268 ms |
3540 KB |
Output is correct |
19 |
Correct |
720 ms |
3340 KB |
Output is correct |
20 |
Correct |
1164 ms |
3648 KB |
Output is correct |
21 |
Correct |
1280 ms |
3580 KB |
Output is correct |
22 |
Correct |
759 ms |
3408 KB |
Output is correct |
23 |
Correct |
2835 ms |
6208 KB |
Output is correct |
24 |
Correct |
3092 ms |
6232 KB |
Output is correct |
25 |
Correct |
2477 ms |
6072 KB |
Output is correct |
26 |
Correct |
3291 ms |
6316 KB |
Output is correct |
27 |
Correct |
3526 ms |
6216 KB |
Output is correct |
28 |
Correct |
1169 ms |
2420 KB |
Output is correct |
29 |
Correct |
1094 ms |
2304 KB |
Output is correct |
30 |
Correct |
1137 ms |
2308 KB |
Output is correct |
31 |
Correct |
1105 ms |
2336 KB |
Output is correct |
32 |
Correct |
3478 ms |
6192 KB |
Output is correct |
33 |
Correct |
3351 ms |
6272 KB |
Output is correct |
34 |
Correct |
3288 ms |
6244 KB |
Output is correct |
35 |
Correct |
2912 ms |
6396 KB |
Output is correct |
36 |
Correct |
2549 ms |
6200 KB |
Output is correct |
37 |
Correct |
4340 ms |
6620 KB |
Output is correct |
38 |
Correct |
3518 ms |
6216 KB |
Output is correct |
39 |
Correct |
3249 ms |
6180 KB |
Output is correct |
40 |
Correct |
3767 ms |
6332 KB |
Output is correct |
41 |
Correct |
4726 ms |
6872 KB |
Output is correct |
42 |
Correct |
4974 ms |
6996 KB |
Output is correct |