/**
* author: m371
* created: 06.11.2021 22:12:33
**/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAX = 4 * N;
const long long inf = 1e17;
long long st[MAX];
void build(int x, int l, int r) {
st[x] = inf;
if (l == r) return;
int mid = l + r >> 1;
build(x + x, l, mid);
build(x + x + 1, mid + 1, r);
}
void modify(int x, int l, int r, int pos, long long val) {
if (l == r) {
st[x] = min(st[x], val);
return;
}
int mid = l + r >> 1;
if (pos <= mid) {
modify(x + x, l, mid, pos, val);
} else {
modify(x + x + 1, mid + 1, r, pos, val);
}
st[x] = min(st[x + x], st[x + x + 1]);
}
long long get(int x, int l, int r, int ll, int rr) {
if (l > r || l > rr || r < ll) return inf;
if (ll <= l && r <= rr) return st[x];
int mid = l + r >> 1;
return min(get(x + x, l, mid, ll, rr), get(x + x + 1, mid + 1, r, ll, rr));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), c(n), cost(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i] >> cost[i];
}
vector<int> cs;
cs.push_back(1);
cs.push_back(m);
for (int i = 0; i < n; i++) {
cs.push_back(a[i]);
cs.push_back(b[i]);
cs.push_back(c[i]);
}
sort(cs.begin(), cs.end());
cs.erase(unique(cs.begin(), cs.end()), cs.end());
map<int, int> val;
for (int i = 0; i < cs.size(); i++) {
val[cs[i]] = i;
}
build(1, 0, N);
modify(1, 0, N, val[1], 0);
vector<long long> L(n), R(n);
for (int i = 0; i < n; i++) {
L[i] = get(1, 0, N, val[a[i]], val[b[i]]);
modify(1, 0, N, val[c[i]], L[i] + cost[i]);
}
build(1, 0, N);
modify(1, 0, N, val[m], 0);
for (int i = 0; i < n; i++) {
R[i] = get(1, 0, N, val[a[i]], val[b[i]]);
modify(1, 0, N, val[c[i]], R[i] + cost[i]);
}
long long ans = inf;
for (int i = 0; i < n; i++) {
ans = min(ans, L[i] + R[i] + cost[i]);
}
cout << (ans == inf ? -1 : ans) << '\n';
return 0;
}
Compilation message
pinball.cpp: In function 'void build(int, int, int)':
pinball.cpp:19:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In function 'void modify(int, int, int, int, long long int)':
pinball.cpp:29:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In function 'long long int get(int, int, int, int, int)':
pinball.cpp:41:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
pinball.cpp: In function 'int main()':
pinball.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < cs.size(); i++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2252 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2252 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2380 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2252 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2380 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2380 KB |
Output is correct |
15 |
Correct |
2 ms |
2364 KB |
Output is correct |
16 |
Correct |
2 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
2508 KB |
Output is correct |
18 |
Correct |
3 ms |
2380 KB |
Output is correct |
19 |
Correct |
4 ms |
2508 KB |
Output is correct |
20 |
Correct |
4 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
4 ms |
2508 KB |
Output is correct |
23 |
Correct |
5 ms |
2508 KB |
Output is correct |
24 |
Correct |
3 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2252 KB |
Output is correct |
2 |
Correct |
2 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
2 ms |
2252 KB |
Output is correct |
5 |
Correct |
2 ms |
2252 KB |
Output is correct |
6 |
Correct |
2 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2252 KB |
Output is correct |
8 |
Correct |
2 ms |
2252 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
2 ms |
2380 KB |
Output is correct |
12 |
Correct |
2 ms |
2380 KB |
Output is correct |
13 |
Correct |
2 ms |
2380 KB |
Output is correct |
14 |
Correct |
2 ms |
2380 KB |
Output is correct |
15 |
Correct |
2 ms |
2364 KB |
Output is correct |
16 |
Correct |
2 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
2508 KB |
Output is correct |
18 |
Correct |
3 ms |
2380 KB |
Output is correct |
19 |
Correct |
4 ms |
2508 KB |
Output is correct |
20 |
Correct |
4 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
2380 KB |
Output is correct |
22 |
Correct |
4 ms |
2508 KB |
Output is correct |
23 |
Correct |
5 ms |
2508 KB |
Output is correct |
24 |
Correct |
3 ms |
2508 KB |
Output is correct |
25 |
Correct |
26 ms |
3788 KB |
Output is correct |
26 |
Correct |
98 ms |
6956 KB |
Output is correct |
27 |
Incorrect |
242 ms |
12676 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |