#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 3e5+3;
int m, n;
const ll inf = 1LL << 60;
ll dp1[M], dp2[M], a[M], b[M], c[M], d[M];
struct SegTree{
ll t[M * 4], tag[M * 4];
void pushdown(int u, int v){
if(tag[u] != -1){
t[v] = tag[v] = tag[u];
}
}
void push(int v){
if(tag[v] != -1){
pushdown(v, v * 2);
pushdown(v, v * 2 + 1);
}
tag[v] = -1;
}
void build(ll* val, int v = 1, int tl = 0, int tr = n - 1){
if(tl == tr){
t[v] = val[tl]; tag[v] = -1;
return;
}
int tm = (tl + tr) / 2;
build(val, v * 2, tl, tm);
build(val, v * 2 + 1, tm + 1, tr);
t[v] = min(t[v * 2], t[v * 2 + 1]);
tag[v] = -1;
}
ll qry_min(int l, int r, int v = 1, int tl = 0, int tr = n - 1){
if(l > r) return inf;
if(l <= tl && tr <= r) return t[v];
push(v);
int tm = (tl + tr) / 2;
return min(qry_min(l, min(tm, r), v * 2, tl, tm),
qry_min(max(tm + 1, l), r, v * 2 + 1, tm + 1, tr));
}
void upd(int l, int r, ll val, int v = 1, int tl = 0, int tr = n - 1){
if(l > r) return;
if(l <= tl && tr <= r){
t[v] = tag[v] = val;
return;
}
push(v);
int tm = (tl + tr) / 2;
upd(l, min(tm, r), val, v * 2, tl, tm);
upd(max(tm + 1, l), r, val, v * 2 + 1, tm + 1, tr);
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
} seg1, seg2;
int main(){
cin >> m >> n;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
{
set<int> S; for(int i = 0; i < m; i++) S.insert(a[i]), S.insert(b[i]), S.insert(c[i]);
map<int, int> M; int idx = 0; for(auto i : S) M[i] = idx++;
if(*prev(S.end()) != n || *S.begin() != 1){
cout << -1 << endl;
return 0;
}
n = idx;
for(int i = 0; i < m; i++) a[i] = M[a[i]], b[i] = M[b[i]], c[i] = M[c[i]];
}
// cout << n << endl;
// for(int i = 0; i < m; i++){
// cout << a[i] << ' ' << b[i] << ' ' << c[i] << endl;
// }
fill(dp1 + 1, dp1 + n, inf);
fill(dp2, dp2 + n - 1, inf);
seg1.build(dp1);
seg2.build(dp2);
ll ans = inf;
for(int i = 0; i < m; i++){
ans = min(ans, seg1.qry_min(a[i], n - 1) + seg2.qry_min(0, b[i]) + d[i]);
{
ll mn = seg1.qry_min(a[i], c[i]);
seg1.upd(c[i], c[i], min(seg1.qry_min(c[i], c[i]), mn + d[i]));
}
{
ll mn = seg2.qry_min(c[i], b[i]);
seg2.upd(c[i], c[i], min(seg2.qry_min(c[i], c[i]), mn + d[i]));
}
// for(int i = 0; i < n; i++){
// cout << seg1.qry_min(i, i) << ' ';
// }
// cout << endl;
// for(int i = 0; i < n; i++){
// cout << seg2.qry_min(i, i) << ' ';
// }
// cout << endl;
}
cout << (ans == inf ? -1 : ans) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
704 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
580 KB |
Output is correct |
20 |
Correct |
4 ms |
596 KB |
Output is correct |
21 |
Correct |
3 ms |
596 KB |
Output is correct |
22 |
Correct |
4 ms |
1028 KB |
Output is correct |
23 |
Correct |
4 ms |
980 KB |
Output is correct |
24 |
Correct |
5 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
440 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
4 ms |
704 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
580 KB |
Output is correct |
20 |
Correct |
4 ms |
596 KB |
Output is correct |
21 |
Correct |
3 ms |
596 KB |
Output is correct |
22 |
Correct |
4 ms |
1028 KB |
Output is correct |
23 |
Correct |
4 ms |
980 KB |
Output is correct |
24 |
Correct |
5 ms |
980 KB |
Output is correct |
25 |
Correct |
44 ms |
4792 KB |
Output is correct |
26 |
Correct |
133 ms |
12240 KB |
Output is correct |
27 |
Correct |
417 ms |
24504 KB |
Output is correct |
28 |
Correct |
292 ms |
7560 KB |
Output is correct |
29 |
Correct |
268 ms |
21948 KB |
Output is correct |
30 |
Correct |
385 ms |
11444 KB |
Output is correct |
31 |
Correct |
616 ms |
42244 KB |
Output is correct |
32 |
Correct |
546 ms |
28828 KB |
Output is correct |
33 |
Correct |
86 ms |
12100 KB |
Output is correct |
34 |
Correct |
308 ms |
36732 KB |
Output is correct |
35 |
Correct |
491 ms |
72928 KB |
Output is correct |
36 |
Correct |
819 ms |
73088 KB |
Output is correct |
37 |
Correct |
690 ms |
73024 KB |
Output is correct |
38 |
Correct |
653 ms |
73036 KB |
Output is correct |