#include <bits/stdc++.h>
using namespace std;
struct Seg{
int n;
vector<long long> s;
Seg(int _n = 0): n(_n) {
s.resize(4 << __lg(n), 2e18);
}
void upd(int i, long long v) {
int l = 1, r = n, id = 1;
while (l < r) {
int mid = (l + r) / 2;
id *= 2;
if (i <= mid) {
r = mid;
} else {
l = mid + 1;
id++;
}
}
s[id] = min(s[id], v);
while (id > 1) {
id /= 2;
s[id] = min(s[id * 2], s[id * 2 + 1]);
}
}
long long get(int i, int j, int id, int l, int r) {
if (i <= l && r <= j) {
return s[id];
}
int mid = (l + r) / 2;
if (i <= mid && mid < j) {
return min(get(i, j, id * 2, l, mid), get(i, j, id * 2 + 1, mid + 1, r));
}
if (mid < i) {
return get(i, j, id * 2 + 1, mid + 1, r);
}
return get(i, j, id * 2, l, mid);
}
long long get(int i, int j) {
return get(i, j, 1, 1, n);
}
};
int m, n;
int a[100005], b[100005], c[100005], d[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
vector<int> val;
val.emplace_back(1);
val.emplace_back(n);
for (int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
val.emplace_back(a[i]);
val.emplace_back(b[i]);
val.emplace_back(c[i]);
}
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for (int i = 1; i <= m; ++i) {
a[i] = lower_bound(val.begin(), val.end(), a[i]) - val.begin() + 1;
b[i] = lower_bound(val.begin(), val.end(), b[i]) - val.begin() + 1;
c[i] = lower_bound(val.begin(), val.end(), c[i]) - val.begin() + 1;
//cout << a[i] << ' ' << b[i] << ' ' << c[i] << '\n';
}
int t = val.size();
Seg ST1(t), ST2(t);
long long ans = 2e18;
ST1.upd(1, 0);
ST2.upd(t, 0);
for (int i = 1; i <= m; ++i) {
long long k1 = ST1.get(a[i], b[i]);
long long k2 = ST2.get(a[i], b[i]);
if (max(k1, k2) < 2e18) {
ans = min(ans, k1 + k2 + d[i]);
}
if (k1 < 2e18) {
ST1.upd(c[i], k1 + d[i]);
}
if (k2 < 2e18) {
ST2.upd(c[i], k2 + d[i]);
}
}
if (ans < 2e18) {
cout << ans << '\n';
} else {
cout << "-1\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
648 KB |
Output is correct |
25 |
Correct |
12 ms |
1880 KB |
Output is correct |
26 |
Correct |
32 ms |
3288 KB |
Output is correct |
27 |
Correct |
95 ms |
6604 KB |
Output is correct |
28 |
Correct |
92 ms |
3792 KB |
Output is correct |
29 |
Correct |
63 ms |
5840 KB |
Output is correct |
30 |
Correct |
120 ms |
4220 KB |
Output is correct |
31 |
Correct |
146 ms |
11388 KB |
Output is correct |
32 |
Correct |
141 ms |
7624 KB |
Output is correct |
33 |
Correct |
16 ms |
3040 KB |
Output is correct |
34 |
Correct |
59 ms |
10148 KB |
Output is correct |
35 |
Correct |
80 ms |
19652 KB |
Output is correct |
36 |
Correct |
145 ms |
19652 KB |
Output is correct |
37 |
Correct |
117 ms |
19660 KB |
Output is correct |
38 |
Correct |
106 ms |
19656 KB |
Output is correct |