#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
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;
st.range_update(a,c-1,CoverLeft);
st2.range_update(c+1,b, CoverRight);
}
cout << (ans == (long long)1e16 ? -1 : ans) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1816 KB |
Output is correct |
13 |
Correct |
2 ms |
1860 KB |
Output is correct |
14 |
Correct |
2 ms |
1988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1816 KB |
Output is correct |
13 |
Correct |
2 ms |
1860 KB |
Output is correct |
14 |
Correct |
2 ms |
1988 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
1612 KB |
Output is correct |
17 |
Correct |
5 ms |
3748 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
8 ms |
6392 KB |
Output is correct |
20 |
Correct |
5 ms |
2868 KB |
Output is correct |
21 |
Correct |
2 ms |
2120 KB |
Output is correct |
22 |
Correct |
8 ms |
5936 KB |
Output is correct |
23 |
Correct |
8 ms |
8676 KB |
Output is correct |
24 |
Correct |
9 ms |
8876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1816 KB |
Output is correct |
13 |
Correct |
2 ms |
1860 KB |
Output is correct |
14 |
Correct |
2 ms |
1988 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
1612 KB |
Output is correct |
17 |
Correct |
5 ms |
3748 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
8 ms |
6392 KB |
Output is correct |
20 |
Correct |
5 ms |
2868 KB |
Output is correct |
21 |
Correct |
2 ms |
2120 KB |
Output is correct |
22 |
Correct |
8 ms |
5936 KB |
Output is correct |
23 |
Correct |
8 ms |
8676 KB |
Output is correct |
24 |
Correct |
9 ms |
8876 KB |
Output is correct |
25 |
Correct |
31 ms |
12008 KB |
Output is correct |
26 |
Correct |
164 ms |
61228 KB |
Output is correct |
27 |
Correct |
479 ms |
142972 KB |
Output is correct |
28 |
Correct |
255 ms |
6820 KB |
Output is correct |
29 |
Correct |
415 ms |
149868 KB |
Output is correct |
30 |
Correct |
436 ms |
36520 KB |
Output is correct |
31 |
Correct |
887 ms |
266696 KB |
Output is correct |
32 |
Correct |
757 ms |
193476 KB |
Output is correct |
33 |
Correct |
81 ms |
48724 KB |
Output is correct |
34 |
Correct |
412 ms |
205664 KB |
Output is correct |
35 |
Correct |
642 ms |
392088 KB |
Output is correct |
36 |
Execution timed out |
1028 ms |
462984 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |