#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
int n, m;
struct Sparse{
vector<long long>t{(long long)1e16};
vector<long long>lazy{(long long)1e16};
vector<int>L{-1},R{-1};
inline void expand(int k, int l, int r){
if (l != r){
if (L[k] == -1){
L.push_back(-1);
R.push_back(-1);
t.push_back((long long)1e16);
lazy.push_back((long long)1e16);
L[k] = (int)t.size() - 1;
}
if (R[k] == -1){
L.push_back(-1);
R.push_back(-1);
t.push_back((long long)1e16);
R[k] = (int)t.size() - 1;
lazy.push_back((long long)1e16);
}
}
}
inline void propagate(int k, int l, int r){
t[k] = min(t[k], lazy[k]);
if (l != r){
lazy[L[k]] = min(lazy[k], lazy[L[k]]);
lazy[R[k]] = min(lazy[R[k]], lazy[k]);
}
lazy[k] = (long long)1e16;
}
inline void range_update(int a, int b, long long cost, int l = 1, int r = n, int k = 0){
if (a > b)return;
expand(k,l,r);
propagate(k,l,r);
//cout << k << " " << L[k] << " " << R[k] << endl;
if (l >= a && r <= b){
lazy[k] = cost;
propagate(k,l,r);
return;
}
if (l > b || r < a)return;
int mid = (l+r) / 2;
range_update(a,b,cost,l,mid,L[k]);
range_update(a,b,cost,mid+1,r,R[k]);
}
inline long long query(int a, int l = 1, int r = n, int k = 0){
if (a < 1)return 0LL;
if (a > n)return 0LL;
expand(k,l,r);
propagate(k,l,r);
if (l == r){
return t[k];
}
int mid = (l+r) >> 1;
if (a <= mid)
return query(a,l,mid,L[k]);
else
return query(a,mid+1,r,R[k]);
}
} st, st2;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
long long ans = (long long)1e16;
for (int i = 0; i < m; i++){
long long a,b,c,d;
cin >> a >> b >> c >> d;
//it ends here
long long CostLeft = st.query(a-1);
long long CostRight = st2.query(b+1);
ans = min(ans, CostLeft + CostRight + d);
// cout << CostLeft << " " << CostRight << " " << c + 1 << endl;
long long CoverLeft = CostLeft + d;
long long CoverRight = CostRight + d;
if (CoverLeft < (long long)1e16)
st.range_update(a,c-1,CoverLeft);
if (CoverRight < (long long)1e16)
st2.range_update(c+1,b, CoverRight);
}
cout << (ans == (long long)1e16 ? -1 : ans) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
5 ms |
3612 KB |
Output is correct |
18 |
Correct |
4 ms |
480 KB |
Output is correct |
19 |
Correct |
5 ms |
4872 KB |
Output is correct |
20 |
Correct |
5 ms |
2876 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
5 ms |
3632 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
6 ms |
4468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
1 ms |
1468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
5 ms |
3612 KB |
Output is correct |
18 |
Correct |
4 ms |
480 KB |
Output is correct |
19 |
Correct |
5 ms |
4872 KB |
Output is correct |
20 |
Correct |
5 ms |
2876 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
5 ms |
3632 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
6 ms |
4468 KB |
Output is correct |
25 |
Correct |
32 ms |
12208 KB |
Output is correct |
26 |
Correct |
155 ms |
68284 KB |
Output is correct |
27 |
Correct |
471 ms |
148788 KB |
Output is correct |
28 |
Correct |
268 ms |
2848 KB |
Output is correct |
29 |
Correct |
342 ms |
134696 KB |
Output is correct |
30 |
Correct |
484 ms |
36408 KB |
Output is correct |
31 |
Correct |
940 ms |
263312 KB |
Output is correct |
32 |
Correct |
739 ms |
194072 KB |
Output is correct |
33 |
Correct |
52 ms |
36508 KB |
Output is correct |
34 |
Correct |
206 ms |
104040 KB |
Output is correct |
35 |
Correct |
378 ms |
257312 KB |
Output is correct |
36 |
Correct |
684 ms |
339084 KB |
Output is correct |
37 |
Correct |
453 ms |
215948 KB |
Output is correct |
38 |
Correct |
508 ms |
225468 KB |
Output is correct |