#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 |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 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 |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
536 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 |
540 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 |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
536 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 |
540 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 |
604 KB |
Output is correct |
25 |
Correct |
12 ms |
2140 KB |
Output is correct |
26 |
Correct |
34 ms |
3972 KB |
Output is correct |
27 |
Correct |
100 ms |
9140 KB |
Output is correct |
28 |
Correct |
102 ms |
7372 KB |
Output is correct |
29 |
Correct |
66 ms |
7784 KB |
Output is correct |
30 |
Correct |
117 ms |
7880 KB |
Output is correct |
31 |
Correct |
144 ms |
15308 KB |
Output is correct |
32 |
Correct |
141 ms |
11300 KB |
Output is correct |
33 |
Correct |
17 ms |
3808 KB |
Output is correct |
34 |
Correct |
58 ms |
12092 KB |
Output is correct |
35 |
Correct |
85 ms |
23416 KB |
Output is correct |
36 |
Correct |
172 ms |
23496 KB |
Output is correct |
37 |
Correct |
105 ms |
23548 KB |
Output is correct |
38 |
Correct |
123 ms |
23492 KB |
Output is correct |