#include <bits/stdc++.h>
#include "elephants.h"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
int n, L;
const int K = 400;
int block[K][2 * K], nxt[K][2 * K], val[K][2 * K], sz[K], b[(int) 2e5+5], a[(int) 2e5+5];
pair<int, int> go[K][2 * K];
void make(int k) {
int r = 0;
for (int i = 0; i < sz[k]; i++) {
while (r < sz[k] && block[k][r] - block[k][i] <= L) r++;
nxt[k][i] = r;
val[k][i] = block[k][i] + L;
}
for (int i = sz[k] - 1; i >= 0; i--) {
if (nxt[k][i] == sz[k]) go[k][i] = {1, val[k][i]};
else go[k][i] = {1 + go[k][nxt[k][i]].first, go[k][nxt[k][i]].second};
}
}
void build() {
n = 0;
for (int i = 0; i < K; i++)
for (int j = 0; j < sz[i]; j++) b[n++] = block[i][j];
for (int i = 0; i < n; i += K) {
int r = min(n, i + K);
sz[i / K] = r - i;
for (int j = i; j < r; j++) block[i / K][j - i] = b[j];
make(i / K);
}
}
void init(int N, int _L, int X[]) {
n = N, L = _L;
for (int i = 0; i < n; i++) b[i] = a[i] = X[i];
for (int i = 0; i < n; i += K) {
int r = min(n, i + K);
sz[i / K] = r - i;
for (int j = i; j < r; j++) block[i / K][j - i] = b[j];
}
build();
}
void erase(int i, int x) {
for (int j = 0; j < sz[i]; j++) {
if (block[i][j] < x) continue;
block[i][j] = block[i][j + 1];
}
sz[i]--;
make(i);
}
void del(int x) {
for (int i = 0; i < K; i++) {
if (x <= block[i][sz[i] - 1]) {
erase(i, x);
break;
}
}
}
void add(int i, int x) {
int dummy = x;
for (int j = 0; j < sz[i]; j++) {
if (block[i][j] < x) continue;
swap(block[i][j], dummy);
}
block[i][sz[i]++] = dummy;
make(i);
}
void Update(int x) {
for (int i = 0; i < K; i++) {
if (x <= block[i][sz[i] - 1]) {
add(i, x);
break;
}
if (i == (n - 1) / K) {
add(i, x);
break;
}
}
}
int q = 0;
int update(int x, int y) {
del(a[x]);
a[x] = y;
Update(a[x]);
q++;
if (q == K) {
build();
q = 0;
}
int ans = 0, pos = block[0][0], i = 0, j = 0;
int cnt = 0;
while (sz[i]) {
ans += go[i][j].first;
pos = go[i][j].second;
while (sz[i] && block[i][sz[i] - 1] <= pos) i++;
if (!sz[i]) break;
int l = 0, r = sz[i] - 1;
while (l < r) {
int m = (l + r) / 2;
if (block[i][m] <= pos) l = m + 1;
else r = m;
}
j = r;
}
return ans;
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:106:9: warning: unused variable 'cnt' [-Wunused-variable]
106 | int cnt = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
295 ms |
1572 KB |
Output is correct |
8 |
Correct |
307 ms |
1748 KB |
Output is correct |
9 |
Correct |
306 ms |
3468 KB |
Output is correct |
10 |
Correct |
285 ms |
3484 KB |
Output is correct |
11 |
Correct |
252 ms |
3480 KB |
Output is correct |
12 |
Correct |
504 ms |
3476 KB |
Output is correct |
13 |
Correct |
255 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
295 ms |
1572 KB |
Output is correct |
8 |
Correct |
307 ms |
1748 KB |
Output is correct |
9 |
Correct |
306 ms |
3468 KB |
Output is correct |
10 |
Correct |
285 ms |
3484 KB |
Output is correct |
11 |
Correct |
252 ms |
3480 KB |
Output is correct |
12 |
Correct |
504 ms |
3476 KB |
Output is correct |
13 |
Correct |
255 ms |
3412 KB |
Output is correct |
14 |
Correct |
289 ms |
2052 KB |
Output is correct |
15 |
Correct |
474 ms |
2356 KB |
Output is correct |
16 |
Correct |
804 ms |
3700 KB |
Output is correct |
17 |
Correct |
863 ms |
4784 KB |
Output is correct |
18 |
Correct |
868 ms |
4724 KB |
Output is correct |
19 |
Correct |
433 ms |
4708 KB |
Output is correct |
20 |
Correct |
849 ms |
4840 KB |
Output is correct |
21 |
Correct |
793 ms |
4812 KB |
Output is correct |
22 |
Correct |
429 ms |
4728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
295 ms |
1572 KB |
Output is correct |
8 |
Correct |
307 ms |
1748 KB |
Output is correct |
9 |
Correct |
306 ms |
3468 KB |
Output is correct |
10 |
Correct |
285 ms |
3484 KB |
Output is correct |
11 |
Correct |
252 ms |
3480 KB |
Output is correct |
12 |
Correct |
504 ms |
3476 KB |
Output is correct |
13 |
Correct |
255 ms |
3412 KB |
Output is correct |
14 |
Correct |
289 ms |
2052 KB |
Output is correct |
15 |
Correct |
474 ms |
2356 KB |
Output is correct |
16 |
Correct |
804 ms |
3700 KB |
Output is correct |
17 |
Correct |
863 ms |
4784 KB |
Output is correct |
18 |
Correct |
868 ms |
4724 KB |
Output is correct |
19 |
Correct |
433 ms |
4708 KB |
Output is correct |
20 |
Correct |
849 ms |
4840 KB |
Output is correct |
21 |
Correct |
793 ms |
4812 KB |
Output is correct |
22 |
Correct |
429 ms |
4728 KB |
Output is correct |
23 |
Correct |
2179 ms |
9836 KB |
Output is correct |
24 |
Correct |
2318 ms |
14800 KB |
Output is correct |
25 |
Correct |
1549 ms |
14744 KB |
Output is correct |
26 |
Correct |
1974 ms |
14796 KB |
Output is correct |
27 |
Correct |
1838 ms |
14608 KB |
Output is correct |
28 |
Correct |
1096 ms |
5260 KB |
Output is correct |
29 |
Correct |
1055 ms |
5256 KB |
Output is correct |
30 |
Correct |
1099 ms |
5376 KB |
Output is correct |
31 |
Correct |
1044 ms |
5260 KB |
Output is correct |
32 |
Correct |
1291 ms |
14268 KB |
Output is correct |
33 |
Correct |
1316 ms |
13516 KB |
Output is correct |
34 |
Correct |
1601 ms |
14400 KB |
Output is correct |
35 |
Correct |
986 ms |
14704 KB |
Output is correct |
36 |
Correct |
375 ms |
14164 KB |
Output is correct |
37 |
Correct |
2197 ms |
14700 KB |
Output is correct |
38 |
Correct |
1458 ms |
13440 KB |
Output is correct |
39 |
Correct |
1486 ms |
14364 KB |
Output is correct |
40 |
Correct |
1437 ms |
13432 KB |
Output is correct |
41 |
Correct |
2694 ms |
14320 KB |
Output is correct |
42 |
Correct |
2795 ms |
14532 KB |
Output is correct |