#include <bits/stdc++.h>
#ifndef LOCAL
#include "elephants.h"
#endif // LOCAL
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
using namespace std;
const int MAX = 150000;
int magic = sqrt(MAX) + 10;
const int maxMagic = 500;
int N, L, x[MAX], tmp[MAX], nBlocks, countUpdates = 0;
vector<int> blocks[maxMagic];
pair<int, int> id[MAX];
int original[MAX];
vector<pair<int, int>> infoBlocks[MAX]; //[n_jumps, last_jump]
void buildBlock(int ib){
infoBlocks[ib].resize(sz(blocks[ib]));
for(int i = sz(blocks[ib]) - 1; i >= 0; --i){
if(i == sz(blocks[ib]) - 1){
infoBlocks[ib][i] = {1, blocks[ib][i] + L};
} else{
int l = i + 1, r = sz(blocks[ib]) - 1;
int n_jumps = 1, last_jump = blocks[ib][i] + L;
while(l <= r){
int mid = l + r >> 1;
if(blocks[ib][i] + L < blocks[ib][mid]){
n_jumps = infoBlocks[ib][mid].first + 1;
last_jump = infoBlocks[ib][mid].second;
r = mid - 1;
} else l = mid + 1;
}
infoBlocks[ib][i] = {n_jumps, last_jump};
}
}
}
int simulate(){
int total = 0, cur = -1;
for(int i = 0; i < nBlocks; ++i){
if(blocks[i].empty() || blocks[i].back() <= cur) {
continue; //over
}
int pos = upper_bound(all(blocks[i]), cur) - blocks[i].begin();
total += infoBlocks[i][pos].first;
cur = infoBlocks[i][pos].second;
}
return total;
}
void init(int _N, int _L, int _X[]){
N = _N;
L = _L;
for(int i = 0; i < N; ++i) x[i] = _X[i];
//tested for magic exactly sqrt(n)
magic = 1280;
nBlocks = 0;
iota(original, original + N, 0);
for(int i = 0; i < N; i += magic, ++nBlocks){
int j = min(i + magic, N) - 1;
blocks[nBlocks] = vector<int>(x + i, x + j + 1);
buildBlock(nBlocks);
}
}
void debugBlock(int i){
cout << "block : " << i << '\n';
for(int x : blocks[i]) cout << x << ' ';
cout << '\n';
}
void debugAllBlock(){
for(int i = 0; i < nBlocks; ++i){
debugBlock(i);
}
}
void rebuild(){
copy(x, x + N, tmp);
sort(tmp, tmp + N);
nBlocks = 0;
for(int i = 0; i < N; i += magic, ++nBlocks){
int j = min(i + magic, N) - 1;
blocks[nBlocks] = vector<int>(tmp + i, tmp + j + 1);
buildBlock(nBlocks);
}
}
void remove(int x){
for(int i = 0; i < nBlocks; ++i){
if(blocks[i].empty()) continue;
if(!binary_search(all(blocks[i]), x)) continue;
blocks[i].erase(find(all(blocks[i]), x));
buildBlock(i);
return;
}
assert(false);
}
void insert(int x){
for(int i = 0; i < nBlocks; ++i){
if(blocks[i].empty()) continue;
if(blocks[i].back() > x){
int pos = upper_bound(all(blocks[i]), x) - blocks[i].begin();
blocks[i].insert(blocks[i].begin() + pos, x);
buildBlock(i);
return;
}
}
blocks[nBlocks - 1].push_back(x);
buildBlock(nBlocks - 1);
}
int update(int i, int y){
++countUpdates;
if(countUpdates == 2 * magic){
x[i] = y;
rebuild();
countUpdates = 0;
}
remove(x[i]);
x[i] = y;
insert(x[i]);
return simulate();
}
#ifdef LOCAL
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
int N, L;
cin >> N >> L;
int X[N];
for(int i = 0; i < N; ++i) cin >> X[i];
init(N, L, X);
int Q; cin >> Q;
while(Q--){
int i, y;
cin >> i >> y;
cout << update(i, y) << '\n';
}
return 0;
}
#endif // LOCAL
Compilation message
elephants.cpp: In function 'void buildBlock(int)':
elephants.cpp:32:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
15156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
15156 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14928 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
15156 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14928 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
1799 ms |
17224 KB |
Output is correct |
8 |
Correct |
1832 ms |
17444 KB |
Output is correct |
9 |
Correct |
1176 ms |
17764 KB |
Output is correct |
10 |
Correct |
928 ms |
17660 KB |
Output is correct |
11 |
Correct |
904 ms |
17488 KB |
Output is correct |
12 |
Correct |
1774 ms |
18104 KB |
Output is correct |
13 |
Correct |
955 ms |
17636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
15156 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14928 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
1799 ms |
17224 KB |
Output is correct |
8 |
Correct |
1832 ms |
17444 KB |
Output is correct |
9 |
Correct |
1176 ms |
17764 KB |
Output is correct |
10 |
Correct |
928 ms |
17660 KB |
Output is correct |
11 |
Correct |
904 ms |
17488 KB |
Output is correct |
12 |
Correct |
1774 ms |
18104 KB |
Output is correct |
13 |
Correct |
955 ms |
17636 KB |
Output is correct |
14 |
Correct |
1803 ms |
19444 KB |
Output is correct |
15 |
Correct |
2149 ms |
19528 KB |
Output is correct |
16 |
Correct |
2851 ms |
20168 KB |
Output is correct |
17 |
Correct |
2855 ms |
20552 KB |
Output is correct |
18 |
Correct |
2920 ms |
20648 KB |
Output is correct |
19 |
Correct |
2234 ms |
19984 KB |
Output is correct |
20 |
Correct |
2770 ms |
20636 KB |
Output is correct |
21 |
Correct |
2602 ms |
20604 KB |
Output is correct |
22 |
Correct |
1980 ms |
19920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
3 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
15156 KB |
Output is correct |
4 |
Correct |
3 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14928 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
1799 ms |
17224 KB |
Output is correct |
8 |
Correct |
1832 ms |
17444 KB |
Output is correct |
9 |
Correct |
1176 ms |
17764 KB |
Output is correct |
10 |
Correct |
928 ms |
17660 KB |
Output is correct |
11 |
Correct |
904 ms |
17488 KB |
Output is correct |
12 |
Correct |
1774 ms |
18104 KB |
Output is correct |
13 |
Correct |
955 ms |
17636 KB |
Output is correct |
14 |
Correct |
1803 ms |
19444 KB |
Output is correct |
15 |
Correct |
2149 ms |
19528 KB |
Output is correct |
16 |
Correct |
2851 ms |
20168 KB |
Output is correct |
17 |
Correct |
2855 ms |
20552 KB |
Output is correct |
18 |
Correct |
2920 ms |
20648 KB |
Output is correct |
19 |
Correct |
2234 ms |
19984 KB |
Output is correct |
20 |
Correct |
2770 ms |
20636 KB |
Output is correct |
21 |
Correct |
2602 ms |
20604 KB |
Output is correct |
22 |
Correct |
1980 ms |
19920 KB |
Output is correct |
23 |
Correct |
4757 ms |
23536 KB |
Output is correct |
24 |
Correct |
5304 ms |
23588 KB |
Output is correct |
25 |
Correct |
3881 ms |
23116 KB |
Output is correct |
26 |
Correct |
4066 ms |
22924 KB |
Output is correct |
27 |
Correct |
6603 ms |
22968 KB |
Output is correct |
28 |
Correct |
4289 ms |
19200 KB |
Output is correct |
29 |
Correct |
4705 ms |
19192 KB |
Output is correct |
30 |
Correct |
4280 ms |
19204 KB |
Output is correct |
31 |
Correct |
4684 ms |
19188 KB |
Output is correct |
32 |
Correct |
3350 ms |
22960 KB |
Output is correct |
33 |
Correct |
3099 ms |
22964 KB |
Output is correct |
34 |
Correct |
3386 ms |
23196 KB |
Output is correct |
35 |
Correct |
2702 ms |
22964 KB |
Output is correct |
36 |
Correct |
2278 ms |
22856 KB |
Output is correct |
37 |
Correct |
5340 ms |
24660 KB |
Output is correct |
38 |
Correct |
3501 ms |
22964 KB |
Output is correct |
39 |
Correct |
5154 ms |
22976 KB |
Output is correct |
40 |
Correct |
3382 ms |
22964 KB |
Output is correct |
41 |
Correct |
6556 ms |
24396 KB |
Output is correct |
42 |
Correct |
6720 ms |
24336 KB |
Output is correct |