#include <bits/stdc++.h>
using namespace std;
#include "elephants.h"
const int MAX_N = 150'000, B = 500, INF = 2e9;
int N, L, LIM, X[MAX_N], Y[MAX_N], num[MAX_N], pos[MAX_N], q_;
vector<int> a[B];
void recalculate(int j) {
auto &c = a[j];
int sz = size(c), pt = sz;
for(int i = sz; --i >= 0; ) {
while(pt && X[c[i]] + L < X[c[pt-1]]) --pt;
num[c[i]] = pt < sz ? num[c[pt]] + 1 : 1;
pos[c[i]] = pt < sz ? pos[c[pt]] : X[c[i]] + L;
}
}
void reset() {
for(int i = 0, j = 0; i < LIM; i++) {
for(int &v : a[i]) pos[j++] = v;
a[i].clear();
}
for(int i = 0; i < N; i++) {
Y[pos[i]] = i/B;
a[i/B].push_back(pos[i]);
}
for(int i = 0; i < LIM; i++)
recalculate(i);
}
void init(int n, int l, int x[]) {
N = n, L = l, LIM = (N - 1)/B + 1;
for(int i = 0; i < N; i++) {
X[i] = x[i];
a[i/B].push_back(i);
}
reset();
}
int lowerB(int val, int j){
int r = -1;
for(int k=512; k/=2; )
while(r + k < (int)size(a[j]) && X[a[j][r+k]] < val) r += k;
return r + 1;
}
int update(int i, int y) {
int l = Y[i], p = 0;
while(a[l][p] != i) ++p;
a[l].erase(begin(a[l]) + p);
recalculate(l);
X[i] = y;
for(l = 0; l < LIM; l++)
if(!empty(a[l]) && X[a[l][0]] > X[i]) break;
if(l) --l;
p = lowerB(y, l);
Y[i] = l;
a[l].insert(begin(a[l]) + p, i);
recalculate(l);
if(!(++q_ % (B - 2))) reset();
int res = 0, curr = -1;
for(int k = 0; k < LIM; k++) {
if(empty(a[k]) || curr >= X[a[k].back()]) continue;
int j = lowerB(curr + 1, k);
res += num[a[k][j]];
curr = pos[a[k][j]];
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
595 ms |
1220 KB |
Output is correct |
8 |
Correct |
635 ms |
1340 KB |
Output is correct |
9 |
Correct |
576 ms |
2124 KB |
Output is correct |
10 |
Correct |
675 ms |
2124 KB |
Output is correct |
11 |
Correct |
582 ms |
2124 KB |
Output is correct |
12 |
Correct |
902 ms |
2260 KB |
Output is correct |
13 |
Correct |
864 ms |
2124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
595 ms |
1220 KB |
Output is correct |
8 |
Correct |
635 ms |
1340 KB |
Output is correct |
9 |
Correct |
576 ms |
2124 KB |
Output is correct |
10 |
Correct |
675 ms |
2124 KB |
Output is correct |
11 |
Correct |
582 ms |
2124 KB |
Output is correct |
12 |
Correct |
902 ms |
2260 KB |
Output is correct |
13 |
Correct |
864 ms |
2124 KB |
Output is correct |
14 |
Correct |
649 ms |
1716 KB |
Output is correct |
15 |
Correct |
940 ms |
1796 KB |
Output is correct |
16 |
Correct |
1496 ms |
2472 KB |
Output is correct |
17 |
Correct |
1458 ms |
2872 KB |
Output is correct |
18 |
Correct |
1679 ms |
2892 KB |
Output is correct |
19 |
Correct |
1091 ms |
2828 KB |
Output is correct |
20 |
Correct |
1548 ms |
2960 KB |
Output is correct |
21 |
Correct |
1547 ms |
2904 KB |
Output is correct |
22 |
Correct |
1405 ms |
2824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
595 ms |
1220 KB |
Output is correct |
8 |
Correct |
635 ms |
1340 KB |
Output is correct |
9 |
Correct |
576 ms |
2124 KB |
Output is correct |
10 |
Correct |
675 ms |
2124 KB |
Output is correct |
11 |
Correct |
582 ms |
2124 KB |
Output is correct |
12 |
Correct |
902 ms |
2260 KB |
Output is correct |
13 |
Correct |
864 ms |
2124 KB |
Output is correct |
14 |
Correct |
649 ms |
1716 KB |
Output is correct |
15 |
Correct |
940 ms |
1796 KB |
Output is correct |
16 |
Correct |
1496 ms |
2472 KB |
Output is correct |
17 |
Correct |
1458 ms |
2872 KB |
Output is correct |
18 |
Correct |
1679 ms |
2892 KB |
Output is correct |
19 |
Correct |
1091 ms |
2828 KB |
Output is correct |
20 |
Correct |
1548 ms |
2960 KB |
Output is correct |
21 |
Correct |
1547 ms |
2904 KB |
Output is correct |
22 |
Correct |
1405 ms |
2824 KB |
Output is correct |
23 |
Correct |
3711 ms |
5628 KB |
Output is correct |
24 |
Correct |
3776 ms |
5648 KB |
Output is correct |
25 |
Correct |
3186 ms |
5728 KB |
Output is correct |
26 |
Correct |
3757 ms |
5632 KB |
Output is correct |
27 |
Correct |
3869 ms |
5636 KB |
Output is correct |
28 |
Correct |
2039 ms |
2228 KB |
Output is correct |
29 |
Correct |
2029 ms |
2224 KB |
Output is correct |
30 |
Correct |
2086 ms |
2228 KB |
Output is correct |
31 |
Correct |
2055 ms |
2236 KB |
Output is correct |
32 |
Correct |
3063 ms |
5648 KB |
Output is correct |
33 |
Correct |
2323 ms |
5632 KB |
Output is correct |
34 |
Correct |
4111 ms |
5636 KB |
Output is correct |
35 |
Correct |
2319 ms |
5636 KB |
Output is correct |
36 |
Correct |
1988 ms |
5632 KB |
Output is correct |
37 |
Correct |
4482 ms |
6492 KB |
Output is correct |
38 |
Correct |
5760 ms |
5632 KB |
Output is correct |
39 |
Correct |
4137 ms |
5628 KB |
Output is correct |
40 |
Correct |
7216 ms |
5632 KB |
Output is correct |
41 |
Correct |
5023 ms |
5672 KB |
Output is correct |
42 |
Correct |
5372 ms |
5632 KB |
Output is correct |