#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
using ll = long long;
constexpr ll INF = std::numeric_limits<ll>::max() / 3;
struct Segtree {
int size;
Vec<ll> min;
Segtree(const int n): size(n), min(2 * n, INF) { }
void setmin(int i, const ll x) {
i += size;
while (i > 0) {
min[i] = std::min(min[i], x);
i >>= 1;
}
}
ll fold(int l, int r) const {
ll ret = INF;
l += size;
r += size;
while (l < r) {
if (l & 1) ret = std::min(ret, min[l++]);
if (r & 1) ret = std::min(ret, min[--r]);
l >>= 1;
r >>= 1;
}
return ret;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int H, W;
std::cin >> H >> W;
Vec<int> A(H), B(H), C(H), D(H);
Vec<int> cmp;
cmp.reserve(3 * H + 2);
cmp.push_back(1);
cmp.push_back(W);
for (int i = 0; i < H; ++i) {
std::cin >> A[i] >> B[i] >> C[i] >> D[i];
cmp.push_back(A[i]);
cmp.push_back(B[i]);
cmp.push_back(C[i]);
}
std::sort(cmp.begin(), cmp.end());
cmp.erase(std::unique(cmp.begin(), cmp.end()), cmp.end());
for (auto& x: A) {
x = std::lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
}
for (auto& x: B) {
x = std::lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
}
for (auto& x: C) {
x = std::lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
}
W = (int) cmp.size();
Segtree left(W), right(W);
left.setmin(0, 0);
right.setmin(W - 1, 0);
ll ans = INF;
for (int i = 0; i < H; ++i) {
left.setmin(C[i], left.fold(A[i], C[i]) + D[i]);
right.setmin(C[i], right.fold(C[i] + 1, B[i] + 1) + D[i]);
ans = std::min(ans, left.fold(A[i], W) + right.fold(0, B[i] + 1) + D[i]);
}
if (ans == INF) {
std::cout << -1 << '\n';
}
else {
std::cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
17 ms |
1104 KB |
Output is correct |
26 |
Correct |
41 ms |
2668 KB |
Output is correct |
27 |
Correct |
142 ms |
5308 KB |
Output is correct |
28 |
Correct |
116 ms |
3048 KB |
Output is correct |
29 |
Correct |
85 ms |
4468 KB |
Output is correct |
30 |
Correct |
144 ms |
3616 KB |
Output is correct |
31 |
Correct |
185 ms |
8328 KB |
Output is correct |
32 |
Correct |
173 ms |
6828 KB |
Output is correct |
33 |
Correct |
24 ms |
2660 KB |
Output is correct |
34 |
Correct |
86 ms |
6348 KB |
Output is correct |
35 |
Correct |
123 ms |
12320 KB |
Output is correct |
36 |
Correct |
202 ms |
12356 KB |
Output is correct |
37 |
Correct |
176 ms |
12436 KB |
Output is correct |
38 |
Correct |
190 ms |
12348 KB |
Output is correct |