#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const ll linf = 1e18;
struct Segtree {
ll st[12 * N];
void Reset() {
for (int i = 1; i < 12 * N; i++) st[i] = linf;
}
void Update(int node, int l, int r, int x, ll y) {
smin(st[node], y);
if (l == r) return;
int mid = l + r >> 1;
if (x <= mid) Update(2 * node, l, mid, x, y);
else Update(2 * node + 1, mid + 1, r, x, y);
}
ll Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l) return linf;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return min(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
}st;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), c(n), d(n);
vector<int> all;
all.push_back(1); all.push_back(m);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
all.push_back(a[i]);
all.push_back(b[i]);
all.push_back(c[i]);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
int sz = all.size();
map<int, int> mp;
for (int i = 0; i < all.size(); i++) mp[all[i]] = i;
for (int i = 0; i < n; i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
c[i] = mp[c[i]];
}
vector<ll> dp1(n, linf), dp2(n, linf);
st.Reset();
for (int i = 0; i < n; i++) {
if (a[i] == 0) dp1[i] = d[i];
else smin(dp1[i], st.Get(1, 0, sz - 1, a[i], b[i]) + d[i]);
st.Update(1, 0, sz - 1, c[i], dp1[i]);
}
st.Reset();
for (int i = 0; i < n; i++) {
if (b[i] == sz - 1) dp2[i] = d[i];
else smin(dp2[i], st.Get(1, 0, sz - 1, a[i], b[i]) + d[i]);
st.Update(1, 0, sz - 1, c[i], dp2[i]);
}
ll ans = linf;
for (int i = 0; i < n; i++) smin(ans, dp1[i] + dp2[i] - d[i]);
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
Compilation message
pinball.cpp: In member function 'void Segtree::Update(int, int, int, int, long long int)':
pinball.cpp:19:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In member function 'long long int Segtree::Get(int, int, int, int, int)':
pinball.cpp:26:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In function 'int main()':
pinball.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < all.size(); i++) mp[all[i]] = i;
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9688 KB |
Output is correct |
5 |
Correct |
5 ms |
9596 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9688 KB |
Output is correct |
5 |
Correct |
5 ms |
9596 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9628 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9672 KB |
Output is correct |
12 |
Correct |
6 ms |
9672 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9688 KB |
Output is correct |
5 |
Correct |
5 ms |
9596 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9628 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9672 KB |
Output is correct |
12 |
Correct |
6 ms |
9672 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9764 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
7 ms |
9812 KB |
Output is correct |
20 |
Correct |
8 ms |
9736 KB |
Output is correct |
21 |
Correct |
9 ms |
9812 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9860 KB |
Output is correct |
24 |
Correct |
7 ms |
9960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9688 KB |
Output is correct |
5 |
Correct |
5 ms |
9596 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9628 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9672 KB |
Output is correct |
12 |
Correct |
6 ms |
9672 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9764 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
7 ms |
9812 KB |
Output is correct |
20 |
Correct |
8 ms |
9736 KB |
Output is correct |
21 |
Correct |
9 ms |
9812 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9860 KB |
Output is correct |
24 |
Correct |
7 ms |
9960 KB |
Output is correct |
25 |
Correct |
24 ms |
11220 KB |
Output is correct |
26 |
Correct |
65 ms |
14276 KB |
Output is correct |
27 |
Correct |
173 ms |
20024 KB |
Output is correct |
28 |
Correct |
127 ms |
17832 KB |
Output is correct |
29 |
Correct |
124 ms |
17992 KB |
Output is correct |
30 |
Correct |
166 ms |
18684 KB |
Output is correct |
31 |
Correct |
278 ms |
25720 KB |
Output is correct |
32 |
Correct |
249 ms |
23508 KB |
Output is correct |
33 |
Correct |
41 ms |
13908 KB |
Output is correct |
34 |
Correct |
149 ms |
20808 KB |
Output is correct |
35 |
Correct |
199 ms |
31948 KB |
Output is correct |
36 |
Correct |
359 ms |
31940 KB |
Output is correct |
37 |
Correct |
298 ms |
31904 KB |
Output is correct |
38 |
Correct |
285 ms |
31888 KB |
Output is correct |