#include <bits/stdc++.h>
#include "elephants.h"
using namespace std;
const int kN = 1e5 + 5e4;
const int bucketSize = 1500;
const int kBuckets = 100;
int n, len, a[kN], bucket[kN], indexInBucket[kN];
int updates, auxSize, auxIndices[kN];
class bucket_t {
private:
int N = 0, indices[2 * bucketSize];
int req[2 * bucketSize], maxReach[2 * bucketSize];
public:
bool isEmpty() {
return N == 0;
}
int getSize() {
return N;
}
int last() {
return a[indices[N - 1]];
}
void add(int index) {
indices[N++] = index;
}
void addAll() {
for (int i = 0; i < N; ++i) {
auxIndices[auxSize++] = indices[i];
}
N = 0;
}
int cameras(int index) {
return req[index];
}
int furthest(int index) {
return maxReach[index];
}
void build() {
int r = N - 1;
for (int l = N - 1; l >= 0; --l) {
while (a[indices[r]] - a[indices[l]] > len) {
r -= 1;
}
if (r == N - 1) {
req[l] = 1;
maxReach[l] = a[indices[l]] + len;
} else {
req[l] = req[r + 1] + 1;
maxReach[l] = maxReach[r + 1];
}
}
}
void rem(int pos) {
for (int i = pos; i < N - 1; ++i) {
indices[i] = indices[i + 1];
indexInBucket[indices[i]] = i;
}
N -= 1;
build();
}
void ins(int index) {
int ptr = N - 1;
while (ptr >= 0 && a[index] < a[indices[ptr]]) {
ptr -= 1;
}
for (int i = N; i > ptr + 1; --i) {
indices[i] = indices[i - 1];
indexInBucket[indices[i]] = i;
}
indices[ptr + 1] = index;
indexInBucket[index] = ptr + 1;
N += 1;
build();
}
int upperBound(int x) {
int l = -1, r = N;
while (r - l > 1) {
int mid = (l + r) / 2;
if (mid != -1 && (mid == N || x < a[indices[mid]])) {
r = mid;
} else {
l = mid;
}
}
return r;
}
} buckets[kBuckets];
void init(int N, int L, int X[]) {
n = N;
len = L;
for (int i = 0; i < n; ++i) {
a[i] = X[i];
bucket[i] = i / bucketSize;
indexInBucket[i] = i % bucketSize;
buckets[i / bucketSize].add(i);
}
for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
buckets[i].build();
}
}
void rebuild() {
auxSize = 0;
for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
buckets[i].addAll();
}
for (int i = 0; i < n; ++i) {
bucket[auxIndices[i]] = i / bucketSize;
indexInBucket[auxIndices[i]] = i % bucketSize;
buckets[i / bucketSize].add(auxIndices[i]);
}
for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
buckets[i].build();
}
}
int query() {
int res = 0;
int reach = -1;
for (int i = 0; i <= (n - 1) / bucketSize; ++i) {
if (!buckets[i].isEmpty() && reach < buckets[i].last()) {
int index = buckets[i].upperBound(reach);
res += buckets[i].cameras(index);
reach = buckets[i].furthest(index);
}
}
return res;
}
int update(int index, int x) {
buckets[bucket[index]].rem(indexInBucket[index]);
a[index] = x;
bucket[index] = -1;
for (int i = 0; i <= (n - 1) / bucketSize && bucket[index] == -1; ++i) {
if (!buckets[i].isEmpty() && x <= buckets[i].last()) {
bucket[index] = i;
}
}
if (bucket[index] == -1) {
bucket[index] = (n - 1) / bucketSize;
}
buckets[bucket[index]].ins(index);
updates += 1;
if (updates == bucketSize) {
rebuild();
updates = 0;
}
return query();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
893 ms |
1836 KB |
Output is correct |
8 |
Correct |
888 ms |
2048 KB |
Output is correct |
9 |
Correct |
843 ms |
3152 KB |
Output is correct |
10 |
Correct |
614 ms |
3176 KB |
Output is correct |
11 |
Correct |
598 ms |
3148 KB |
Output is correct |
12 |
Correct |
1516 ms |
3196 KB |
Output is correct |
13 |
Correct |
630 ms |
3196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
893 ms |
1836 KB |
Output is correct |
8 |
Correct |
888 ms |
2048 KB |
Output is correct |
9 |
Correct |
843 ms |
3152 KB |
Output is correct |
10 |
Correct |
614 ms |
3176 KB |
Output is correct |
11 |
Correct |
598 ms |
3148 KB |
Output is correct |
12 |
Correct |
1516 ms |
3196 KB |
Output is correct |
13 |
Correct |
630 ms |
3196 KB |
Output is correct |
14 |
Correct |
879 ms |
2268 KB |
Output is correct |
15 |
Correct |
1156 ms |
2404 KB |
Output is correct |
16 |
Correct |
2960 ms |
3396 KB |
Output is correct |
17 |
Correct |
2832 ms |
4156 KB |
Output is correct |
18 |
Correct |
3110 ms |
4164 KB |
Output is correct |
19 |
Correct |
1610 ms |
4124 KB |
Output is correct |
20 |
Correct |
2593 ms |
4264 KB |
Output is correct |
21 |
Correct |
2510 ms |
4248 KB |
Output is correct |
22 |
Correct |
1054 ms |
4124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
893 ms |
1836 KB |
Output is correct |
8 |
Correct |
888 ms |
2048 KB |
Output is correct |
9 |
Correct |
843 ms |
3152 KB |
Output is correct |
10 |
Correct |
614 ms |
3176 KB |
Output is correct |
11 |
Correct |
598 ms |
3148 KB |
Output is correct |
12 |
Correct |
1516 ms |
3196 KB |
Output is correct |
13 |
Correct |
630 ms |
3196 KB |
Output is correct |
14 |
Correct |
879 ms |
2268 KB |
Output is correct |
15 |
Correct |
1156 ms |
2404 KB |
Output is correct |
16 |
Correct |
2960 ms |
3396 KB |
Output is correct |
17 |
Correct |
2832 ms |
4156 KB |
Output is correct |
18 |
Correct |
3110 ms |
4164 KB |
Output is correct |
19 |
Correct |
1610 ms |
4124 KB |
Output is correct |
20 |
Correct |
2593 ms |
4264 KB |
Output is correct |
21 |
Correct |
2510 ms |
4248 KB |
Output is correct |
22 |
Correct |
1054 ms |
4124 KB |
Output is correct |
23 |
Correct |
4702 ms |
7960 KB |
Output is correct |
24 |
Correct |
5428 ms |
7888 KB |
Output is correct |
25 |
Correct |
3641 ms |
7976 KB |
Output is correct |
26 |
Correct |
3802 ms |
7968 KB |
Output is correct |
27 |
Correct |
4560 ms |
7884 KB |
Output is correct |
28 |
Correct |
4027 ms |
2780 KB |
Output is correct |
29 |
Correct |
4073 ms |
2656 KB |
Output is correct |
30 |
Correct |
4480 ms |
2784 KB |
Output is correct |
31 |
Correct |
4169 ms |
2660 KB |
Output is correct |
32 |
Correct |
3527 ms |
7968 KB |
Output is correct |
33 |
Correct |
2676 ms |
7964 KB |
Output is correct |
34 |
Correct |
3413 ms |
7968 KB |
Output is correct |
35 |
Correct |
2323 ms |
8544 KB |
Output is correct |
36 |
Correct |
1808 ms |
7968 KB |
Output is correct |
37 |
Correct |
5571 ms |
8656 KB |
Output is correct |
38 |
Correct |
3541 ms |
7968 KB |
Output is correct |
39 |
Correct |
4091 ms |
7972 KB |
Output is correct |
40 |
Correct |
3557 ms |
7968 KB |
Output is correct |
41 |
Correct |
7681 ms |
8120 KB |
Output is correct |
42 |
Correct |
7629 ms |
7976 KB |
Output is correct |