#include "elephants.h"
#include <algorithm>
using namespace std;
const int t = 2000;
const int MAXN = 2e5;
int n, l, a[MAXN];
int x[MAXN];
struct _dp {
int cnt, mx;
_dp operator+(int x) const {
return { cnt + x, mx };
}
} dp[t][t << 1 | 1];
int buc[t][t << 1 | 1];
int s[t];
int idx[MAXN];
const int inf = 1e9;
void recalc(int s) {
int j = buc[s][0];
for (int i = j++; i > 0; --i) {
while (j > 1 && buc[s][i] + l < buc[s][j - 1]) --j;
if (j <= buc[s][0]) dp[s][i] = dp[s][j] + 1;
else dp[s][i] = { 1, buc[s][i] };
}
}
void reset() {
for (int i = 0; t * i < n; ++i) {
buc[i][0] = 0;
for (int j = 0; j < t && i + j < n; ++j) {
buc[i][++buc[i][0]] = x[t * i + j];
idx[t * i + j] = i;
}
recalc(i);
s[i] = buc[i][1];
}
}
void getX() {
int p = 0;
for (int i = 0; i < t; ++i) {
for (int j = 1; j <= buc[t][0]; ++j) {
x[p++] = buc[i][j];
}
}
}
void init(int N, int L, int X[]) {
n = N;
l = L;
for (int i = 0; i < n; ++i) a[i] = x[i] = X[i];
sort(x, x + n);
reset();
}
int getBuc(int i) {
return lower_bound(s + 1, s + (n + t - 1) / t, i + 1) - s - 1;
}
int update(int p, int y) {
int s = getBuc(a[p]);
int si = lower_bound(buc[s] + 1, buc[s] + (buc[s][0] + 1), a[p]) - buc[s];
for (int i = si; i < buc[s][0]; ++i) {
buc[s][i] = buc[s][i + 1];
}
--buc[s][0];
recalc(s);
s = getBuc(y);
if (buc[s][0] == (t << 1)) getX(), reset(), s = getBuc(y);
si = lower_bound(buc[s] + 1, buc[s] + (buc[s][0] + 1), y) - buc[s];
for (int i = ++buc[s][0]; i > si; --i) {
buc[s][i] = buc[s][i - 1];
}
buc[s][si] = y;
recalc(s);
a[p] = y;
int ret = 0;
p = -inf;
for (int i = 0; t * i < n; ++i) {
int j = lower_bound(buc[i] + 1, buc[i] + (buc[i][0] + 1), p + l + 1) - buc[i];
if (j <= buc[i][0]) {
ret += dp[i][j].cnt;
p = dp[i][j].mx;
}
}
return ret;
}
Compilation message
elephants.cpp: In function 'void getX()':
elephants.cpp:47:35: warning: array subscript is above array bounds [-Warray-bounds]
for (int j = 1; j <= buc[t][0]; ++j) {
~~~~~^
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:47:35: warning: array subscript is above array bounds [-Warray-bounds]
for (int j = 1; j <= buc[t][0]; ++j) {
~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
664 KB |
Output is correct |
6 |
Correct |
2 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
664 KB |
Output is correct |
6 |
Correct |
2 ms |
664 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1540 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
664 KB |
Output is correct |
6 |
Correct |
2 ms |
664 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1540 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
560 KB |
Output is correct |
4 |
Correct |
3 ms |
560 KB |
Output is correct |
5 |
Correct |
3 ms |
664 KB |
Output is correct |
6 |
Correct |
2 ms |
664 KB |
Output is correct |
7 |
Incorrect |
20 ms |
1540 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |