This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* 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 (stderr)
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:103:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
103 | int mid = low + high >> 1;
| ~~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |