#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 1e5 + 2;
int64 a[maxn], b[maxn], c[maxn], cost[maxn];
vector <int64> coord;
int64 sgt[2][12 * maxn]; // sgt[0] -> l, sgt[1] -> r;
int64 n,m;
int64 query (int x, int l, int r, int il, int ir, int idx) {
if (il <= l && r <= ir) return sgt[idx][x];
if (l > ir || r < il) return 1e15;
int mid = (l + r) / 2;
return min(query(x * 2, l, mid, il, ir, idx), query(x * 2 + 1, mid + 1, r, il, ir, idx));
}
int64 update (int x, int l, int r, int pos, int64 nval, int idx) {
if (l == pos && r == pos) return sgt[idx][x] = min(sgt[idx][x], nval);
if (l > pos || r < pos) return sgt[idx][x];
int mid = (l + r) / 2;
return sgt[idx][x] = min(update(x * 2, l, mid, pos, nval, idx), update(x * 2 + 1, mid + 1, r, pos, nval, idx));
}
int main() {
cin >> n >> m;
bool f1 = 0, f2 = 0;
for (int i = 0; i < 12 * maxn; i++){
sgt[0][i] = sgt[1][i] = 1e15;
}
for (int i = 0; i < n; i++){
cin >> a[i] >> b[i] >> c[i] >> cost[i];
if (a[i] == 1) f1 = 1;
if (b[i] == m) f2 = 1;
coord.push_back(a[i]);
coord.push_back(b[i]);
coord.push_back(c[i]);
}
if (!f1 || !f2){
cout << -1;
return 0;
}
sort(coord.begin(), coord.end());
coord.erase(unique(coord.begin(), coord.end()), coord.end());
m = 0;
for (int i = 0; i < n; i++){
a[i] = lower_bound(coord.begin(), coord.end(), a[i]) - coord.begin();
b[i] = lower_bound(coord.begin(), coord.end(), b[i]) - coord.begin();
c[i] = lower_bound(coord.begin(), coord.end(), c[i]) - coord.begin();
m = max(m, b[i]);
}
int64 rs = 1e15;
update(1, 0, maxn, 0, 0, 0);
update(1, 0, maxn, m, 0, 1);
for (int i = 0; i < n; i++){
rs = min(rs, query(1, 0, maxn, a[i], b[i], 0) + query(1, 0, maxn, a[i], b[i], 1) + cost[i]);
int64 vafle = query(1, 0, maxn, a[i], b[i], 0) + cost[i];
update(1, 0, maxn, c[i], vafle, 0);
vafle = query(1, 0, maxn, a[i], b[i], 1) + cost[i];
update(1, 0, maxn, c[i], vafle, 1);
}
cout << (rs >= 1e15 ? -1: rs);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19052 KB |
Output is correct |
3 |
Correct |
8 ms |
19064 KB |
Output is correct |
4 |
Correct |
8 ms |
19028 KB |
Output is correct |
5 |
Correct |
8 ms |
19028 KB |
Output is correct |
6 |
Correct |
9 ms |
19028 KB |
Output is correct |
7 |
Correct |
9 ms |
18984 KB |
Output is correct |
8 |
Correct |
9 ms |
19096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19052 KB |
Output is correct |
3 |
Correct |
8 ms |
19064 KB |
Output is correct |
4 |
Correct |
8 ms |
19028 KB |
Output is correct |
5 |
Correct |
8 ms |
19028 KB |
Output is correct |
6 |
Correct |
9 ms |
19028 KB |
Output is correct |
7 |
Correct |
9 ms |
18984 KB |
Output is correct |
8 |
Correct |
9 ms |
19096 KB |
Output is correct |
9 |
Correct |
9 ms |
19064 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19092 KB |
Output is correct |
13 |
Correct |
10 ms |
19056 KB |
Output is correct |
14 |
Correct |
9 ms |
19128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19052 KB |
Output is correct |
3 |
Correct |
8 ms |
19064 KB |
Output is correct |
4 |
Correct |
8 ms |
19028 KB |
Output is correct |
5 |
Correct |
8 ms |
19028 KB |
Output is correct |
6 |
Correct |
9 ms |
19028 KB |
Output is correct |
7 |
Correct |
9 ms |
18984 KB |
Output is correct |
8 |
Correct |
9 ms |
19096 KB |
Output is correct |
9 |
Correct |
9 ms |
19064 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19092 KB |
Output is correct |
13 |
Correct |
10 ms |
19056 KB |
Output is correct |
14 |
Correct |
9 ms |
19128 KB |
Output is correct |
15 |
Correct |
9 ms |
19028 KB |
Output is correct |
16 |
Correct |
9 ms |
19096 KB |
Output is correct |
17 |
Correct |
12 ms |
19112 KB |
Output is correct |
18 |
Correct |
14 ms |
19088 KB |
Output is correct |
19 |
Correct |
13 ms |
19176 KB |
Output is correct |
20 |
Correct |
12 ms |
19192 KB |
Output is correct |
21 |
Correct |
11 ms |
19152 KB |
Output is correct |
22 |
Correct |
12 ms |
19156 KB |
Output is correct |
23 |
Correct |
13 ms |
19148 KB |
Output is correct |
24 |
Correct |
11 ms |
19152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19052 KB |
Output is correct |
3 |
Correct |
8 ms |
19064 KB |
Output is correct |
4 |
Correct |
8 ms |
19028 KB |
Output is correct |
5 |
Correct |
8 ms |
19028 KB |
Output is correct |
6 |
Correct |
9 ms |
19028 KB |
Output is correct |
7 |
Correct |
9 ms |
18984 KB |
Output is correct |
8 |
Correct |
9 ms |
19096 KB |
Output is correct |
9 |
Correct |
9 ms |
19064 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19092 KB |
Output is correct |
13 |
Correct |
10 ms |
19056 KB |
Output is correct |
14 |
Correct |
9 ms |
19128 KB |
Output is correct |
15 |
Correct |
9 ms |
19028 KB |
Output is correct |
16 |
Correct |
9 ms |
19096 KB |
Output is correct |
17 |
Correct |
12 ms |
19112 KB |
Output is correct |
18 |
Correct |
14 ms |
19088 KB |
Output is correct |
19 |
Correct |
13 ms |
19176 KB |
Output is correct |
20 |
Correct |
12 ms |
19192 KB |
Output is correct |
21 |
Correct |
11 ms |
19152 KB |
Output is correct |
22 |
Correct |
12 ms |
19156 KB |
Output is correct |
23 |
Correct |
13 ms |
19148 KB |
Output is correct |
24 |
Correct |
11 ms |
19152 KB |
Output is correct |
25 |
Correct |
38 ms |
19732 KB |
Output is correct |
26 |
Correct |
90 ms |
20916 KB |
Output is correct |
27 |
Incorrect |
244 ms |
23000 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |