#include <bits/stdc++.h>
using namespace std;
const int DIM = 300005;
struct Segment {
int lef, rig, pos, val; } seg[DIM];
pair<long long, long long> sgt[DIM << 2]; vector<int> lst;
void update(int tp, int nd, int le, int ri, int ps, long long vl) {
if (le == ri) {
!tp ? (sgt[nd].first = min(sgt[nd].first, vl)) :
(sgt[nd].second = min(sgt[nd].second, vl)); }
else {
int md = (le + ri) / 2;
(ps <= md) ? update(tp, nd << 1, le, md, ps, vl) :
update(tp, nd << 1 | 1, md + 1, ri, ps, vl);
sgt[nd] = make_pair(min(sgt[nd << 1].first, sgt[nd << 1 | 1].first),
min(sgt[nd << 1].second, sgt[nd << 1 | 1].second)); } }
pair<long long, long long> query(int nd, int le, int ri, int st, int en) {
if (st <= le and ri <= en) {
return sgt[nd]; }
else {
int md = (le + ri) / 2;
if (en <= md) {
return query(nd << 1, le, md, st, en); }
if (md < st) {
return query(nd << 1 | 1, md + 1, ri, st, en); }
auto lans = query(nd << 1, le, md, st, en),
rans = query(nd << 1 | 1, md + 1, ri, st, en);
return make_pair(min(lans.first, rans.first),
min(lans.second, rans.second)); } }
inline int f(const int x) {
return lower_bound(lst.begin(), lst.end(), x) - lst.begin() + 1; }
int main(void) {
#ifdef HOME
freopen("pinball.in", "r", stdin);
freopen("pinball.out", "w", stdout);
#endif
int n, m; cin >> n >> m;
for (int i = 1; i <= n; ++i) {
int l, r, p, d; cin >> l >> r >> p >> d; seg[i] = {l, r, p, d};
lst.push_back(l); lst.push_back(r); lst.push_back(p); }
lst.push_back(1); lst.push_back(m); sort(lst.begin(), lst.end());
lst.resize(unique(lst.begin(), lst.end()) - lst.begin());
int k = (int) lst.size();
for (int i = 1; i <= (k << 2); ++i) {
sgt[i].first = sgt[i].second = (1LL << 60); }
update(0, 1, 1, k, 1, 0); update(1, 1, 1, k, k, 0);
long long ans = (1LL << 60);
for (int i = 1; i <= n; ++i) {
int l = f(seg[i].lef), r = f(seg[i].rig), p = f(seg[i].pos), d = seg[i].val;
pair<long long, long long> pr = query(1, 1, k, l, r);
ans = min(ans, pr.first + pr.second + d);
update(0, 1, 1, k, p, pr.first + d); update(1, 1, 1, k, p, pr.second + d); }
cout << (ans <= 1000000000000000LL ? ans : -1) << endl;
return 0; }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
840 KB |
Output is correct |
18 |
Correct |
5 ms |
840 KB |
Output is correct |
19 |
Correct |
5 ms |
940 KB |
Output is correct |
20 |
Correct |
5 ms |
976 KB |
Output is correct |
21 |
Correct |
4 ms |
1016 KB |
Output is correct |
22 |
Correct |
5 ms |
1160 KB |
Output is correct |
23 |
Correct |
5 ms |
1200 KB |
Output is correct |
24 |
Correct |
5 ms |
1240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
372 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
536 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
584 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
3 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
636 KB |
Output is correct |
16 |
Correct |
3 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
840 KB |
Output is correct |
18 |
Correct |
5 ms |
840 KB |
Output is correct |
19 |
Correct |
5 ms |
940 KB |
Output is correct |
20 |
Correct |
5 ms |
976 KB |
Output is correct |
21 |
Correct |
4 ms |
1016 KB |
Output is correct |
22 |
Correct |
5 ms |
1160 KB |
Output is correct |
23 |
Correct |
5 ms |
1200 KB |
Output is correct |
24 |
Correct |
5 ms |
1240 KB |
Output is correct |
25 |
Correct |
31 ms |
2816 KB |
Output is correct |
26 |
Correct |
85 ms |
6552 KB |
Output is correct |
27 |
Correct |
250 ms |
13152 KB |
Output is correct |
28 |
Correct |
274 ms |
13152 KB |
Output is correct |
29 |
Correct |
187 ms |
17272 KB |
Output is correct |
30 |
Correct |
320 ms |
17272 KB |
Output is correct |
31 |
Correct |
395 ms |
23304 KB |
Output is correct |
32 |
Correct |
379 ms |
23304 KB |
Output is correct |
33 |
Correct |
60 ms |
23304 KB |
Output is correct |
34 |
Correct |
189 ms |
23304 KB |
Output is correct |
35 |
Correct |
354 ms |
31732 KB |
Output is correct |
36 |
Correct |
417 ms |
31732 KB |
Output is correct |
37 |
Correct |
374 ms |
31772 KB |
Output is correct |
38 |
Correct |
356 ms |
31772 KB |
Output is correct |