#include "bits/stdc++.h"
#define int long long
using namespace std;
const long long INF = 1ll << 60;
const int N = 5e5 + 15;
vector<int> X;
int A[N], B[N], C[N], D[N], m, n;
struct seg {
int mn[4 * N];
void init() {
memset(mn, 0x3f3f3f, sizeof mn);
}
void update(int v, int l, int r, int ind, int I) {
if (l + 1 == r) return void(mn[v] = min(mn[v], I));
int m = l + r >> 1;
if (ind < m)
update(v << 1, l, m, ind, I);
else
update(v << 1 | 1, m, r, ind, I);
mn[v] = min(mn[v << 1], mn[v << 1 | 1]);
}
int get(int v, int l, int r, int L, int R) {
if (r <= L || R <= l) return INF;
if (L <= l && r <= R) return mn[v];
int m = l + r >> 1;
return min(get(v << 1, l, m, L, R), get(v << 1 | 1, m, r, L, R));
}
inline int g(int L, int R) {
return get(1, 0, X.size(), L, R);
}
inline void u(int ind, int I) {
update(1, 0, X.size(), ind, I);
}
} S[2];
void solve() {
int ans = INF;
S[0].u(0, 0);
S[1].u(X.size() - 1, 0);
for (int i = 0; i < m; i++) {
//cout << A[i] << ' ' << B[i] << endl;
ans = min(ans, S[0].g(A[i], B[i] + 1) + S[1].g(A[i], B[i] + 1) + D[i]);
S[0].u(C[i], S[0].g(A[i], C[i] + 1) + D[i]);
S[1].u(C[i], S[1].g(C[i], B[i] + 1) + D[i]);
}
cout << (ans == INF ? -1 : ans) << '\n';
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> m >> n;
X.push_back(1);
X.push_back(n);
for (int i = 0; i < m; i++) {
cin >> A[i] >> B[i] >> C[i] >> D[i];
X.push_back(A[i]), X.push_back(B[i]), X.push_back(C[i]), X.push_back(min(n, B[i] + 1)), X.push_back(max(1ll, A[i] - 1));
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
for (int i = 0; i < m; i++) {
A[i] = lower_bound(X.begin(), X.end(), A[i]) - X.begin();
B[i] = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
C[i] = lower_bound(X.begin(), X.end(), C[i]) - X.begin();
}
S[0].init(), S[1].init();
solve();
return 0;
}
Compilation message
pinball.cpp: In member function 'void seg::update(long long int, long long int, long long int, long long int, long long int)':
pinball.cpp:16:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | int m = l + r >> 1;
| ~~^~~
pinball.cpp: In member function 'long long int seg::get(long long int, long long int, long long int, long long int, long long int)':
pinball.cpp:26:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31644 KB |
Output is correct |
2 |
Correct |
13 ms |
31568 KB |
Output is correct |
3 |
Correct |
13 ms |
31652 KB |
Output is correct |
4 |
Correct |
14 ms |
31628 KB |
Output is correct |
5 |
Correct |
13 ms |
31552 KB |
Output is correct |
6 |
Correct |
18 ms |
31688 KB |
Output is correct |
7 |
Correct |
15 ms |
31568 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31644 KB |
Output is correct |
2 |
Correct |
13 ms |
31568 KB |
Output is correct |
3 |
Correct |
13 ms |
31652 KB |
Output is correct |
4 |
Correct |
14 ms |
31628 KB |
Output is correct |
5 |
Correct |
13 ms |
31552 KB |
Output is correct |
6 |
Correct |
18 ms |
31688 KB |
Output is correct |
7 |
Correct |
15 ms |
31568 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
9 |
Correct |
14 ms |
31700 KB |
Output is correct |
10 |
Correct |
15 ms |
31700 KB |
Output is correct |
11 |
Correct |
17 ms |
31700 KB |
Output is correct |
12 |
Correct |
14 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
31612 KB |
Output is correct |
14 |
Correct |
15 ms |
31700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31644 KB |
Output is correct |
2 |
Correct |
13 ms |
31568 KB |
Output is correct |
3 |
Correct |
13 ms |
31652 KB |
Output is correct |
4 |
Correct |
14 ms |
31628 KB |
Output is correct |
5 |
Correct |
13 ms |
31552 KB |
Output is correct |
6 |
Correct |
18 ms |
31688 KB |
Output is correct |
7 |
Correct |
15 ms |
31568 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
9 |
Correct |
14 ms |
31700 KB |
Output is correct |
10 |
Correct |
15 ms |
31700 KB |
Output is correct |
11 |
Correct |
17 ms |
31700 KB |
Output is correct |
12 |
Correct |
14 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
31612 KB |
Output is correct |
14 |
Correct |
15 ms |
31700 KB |
Output is correct |
15 |
Correct |
13 ms |
31572 KB |
Output is correct |
16 |
Correct |
14 ms |
31676 KB |
Output is correct |
17 |
Correct |
16 ms |
31808 KB |
Output is correct |
18 |
Correct |
17 ms |
31828 KB |
Output is correct |
19 |
Correct |
17 ms |
31736 KB |
Output is correct |
20 |
Correct |
16 ms |
31828 KB |
Output is correct |
21 |
Correct |
14 ms |
31708 KB |
Output is correct |
22 |
Correct |
15 ms |
31796 KB |
Output is correct |
23 |
Correct |
15 ms |
31828 KB |
Output is correct |
24 |
Correct |
16 ms |
31828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31644 KB |
Output is correct |
2 |
Correct |
13 ms |
31568 KB |
Output is correct |
3 |
Correct |
13 ms |
31652 KB |
Output is correct |
4 |
Correct |
14 ms |
31628 KB |
Output is correct |
5 |
Correct |
13 ms |
31552 KB |
Output is correct |
6 |
Correct |
18 ms |
31688 KB |
Output is correct |
7 |
Correct |
15 ms |
31568 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
9 |
Correct |
14 ms |
31700 KB |
Output is correct |
10 |
Correct |
15 ms |
31700 KB |
Output is correct |
11 |
Correct |
17 ms |
31700 KB |
Output is correct |
12 |
Correct |
14 ms |
31700 KB |
Output is correct |
13 |
Correct |
15 ms |
31612 KB |
Output is correct |
14 |
Correct |
15 ms |
31700 KB |
Output is correct |
15 |
Correct |
13 ms |
31572 KB |
Output is correct |
16 |
Correct |
14 ms |
31676 KB |
Output is correct |
17 |
Correct |
16 ms |
31808 KB |
Output is correct |
18 |
Correct |
17 ms |
31828 KB |
Output is correct |
19 |
Correct |
17 ms |
31736 KB |
Output is correct |
20 |
Correct |
16 ms |
31828 KB |
Output is correct |
21 |
Correct |
14 ms |
31708 KB |
Output is correct |
22 |
Correct |
15 ms |
31796 KB |
Output is correct |
23 |
Correct |
15 ms |
31828 KB |
Output is correct |
24 |
Correct |
16 ms |
31828 KB |
Output is correct |
25 |
Correct |
35 ms |
32720 KB |
Output is correct |
26 |
Correct |
86 ms |
34372 KB |
Output is correct |
27 |
Correct |
222 ms |
39220 KB |
Output is correct |
28 |
Correct |
173 ms |
42564 KB |
Output is correct |
29 |
Correct |
171 ms |
37244 KB |
Output is correct |
30 |
Correct |
227 ms |
42692 KB |
Output is correct |
31 |
Correct |
335 ms |
42668 KB |
Output is correct |
32 |
Correct |
343 ms |
42684 KB |
Output is correct |
33 |
Correct |
51 ms |
33804 KB |
Output is correct |
34 |
Correct |
155 ms |
37168 KB |
Output is correct |
35 |
Correct |
186 ms |
42660 KB |
Output is correct |
36 |
Correct |
326 ms |
42656 KB |
Output is correct |
37 |
Correct |
297 ms |
42700 KB |
Output is correct |
38 |
Correct |
288 ms |
42592 KB |
Output is correct |