#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 read() {
char c = getchar_unlocked();
while (c < '0' or c > '9') {
c = getchar_unlocked();
}
int x = 0;
while ('0' <= c and c <= '9') {
x *= 10;
x += c - '0';
c = getchar_unlocked();
}
return x;
}
int main() {
int H = read();
int W = read();
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) {
A[i] = read();
B[i] = read();
C[i] = read();
D[i] = read();
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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
276 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
276 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
12 ms |
1100 KB |
Output is correct |
26 |
Correct |
33 ms |
2640 KB |
Output is correct |
27 |
Correct |
96 ms |
5288 KB |
Output is correct |
28 |
Correct |
83 ms |
3032 KB |
Output is correct |
29 |
Correct |
75 ms |
4520 KB |
Output is correct |
30 |
Correct |
112 ms |
3524 KB |
Output is correct |
31 |
Correct |
148 ms |
8284 KB |
Output is correct |
32 |
Correct |
141 ms |
6744 KB |
Output is correct |
33 |
Correct |
21 ms |
2600 KB |
Output is correct |
34 |
Correct |
74 ms |
6400 KB |
Output is correct |
35 |
Correct |
113 ms |
12308 KB |
Output is correct |
36 |
Correct |
162 ms |
12380 KB |
Output is correct |
37 |
Correct |
149 ms |
12356 KB |
Output is correct |
38 |
Correct |
147 ms |
12420 KB |
Output is correct |