#include <bits/stdc++.h>
using i64 = long long;
constexpr int inf = 2001111111;
struct SegmentTree {
int n;
std::vector<int> mn, mn2;
SegmentTree(int n) : n(n), mn(4 * n, -inf), mn2(4 * n, inf) {}
void push(int p) {
mn[2 * p] = std::max(mn[2 * p], mn[p]);
mn[2 * p + 1] = std::max(mn[2 * p + 1], mn[p]);
}
void modify(int p, int l, int r, int x, int y) {
if (l == r - 1) {
mn[p] = y;
mn2[p] = inf;
} else {
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, y);
} else {
modify(2 * p + 1, m, r, x, y);
}
mn[p] = std::min(mn[2 * p], mn[2 * p + 1]);
if (mn[2 * p] == mn[2 * p + 1]) {
mn2[p] = std::min(mn2[2 * p], mn2[2 * p + 1]);
} else if (mn[2 * p] == mn[p]) {
mn2[p] = std::min(mn2[2 * p], mn[2 * p + 1]);
} else {
mn2[p] = std::min(mn[2 * p], mn2[2 * p + 1]);
}
}
}
void modify(int x, int y) {
modify(1, 0, n, x, y);
}
void chmax(int p, int l, int r, int x, int y, int v) {
if (y <= l || r <= x || v <= mn[p]) return;
if (x <= l && r <= y && v < mn2[p]) { // could be here
mn[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
chmax(2 * p, l, m, x, y, v);
chmax(2 * p + 1, m, r, x, y, v);
mn[p] = std::min(mn[2 * p], mn[2 * p + 1]);
if (mn[2 * p] == mn[2 * p + 1]) {
mn2[p] = std::min(mn2[2 * p], mn2[2 * p + 1]);
} else if (mn[2 * p] == mn[p]) {
mn2[p] = std::min(mn2[2 * p], mn[2 * p + 1]);
} else {
mn2[p] = std::min(mn[2 * p], mn2[2 * p + 1]);
}
}
void chmax(int l, int r, int v) {
//~ for (int i = l; i < r; i++) {
//~ modify(i, std::max(at(i), v));
//~ }
chmax(1, 0, n, l, r, v);
}
int at(int p, int l, int r, int x) {
if (l == r - 1) {
return mn[p];
} else {
int m = (l + r) / 2;
push(p);
if (x < m) {
return at(2 * p, l, m, x);
} else {
return at(2 * p + 1, m, r, x);
}
}
}
int at(int x) {
return at(1, 0, n, x);
}
void add(int l, int r, int v) {
for (int i = l; i < r; i++) {
modify(i, at(i) + v);
}
}
};
void chmax(int &a, int b) {
if (a < b) {
a = b;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, M;
std::cin >> n >> M;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
SegmentTree s(n + 1);
s.modify(n, 0);
for (int i = 0; i < n; i++) {
int lo = 0, hi = n + 1;
while (lo + 1 < hi) {
int m = (lo + hi) / 2;
if (a[i] - s.at(m) <= M) {
hi = m;
} else {
lo = m;
}
}
//~ std::cout << hi << "\n";
//~ for (int i = 0; i <= n; i++) {
//~ std::cout << s.at(i) << " ";
//~ } std::cout << "\n";
s.add(0, n + 1, M);
s.chmax(hi - 1, n, a[i]);
}
//~ for (int i = 0; i <= n; i++) {
//~ std::cout << s.at(i) << " ";
//~ } std::cout << "\n";
for (int i = 0; i <= n; i++) {
if (s.at(i) >= 0) {
std::cout << i << "\n";
return 0;
}
}
assert(false);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Execution timed out |
1082 ms |
340 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
1082 ms |
340 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
1082 ms |
340 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |