#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
struct SegmentTree {
int n;
vector <ll> t;
void modify(int id, ll val, int l, int r, int v) {
if (l == r) {
t[v] = min(t[v], val);
} else {
int m = (l + r) >> 1;
if (id <= m) modify(id, val, l, m, 2 * v + 1);
else modify(id, val, m + 1, r, 2 * v + 2);
t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
}
ll get(int ql, int qr, int l, int r, int v) {
if (qr < l || r < ql) return Inf;
if (ql <= l && r <= qr) return t[v];
int m = (l + r) >> 1;
return min(get(ql, qr, l, m, 2 * v + 1), get(ql, qr, m + 1, r, 2 * v + 2));
}
SegmentTree(int nn) {
n = nn;
t.resize(4 * n, Inf);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
{
vector <int> cmp;
for (int i = 0; i < n; ++i) {
cmp.push_back(a[i]);
cmp.push_back(b[i]);
cmp.push_back(c[i]);
}
cmp.push_back(1);
cmp.push_back(m);
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
m = cmp.size();
auto go = [&] (int &x) {
x = lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin() + 1;
};
for (int i = 0; i < n; ++i) {
go(a[i]);
go(b[i]);
go(c[i]);
}
}
vector <ll> dpL(n, Inf), dpR(n, Inf);
SegmentTree trL(m + 1), trR(m + 1);
ll ans = Inf;
for (int i = 0; i < n; ++i) {
ll costL = trL.get(a[i], b[i], 0, m, 0), costR = trR.get(a[i], b[i], 0, m, 0);
if (a[i] == 1) costL = 0;
if (b[i] == m) costR = 0;
ans = min(ans, costL + costR + d[i]);
if (a[i] == 1) {
dpL[i] = d[i];
} else {
dpL[i] = min(dpL[i], d[i] + trL.get(a[i], b[i], 0, m, 0));
}
if (b[i] == m) {
dpR[i] = d[i];
} else {
dpR[i] = min(dpR[i], d[i] + trR.get(a[i], b[i], 0, m, 0));
}
//cout << c[i] << ' ' << dpL[i] << ' ' << dpR[i] << endl;
trL.modify(c[i], dpL[i], 0, m, 0);
trR.modify(c[i], dpR[i], 0, m, 0);
}
if (ans != Inf) cout << ans << '\n';
else cout << "-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
27 ms |
2056 KB |
Output is correct |
26 |
Correct |
74 ms |
5448 KB |
Output is correct |
27 |
Correct |
212 ms |
11576 KB |
Output is correct |
28 |
Correct |
202 ms |
7920 KB |
Output is correct |
29 |
Correct |
161 ms |
9696 KB |
Output is correct |
30 |
Correct |
257 ms |
8656 KB |
Output is correct |
31 |
Correct |
337 ms |
18020 KB |
Output is correct |
32 |
Correct |
313 ms |
15056 KB |
Output is correct |
33 |
Correct |
39 ms |
5392 KB |
Output is correct |
34 |
Correct |
155 ms |
13260 KB |
Output is correct |
35 |
Correct |
217 ms |
26224 KB |
Output is correct |
36 |
Correct |
354 ms |
26072 KB |
Output is correct |
37 |
Correct |
300 ms |
26192 KB |
Output is correct |
38 |
Correct |
286 ms |
26072 KB |
Output is correct |