#include <bits/stdc++.h>
using i64 = long long;
constexpr int inf = 2001111111;
struct SegmentTree {
int n;
std::vector<int> mn, mn2, lazy;
SegmentTree(int n) : n(n), mn(4 * n, -inf), mn2(4 * n, inf), lazy(4 * n) {}
void push(int p) {
mn[2 * p] += lazy[p];
mn[2 * p + 1] += lazy[p];
mn2[2 * p] += lazy[p];
mn2[2 * p + 1] += lazy[p];
mn[2 * p] = std::max(mn[2 * p], mn[p]);
mn[2 * p + 1] = std::max(mn[2 * p + 1], mn[p]);
lazy[2 * p] += lazy[p];
lazy[2 * p + 1] += lazy[p];
lazy[p] = 0;
}
void pull(int p) {
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 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);
}
pull(p);
}
}
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]);
pull(p);
}
void chmax(int l, int r, int 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 p, int l, int r, int x, int y, int v) {
if (y <= l || r <= x) return;
if (x <= l && r <= y) {
lazy[p] += v;
mn[p] += v;
mn2[p] += v;
return;
}
int m = (l + r) / 2;
add(2 * p, l, m, x, y, v);
add(2 * p + 1, m, r, x, y, v);
pull(p);
}
void add(int l, int r, int v) {
add(1, 0, n, l, r, 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
7 ms |
468 KB |
Output is correct |
21 |
Correct |
10 ms |
468 KB |
Output is correct |
22 |
Correct |
11 ms |
592 KB |
Output is correct |
23 |
Correct |
10 ms |
596 KB |
Output is correct |
24 |
Correct |
9 ms |
468 KB |
Output is correct |
25 |
Correct |
10 ms |
468 KB |
Output is correct |
26 |
Correct |
10 ms |
492 KB |
Output is correct |
27 |
Correct |
11 ms |
596 KB |
Output is correct |
28 |
Correct |
9 ms |
600 KB |
Output is correct |
29 |
Correct |
10 ms |
588 KB |
Output is correct |
30 |
Correct |
10 ms |
592 KB |
Output is correct |
31 |
Correct |
11 ms |
468 KB |
Output is correct |
32 |
Correct |
10 ms |
600 KB |
Output is correct |
33 |
Correct |
9 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
468 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Correct |
11 ms |
592 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
9 ms |
468 KB |
Output is correct |
9 |
Correct |
10 ms |
468 KB |
Output is correct |
10 |
Correct |
10 ms |
492 KB |
Output is correct |
11 |
Correct |
11 ms |
596 KB |
Output is correct |
12 |
Correct |
9 ms |
600 KB |
Output is correct |
13 |
Correct |
10 ms |
588 KB |
Output is correct |
14 |
Correct |
10 ms |
592 KB |
Output is correct |
15 |
Correct |
11 ms |
468 KB |
Output is correct |
16 |
Correct |
10 ms |
600 KB |
Output is correct |
17 |
Correct |
9 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Incorrect |
11 ms |
596 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |