#include <bits/stdc++.h>
using namespace std;
class segtree_t {
public:
segtree_t *left, *right;
int l, r, m;
int64_t val;
segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(1e18) {
if (l == r) return;
left = new segtree_t(l, m);
right = new segtree_t(m + 1, r);
}
void Update(int p, int64_t x) {
if (l == r) {
val = min(val, x);
return;
}
if (p <= m) {
left->Update(p, x);
} else {
right->Update(p, x);
}
val = min(left->val, right->val);
}
int64_t Get(int s, int t) {
if (r < s or l > t) return 1e18;
if (s <= l && r <= t) return val;
return min(left->Get(s, t), right->Get(s, t));
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> m >> n;
vector<int> a(m), b(m), c(m), d(m);
for (int i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
vector<int> points({1, n});
for (int i = 0; i < m; i++) points.emplace_back(a[i]);
for (int i = 0; i < m; i++) points.emplace_back(b[i]);
for (int i = 0; i < m; i++) points.emplace_back(c[i]);
sort(points.begin(), points.end());
points.resize(unique(points.begin(), points.end()) - points.begin());
const int N = points.size();
for (int i = 0; i < m; i++) a[i] = lower_bound(points.begin(), points.end(), a[i]) - points.begin() + 1;
for (int i = 0; i < m; i++) b[i] = lower_bound(points.begin(), points.end(), b[i]) - points.begin() + 1;
for (int i = 0; i < m; i++) c[i] = lower_bound(points.begin(), points.end(), c[i]) - points.begin() + 1;
segtree_t *treeL = new segtree_t(1, N);
segtree_t *treeR = new segtree_t(1, N);
treeL->Update(1, 0);
treeR->Update(N, 0);
int64_t res = 1e18;
for (int i = 0; i < m; i++) {
int64_t x = treeL->Get(a[i], b[i]);
int64_t y = treeR->Get(a[i], b[i]);
treeL->Update(c[i], x + d[i]);
treeR->Update(c[i], y + d[i]);
res = min(res, x + y + d[i]);
}
cout << (res == 1e18 ? -1 : res);
}
Compilation message
pinball.cpp: In constructor 'segtree_t::segtree_t(int, int)':
pinball.cpp:10:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
10 | segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(1e18) {
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 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 |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
2 ms |
896 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
852 KB |
Output is correct |
23 |
Correct |
2 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
844 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 |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
320 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
2 ms |
896 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
852 KB |
Output is correct |
23 |
Correct |
2 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
844 KB |
Output is correct |
25 |
Correct |
18 ms |
4048 KB |
Output is correct |
26 |
Correct |
58 ms |
12276 KB |
Output is correct |
27 |
Correct |
201 ms |
23712 KB |
Output is correct |
28 |
Correct |
110 ms |
7880 KB |
Output is correct |
29 |
Correct |
166 ms |
20764 KB |
Output is correct |
30 |
Correct |
166 ms |
10288 KB |
Output is correct |
31 |
Correct |
369 ms |
38340 KB |
Output is correct |
32 |
Correct |
324 ms |
29488 KB |
Output is correct |
33 |
Correct |
33 ms |
12496 KB |
Output is correct |
34 |
Correct |
192 ms |
31748 KB |
Output is correct |
35 |
Correct |
192 ms |
63056 KB |
Output is correct |
36 |
Correct |
441 ms |
63184 KB |
Output is correct |
37 |
Correct |
329 ms |
63180 KB |
Output is correct |
38 |
Correct |
307 ms |
63172 KB |
Output is correct |