#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 = sqrt(N);
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 |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
14976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
14976 KB |
Output is correct |
4 |
Correct |
2 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14992 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 |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
14976 KB |
Output is correct |
4 |
Correct |
2 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14992 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
220 ms |
17480 KB |
Output is correct |
8 |
Correct |
268 ms |
17736 KB |
Output is correct |
9 |
Correct |
380 ms |
17740 KB |
Output is correct |
10 |
Correct |
413 ms |
18768 KB |
Output is correct |
11 |
Correct |
377 ms |
18768 KB |
Output is correct |
12 |
Correct |
699 ms |
19528 KB |
Output is correct |
13 |
Correct |
452 ms |
18768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
14976 KB |
Output is correct |
4 |
Correct |
2 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14992 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
220 ms |
17480 KB |
Output is correct |
8 |
Correct |
268 ms |
17736 KB |
Output is correct |
9 |
Correct |
380 ms |
17740 KB |
Output is correct |
10 |
Correct |
413 ms |
18768 KB |
Output is correct |
11 |
Correct |
377 ms |
18768 KB |
Output is correct |
12 |
Correct |
699 ms |
19528 KB |
Output is correct |
13 |
Correct |
452 ms |
18768 KB |
Output is correct |
14 |
Correct |
331 ms |
21064 KB |
Output is correct |
15 |
Correct |
414 ms |
21064 KB |
Output is correct |
16 |
Correct |
1049 ms |
22096 KB |
Output is correct |
17 |
Correct |
1277 ms |
22784 KB |
Output is correct |
18 |
Correct |
1271 ms |
22716 KB |
Output is correct |
19 |
Correct |
918 ms |
22096 KB |
Output is correct |
20 |
Correct |
1243 ms |
22788 KB |
Output is correct |
21 |
Correct |
1177 ms |
22708 KB |
Output is correct |
22 |
Correct |
727 ms |
21320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14928 KB |
Output is correct |
2 |
Correct |
2 ms |
14928 KB |
Output is correct |
3 |
Correct |
2 ms |
14976 KB |
Output is correct |
4 |
Correct |
2 ms |
14928 KB |
Output is correct |
5 |
Correct |
2 ms |
14992 KB |
Output is correct |
6 |
Correct |
2 ms |
14928 KB |
Output is correct |
7 |
Correct |
220 ms |
17480 KB |
Output is correct |
8 |
Correct |
268 ms |
17736 KB |
Output is correct |
9 |
Correct |
380 ms |
17740 KB |
Output is correct |
10 |
Correct |
413 ms |
18768 KB |
Output is correct |
11 |
Correct |
377 ms |
18768 KB |
Output is correct |
12 |
Correct |
699 ms |
19528 KB |
Output is correct |
13 |
Correct |
452 ms |
18768 KB |
Output is correct |
14 |
Correct |
331 ms |
21064 KB |
Output is correct |
15 |
Correct |
414 ms |
21064 KB |
Output is correct |
16 |
Correct |
1049 ms |
22096 KB |
Output is correct |
17 |
Correct |
1277 ms |
22784 KB |
Output is correct |
18 |
Correct |
1271 ms |
22716 KB |
Output is correct |
19 |
Correct |
918 ms |
22096 KB |
Output is correct |
20 |
Correct |
1243 ms |
22788 KB |
Output is correct |
21 |
Correct |
1177 ms |
22708 KB |
Output is correct |
22 |
Correct |
727 ms |
21320 KB |
Output is correct |
23 |
Correct |
2774 ms |
28772 KB |
Output is correct |
24 |
Correct |
2963 ms |
28724 KB |
Output is correct |
25 |
Correct |
2166 ms |
28396 KB |
Output is correct |
26 |
Correct |
2748 ms |
27956 KB |
Output is correct |
27 |
Correct |
3654 ms |
27804 KB |
Output is correct |
28 |
Correct |
592 ms |
22096 KB |
Output is correct |
29 |
Correct |
566 ms |
22088 KB |
Output is correct |
30 |
Correct |
603 ms |
22132 KB |
Output is correct |
31 |
Correct |
571 ms |
22096 KB |
Output is correct |
32 |
Correct |
2353 ms |
27388 KB |
Output is correct |
33 |
Correct |
2217 ms |
26720 KB |
Output is correct |
34 |
Correct |
2494 ms |
27392 KB |
Output is correct |
35 |
Correct |
1891 ms |
27900 KB |
Output is correct |
36 |
Correct |
1238 ms |
27372 KB |
Output is correct |
37 |
Correct |
3777 ms |
29356 KB |
Output is correct |
38 |
Correct |
2630 ms |
26600 KB |
Output is correct |
39 |
Correct |
3185 ms |
27632 KB |
Output is correct |
40 |
Correct |
2466 ms |
26628 KB |
Output is correct |
41 |
Correct |
4244 ms |
29132 KB |
Output is correct |
42 |
Correct |
4377 ms |
29372 KB |
Output is correct |