#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const long long inf = 1e18;
struct Segtree {
int N;
vector<long long> S;
Segtree(int N): N(N) {
S.resize(4 << __lg(N), inf);
}
void upd(int i, long long x, int id, int l, int r) {
if (l == r) {
S[id] = min(S[id], x);
return;
}
int md = (l + r) / 2;
if (i <= md) {
upd(i, x, id * 2, l, md);
} else {
upd(i, x, id * 2 + 1, md + 1, r);
}
S[id] = min(S[id * 2], S[id * 2 + 1]);
}
void upd(int i, long long x) {
upd(i, x, 1, 1, N);
}
long long qry(int u, int v, int id, int l, int r) {
if (u <= l && r <= v) {
return S[id];
}
int md = (l + r) / 2;
if (v <= md) {
return qry(u, v, id * 2, l, md);
}
if (md < u) {
return qry(u, v, id * 2 + 1, md + 1, r);
}
return min(qry(u, v, id * 2, l, md), qry(u, v, id * 2 + 1, md + 1, r));
}
long long qry(int u, int v) {
return qry(u, v, 1, 1, N);
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int M, N; cin >> M >> N;
vector<int> comp = {1, N};
vector<array<int, 4>> cands;
for (int i = 0; i < M; ++i) {
int a, b, c, d; cin >> a >> b >> c >> d;
cands.push_back({a, b, c, d});
for (int j = 0; j < 3; ++j) {
comp.push_back(cands.back()[j]);
}
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < M; ++i) {
for (int j = 0; j < 3; ++j) {
cands[i][j] = lower_bound(comp.begin(), comp.end(), cands[i][j]) - comp.begin() + 1;
}
}
int K = comp.size();
Segtree lt(K), rt(K);
lt.upd(1, 0);
rt.upd(K, 0);
long long res = inf;
for (int i = 0; i < M; ++i) {
int l = cands[i][0], r = cands[i][1], x = cands[i][2];
auto a = lt.qry(l, r), b = rt.qry(l, r);
if (a != inf && b != inf) {
res = min(res, a + b + cands[i][3]);
}
if (a != inf) {
lt.upd(x, a + cands[i][3]);
}
if (b != inf) {
rt.upd(x, b + cands[i][3]);
}
}
cout << (res == inf ? -1 : res);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
384 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 |
516 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
384 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 |
516 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
11 ms |
2008 KB |
Output is correct |
26 |
Correct |
34 ms |
4304 KB |
Output is correct |
27 |
Correct |
94 ms |
9396 KB |
Output is correct |
28 |
Correct |
94 ms |
7608 KB |
Output is correct |
29 |
Correct |
62 ms |
7872 KB |
Output is correct |
30 |
Correct |
109 ms |
8000 KB |
Output is correct |
31 |
Correct |
161 ms |
15536 KB |
Output is correct |
32 |
Correct |
151 ms |
11444 KB |
Output is correct |
33 |
Correct |
17 ms |
3796 KB |
Output is correct |
34 |
Correct |
59 ms |
11960 KB |
Output is correct |
35 |
Correct |
84 ms |
23676 KB |
Output is correct |
36 |
Correct |
143 ms |
23664 KB |
Output is correct |
37 |
Correct |
119 ms |
23680 KB |
Output is correct |
38 |
Correct |
116 ms |
23472 KB |
Output is correct |