이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* author: wxhtzdy
* created: 28.11.2022 15:11:47
**/
#include <bits/stdc++.h>
using namespace std;
int readint() {
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
return x;
}
const int N = 2000005;
int a[N];
int add[4 * N];
int mx[4 * N];
int idx[4 * N];
pair<int, int> mn[4 * N];
int prv[N];
int main() {
int n = readint();
int d = readint();
int t = readint();
for (int i = 0; i < n; i++) {
a[i] = readint();
}
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()] + i - stk.back() > t) {
stk.pop_back();
}
while (!stk.empty() && a[stk.back()] - stk.back() > a[i] - i) {
stk.pop_back();
}
prv[i] = (a[i] <= t ? i : (stk.empty() ? -1 : stk.back()));
stk.push_back(i);
}
function<void(int, int, int)> Build = [&](int x, int l, int r) {
idx[x] = l;
if (l == r) {
mn[x] = make_pair(prv[l] == -1 ? n + 1 : prv[l], l);
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
Build(x + 1, l, y);
Build(z, y + 1, r);
mn[x] = min(mn[x + 1], mn[z]);
};
function<void(int, int, int)> Pull = [&](int x, int y, int z) {
if (mx[y] >= mx[z]) {
mx[x] = mx[y];
idx[x] = idx[y];
} else {
mx[x] = mx[z];
idx[x] = idx[z];
}
};
function<void(int, int, int, int, int, int)> Modify = [&](int x, int l, int r, int ll, int rr, int v) {
if (ll <= l && r <= rr) {
add[x] += v;
mx[x] += v;
return;
}
if (l > rr || r < ll) {
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
if (add[x] != 0) {
mx[x + 1] += add[x];
mx[z] += add[x];
add[x + 1] += add[x];
add[z] += add[x];
add[x] = 0;
}
Modify(x + 1, l, y, ll, rr, v);
Modify(z, y + 1, r, ll, rr, v);
Pull(x, x + 1, z);
};
Build(0, 0, n - 1);
function<pair<int, int>(int, int, int, int, int)> Query = [&](int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return mn[x];
}
if (l > rr || r < ll) {
return make_pair(n + 2, -1);
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
return min(Query(x + 1, l, y, ll, rr), Query(z, y + 1, r, ll, rr));
};
function<void(int, int, int, int)> Remove = [&](int x, int l, int r, int i) {
if (l == r) {
mn[x] = make_pair(n + 1, l);
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
if (i <= y) {
Remove(x + 1, l, y, i);
} else {
Remove(z, y + 1, r, i);
}
mn[x] = min(mn[x + 1], mn[z]);
};
int ans = 0;
for (int i = 0; i < n; i++) {
if (prv[i] != -1) {
ans += 1;
if (prv[i] < i) {
Modify(0, 0, n - 1, prv[i], i - 1, +1);
}
}
}
while (d--) {
int i = idx[0];
ans -= mx[0];
assert(mx[0] >= 0);
Modify(0, 0, n - 1, i, i, -1e9);
while (i < n - 1) {
auto p = Query(0, 0, n - 1, i + 1, n - 1);
if (p.first <= i) {
Modify(0, 0, n - 1, p.first, p.second - 1, -1);
Remove(0, 0, n - 1, p.second);
} else {
break;
}
}
}
printf("%d", ans);
return 0;
}
| # | 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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |