#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
// change if necessary
#define MAXN 100010
using namespace std;
int n, m;
array<int, 4> nums[MAXN];
map<int, int> x;
ll st[12 * MAXN][2];
ll ans = INFLL;
ll ckmin(ll &a, ll b) {
return a = min(a, b);
}
void build(int v = 1, int l = 1, int r = 3 * m) {
st[v][0] = INFLL;
st[v][1] = INFLL;
if (l != r) {
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m + 1, r);
}
}
void upd(int x, ll y, int idx, int v = 1, int l = 1, int r = 3 * m) {
if (l == r) {
ckmin(st[v][idx], y);
} else {
int m = (l + r) / 2;
if (x <= m) {
upd(x, y, idx, 2 * v, l, m);
} else {
upd(x, y, idx, 2 * v + 1, m + 1, r);
}
st[v][idx] = min(st[2 * v][idx], st[2 * v + 1][idx]);
}
}
ll qry(int x, int y, int idx, int v = 1, int l = 1, int r = 3 * m) {
if (x <= l && r <= y) {
return st[v][idx];
} else if (r < x || l > y) {
return INFLL;
} else {
int m = (l + r) / 2;
return min(qry(x, y, idx, 2 * v, l, m), qry(x, y, idx, 2 * v + 1, m + 1, r));
}
}
int main() {
/* cin.tie(0)->sync_with_stdio(0); */
cin >> m >> n;
x[1] = 0;
x[n] = 0;
for (int i = 0; i < m; i++) {
int a, b, c, d; cin >> a >> b >> c >> d;
x[a] = 0;
x[b] = 0;
x[c] = 0;
nums[i] = {a, b, c, d};
}
int t = 0;
for (auto &p : x) {
p.second = ++t;
}
build();
upd(x[1], 0, 0);
upd(x[n], 0, 1);
for (int i = 0; i < m; i++) {
int a = x[nums[i][0]];
int b = x[nums[i][1]];
int c = x[nums[i][2]];
int d = nums[i][3];
ll min_1 = qry(a, b, 0);
ll min_n = qry(a, b, 1);
ckmin(ans, min_1 + min_n + d);
upd(c, min_1 + d, 0);
upd(c, min_n + d, 1);
}
if (ans == INFLL) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
256 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 |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
256 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 |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 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 |
256 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 |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
3 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
25 |
Correct |
53 ms |
2700 KB |
Output is correct |
26 |
Correct |
162 ms |
8440 KB |
Output is correct |
27 |
Correct |
505 ms |
16888 KB |
Output is correct |
28 |
Correct |
491 ms |
22392 KB |
Output is correct |
29 |
Correct |
373 ms |
15704 KB |
Output is correct |
30 |
Correct |
578 ms |
23288 KB |
Output is correct |
31 |
Correct |
855 ms |
30076 KB |
Output is correct |
32 |
Correct |
785 ms |
27896 KB |
Output is correct |
33 |
Correct |
103 ms |
6136 KB |
Output is correct |
34 |
Correct |
396 ms |
18424 KB |
Output is correct |
35 |
Correct |
582 ms |
36216 KB |
Output is correct |
36 |
Correct |
985 ms |
36248 KB |
Output is correct |
37 |
Correct |
850 ms |
36216 KB |
Output is correct |
38 |
Correct |
792 ms |
36216 KB |
Output is correct |