#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll INF = 1e18;
int n,m;
struct ST{
struct Node{
int l = -1 , r = -1;
ll val = INF;
};
vector<Node> T = vector<Node>(1);
void extend(int &pos){
if(pos != -1) return;
pos = T.size();
T.emplace_back();
}
void W(int pos){
T[pos].val = INF;
int l = T[pos].l , r = T[pos].r;
if(l != -1) T[pos].val = min(T[pos].val , T[l].val);
if(r != -1) T[pos].val = min(T[pos].val , T[r].val);
}
void upd(int idx , ll v , int l = 0 , int r = m - 1 , int pos = 0){
if(idx < l || idx > r) return;
if(l == r){
T[pos].val = min(T[pos].val , v);
return;
}
int mid = (l + r) >> 1;
if(idx <= mid){
extend(T[pos].l);
upd(idx , v , l , mid , T[pos].l);
} else {
extend(T[pos].r);
upd(idx , v , mid + 1 , r , T[pos].r);
}
W(pos);
}
ll get(int L , int R , int l = 0, int r= m - 1 , int pos = 0){
if(r < L || R < l || pos == -1) return INF;
if(L <= l && r <= R) return T[pos].val;
int mid = (l + r) >> 1;
return min(get(L , R , l , mid , T[pos].l) , get(L , R , mid + 1 , r , T[pos].r) );
}
}L , R;
int main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
vector<int> A(n) , B(n) , C(n) , D(n);
for(int i = 0; i < n; ++i) {cin >> A[i] >> B[i] >> C[i] >> D[i]; --A[i]; --B[i]; --C[i];}
L.upd(0 , 0); R.upd(m - 1 , 0);
ll ans = INF;
for(int i = 0; i < n; ++i){
int l = A[i] , r = B[i] , center = C[i] , cost = D[i];
ll left = L.get(l , r) , right = R.get(l , r);
ans = min(ans, left + right + cost);
L.upd(center , left + cost); R.upd(center , right + cost);
}
if(ans == INF) ans = -1;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 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 |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 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 |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
1020 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
4 ms |
1404 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
1340 KB |
Output is correct |
23 |
Correct |
4 ms |
1404 KB |
Output is correct |
24 |
Correct |
3 ms |
1404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 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 |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
1020 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
4 ms |
1404 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
1340 KB |
Output is correct |
23 |
Correct |
4 ms |
1404 KB |
Output is correct |
24 |
Correct |
3 ms |
1404 KB |
Output is correct |
25 |
Correct |
22 ms |
2788 KB |
Output is correct |
26 |
Correct |
78 ms |
9368 KB |
Output is correct |
27 |
Correct |
285 ms |
28856 KB |
Output is correct |
28 |
Correct |
295 ms |
6388 KB |
Output is correct |
29 |
Correct |
210 ms |
27960 KB |
Output is correct |
30 |
Correct |
349 ms |
12124 KB |
Output is correct |
31 |
Correct |
466 ms |
38672 KB |
Output is correct |
32 |
Correct |
471 ms |
32272 KB |
Output is correct |
33 |
Correct |
47 ms |
7876 KB |
Output is correct |
34 |
Correct |
190 ms |
27768 KB |
Output is correct |
35 |
Correct |
280 ms |
55308 KB |
Output is correct |
36 |
Correct |
443 ms |
55300 KB |
Output is correct |
37 |
Correct |
387 ms |
55300 KB |
Output is correct |
38 |
Correct |
361 ms |
55308 KB |
Output is correct |