#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int INF = 1e16;
struct SegmentTree{
vector<int> a; int sz = 1;
SegmentTree(int n){
while(sz < n) sz += sz;
a.assign(2*sz, INF);
}
void update(int i, int v, int x, int lx, int rx){
if(rx-lx==1) return void(a[x] = min(a[x], v));
int mx = (lx + rx) / 2;
if(i < mx) update(i, v, 2*x+1, lx, mx);
else update(i, v, 2*x+2, mx, rx);
a[x] = min(a[2*x+1], a[2*x+2]);
}
void update(int i, int v){ update(i, v, 0, 0, sz); }
int rangeMin(int l, int r, int x, int lx, int rx){
if(r<=lx || rx<=l) return INF;
if(l<=lx && rx<=r) return a[x];
int mx = (lx + rx) / 2;
return min(rangeMin(l, r, 2*x+1, lx, mx), rangeMin(l, r, 2*x+2, mx, rx));
}
int rangeMin(int l, int r){ return rangeMin(l, r+1, 0, 0, sz); }
};
void C(vector<int*> &vals){
int curr = -1, prev = -INF;
sort(vals.begin(), vals.end(), [](int* x, int* y){
return *x < *y;
});
for(int i=0; i<(int)vals.size() ;++i){
if(prev < *vals[i]) ++curr, prev = *vals[i];
*vals[i] = curr;
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int m, n; cin >> m >> n;
array<int, 4> a[m];
int lx = 1, last = n;
vector<int*> vals;
vals.push_back(&lx);
vals.push_back(&last);
for(auto &i : a){
for(int j=0; j<3; ++j) cin >> i[j], vals.push_back(&i[j]);
cin >> i[3];
}
C(vals);
SegmentTree dpL(last+1), dpR(last+1);
dpL.update(0, 0);
dpR.update(last, 0);
int res = INF;
for(auto &i : a){
int l = dpL.rangeMin(i[0], i[1]);
int r = dpR.rangeMin(i[0], i[1]);
res = min(res, l + r + i[3]);
dpL.update(i[2], l + i[3]);
dpR.update(i[2], r + i[3]);
}
cout << (res == INF ? -1 : res);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
17 ms |
2036 KB |
Output is correct |
26 |
Correct |
50 ms |
3868 KB |
Output is correct |
27 |
Correct |
143 ms |
8392 KB |
Output is correct |
28 |
Correct |
135 ms |
7648 KB |
Output is correct |
29 |
Correct |
103 ms |
7164 KB |
Output is correct |
30 |
Correct |
167 ms |
7608 KB |
Output is correct |
31 |
Correct |
223 ms |
14124 KB |
Output is correct |
32 |
Correct |
199 ms |
9776 KB |
Output is correct |
33 |
Correct |
28 ms |
3584 KB |
Output is correct |
34 |
Correct |
99 ms |
11320 KB |
Output is correct |
35 |
Correct |
154 ms |
22320 KB |
Output is correct |
36 |
Correct |
250 ms |
22344 KB |
Output is correct |
37 |
Correct |
217 ms |
22320 KB |
Output is correct |
38 |
Correct |
201 ms |
22336 KB |
Output is correct |