/**
* author: wxhtzdy
* created: 20.07.2022 11:59:55
**/
#include "elephants.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 150005;
const int BLOCK = sqrt(MAX);
int n, l, x[MAX], cnt[MAX], pos[MAX];
int id[MAX], order[MAX];
vector<int> idx[MAX / BLOCK + 5];
void BuildBlock(int i) {
int sz = (int) idx[i].size();
int ptr = sz - 1;
for (int j = sz - 1; j >= 0; j--) {
if (x[idx[i][sz - 1]] - x[idx[i][j]] <= l) {
cnt[idx[i][j]] = 1;
pos[idx[i][j]] = x[idx[i][j]] + l;
} else {
while (x[idx[i][ptr]] - x[idx[i][j]] > l) {
ptr -= 1;
}
cnt[idx[i][j]] = cnt[idx[i][ptr + 1]] + 1;
pos[idx[i][j]] = pos[idx[i][ptr + 1]];
}
}
}
void Build() {
for (int i = 0; i < n; i++) {
order[i] = i;
}
sort(order, order + n, [&](int i, int j) {
return x[i] < x[j];
});
for (int i = 0; i <= n / BLOCK; i++) {
idx[i].clear();
}
for (int i = 0; i < n; i++) {
id[order[i]] = i / BLOCK;
idx[id[order[i]]].push_back(order[i]);
}
for (int i = 0; i <= n / BLOCK; i++) {
BuildBlock(i);
}
}
int Q;
int update(int i, int y) {
++Q;
x[i] = y;
if (Q == 2 * BLOCK) {
Build();
Q = 0;
} else {
vector<int> b;
for (int j = 0; j < (int) idx[id[i]].size(); j++) {
if (idx[id[i]][j] != i) {
b.push_back(idx[id[i]][j]);
}
}
idx[id[i]] = b;
BuildBlock(id[i]);
b.clear();
id[i] = 0;
for (int j = n / BLOCK; j >= 0; j--) {
if (!idx[j].empty() && x[idx[j][0]] <= x[i]) {
id[i] = j;
break;
}
}
if (idx[id[i]].empty() || x[idx[id[i]].back()] < y) {
idx[id[i]].push_back(i);
} else {
vector<int> b;
for (int j = 0; j < (int) idx[id[i]].size(); j++) {
if (y <= x[idx[id[i]][j]]) {
b.push_back(i);
y = (int) 1.01e9;
j -= 1;
} else {
b.push_back(idx[id[i]][j]);
}
}
idx[id[i]] = b;
}
BuildBlock(id[i]);
}
int ans = 0, R = -1;
for (int j = 0; j <= n / BLOCK; j++) {
if (idx[j].empty() || x[idx[j].back()] <= R) {
continue;
}
int low = 0, high = (int) idx[j].size() - 1, at = 0;
while (low <= high) {
int mid = low + high >> 1;
if (x[idx[j][mid]] > R) {
at = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
ans += cnt[idx[j][at]];
R = pos[idx[j][at]];
}
return ans;
}
void init(int N, int L, int* X) {
n = N;
l = L;
for (int i = 0; i < n; i++) {
x[i] = X[i];
}
Build();
}
Compilation message
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:103:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
103 | int mid = low + high >> 1;
| ~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
264 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
264 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
505 ms |
2196 KB |
Output is correct |
8 |
Correct |
532 ms |
2336 KB |
Output is correct |
9 |
Correct |
645 ms |
3924 KB |
Output is correct |
10 |
Correct |
524 ms |
3704 KB |
Output is correct |
11 |
Correct |
520 ms |
3644 KB |
Output is correct |
12 |
Correct |
1014 ms |
3812 KB |
Output is correct |
13 |
Correct |
522 ms |
3496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
264 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
505 ms |
2196 KB |
Output is correct |
8 |
Correct |
532 ms |
2336 KB |
Output is correct |
9 |
Correct |
645 ms |
3924 KB |
Output is correct |
10 |
Correct |
524 ms |
3704 KB |
Output is correct |
11 |
Correct |
520 ms |
3644 KB |
Output is correct |
12 |
Correct |
1014 ms |
3812 KB |
Output is correct |
13 |
Correct |
522 ms |
3496 KB |
Output is correct |
14 |
Correct |
761 ms |
3372 KB |
Output is correct |
15 |
Correct |
795 ms |
3248 KB |
Output is correct |
16 |
Correct |
1576 ms |
4408 KB |
Output is correct |
17 |
Correct |
1717 ms |
5228 KB |
Output is correct |
18 |
Correct |
1821 ms |
5172 KB |
Output is correct |
19 |
Correct |
1369 ms |
5368 KB |
Output is correct |
20 |
Correct |
1742 ms |
5228 KB |
Output is correct |
21 |
Correct |
1721 ms |
5200 KB |
Output is correct |
22 |
Correct |
965 ms |
4788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
264 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
505 ms |
2196 KB |
Output is correct |
8 |
Correct |
532 ms |
2336 KB |
Output is correct |
9 |
Correct |
645 ms |
3924 KB |
Output is correct |
10 |
Correct |
524 ms |
3704 KB |
Output is correct |
11 |
Correct |
520 ms |
3644 KB |
Output is correct |
12 |
Correct |
1014 ms |
3812 KB |
Output is correct |
13 |
Correct |
522 ms |
3496 KB |
Output is correct |
14 |
Correct |
761 ms |
3372 KB |
Output is correct |
15 |
Correct |
795 ms |
3248 KB |
Output is correct |
16 |
Correct |
1576 ms |
4408 KB |
Output is correct |
17 |
Correct |
1717 ms |
5228 KB |
Output is correct |
18 |
Correct |
1821 ms |
5172 KB |
Output is correct |
19 |
Correct |
1369 ms |
5368 KB |
Output is correct |
20 |
Correct |
1742 ms |
5228 KB |
Output is correct |
21 |
Correct |
1721 ms |
5200 KB |
Output is correct |
22 |
Correct |
965 ms |
4788 KB |
Output is correct |
23 |
Correct |
3645 ms |
11404 KB |
Output is correct |
24 |
Correct |
3952 ms |
11408 KB |
Output is correct |
25 |
Correct |
3195 ms |
11328 KB |
Output is correct |
26 |
Correct |
4070 ms |
11420 KB |
Output is correct |
27 |
Correct |
4988 ms |
11284 KB |
Output is correct |
28 |
Correct |
1914 ms |
5172 KB |
Output is correct |
29 |
Correct |
1854 ms |
5052 KB |
Output is correct |
30 |
Correct |
1824 ms |
5264 KB |
Output is correct |
31 |
Correct |
1815 ms |
5180 KB |
Output is correct |
32 |
Correct |
3137 ms |
10868 KB |
Output is correct |
33 |
Correct |
2977 ms |
10188 KB |
Output is correct |
34 |
Correct |
3545 ms |
11056 KB |
Output is correct |
35 |
Correct |
2965 ms |
11356 KB |
Output is correct |
36 |
Correct |
2436 ms |
10840 KB |
Output is correct |
37 |
Correct |
6494 ms |
11540 KB |
Output is correct |
38 |
Correct |
3905 ms |
10060 KB |
Output is correct |
39 |
Correct |
4944 ms |
11112 KB |
Output is correct |
40 |
Correct |
3818 ms |
10084 KB |
Output is correct |
41 |
Correct |
6004 ms |
11000 KB |
Output is correct |
42 |
Correct |
6143 ms |
11092 KB |
Output is correct |