#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define bl " "
#define endl "\n"
#define all(v) v.begin(), v.end()
#define comp(v) v.erase(unique(all(v)), v.end())
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, pi> pii;
typedef pair<ll, ll> pll;
const int MAXN = 100100;
const int MOD = 1e9+7;
struct Wall {
int a, b, c, d;
}wall[MAXN];
vector<pi> cp;
vector<ll> tree1, tree2;
void update(vector<ll>& tree, int node, int s, int e, int idx, ll v) {
if(e < idx || idx < s) return;
if(s == e) {
tree[node] = min(tree[node], v);
return;
}
int mid = s+e>>1;
update(tree, node<<1, s, mid, idx, v); update(tree, node<<1|1, mid+1, e, idx, v);
tree[node] = min(tree[node<<1], tree[node<<1|1]);
}
ll solve(vector<ll>& tree, int node, int s, int e, int l, int r) {
if(e < l || r < s) return LLONG_MAX;
if(l <= s && e <= r) return tree[node];
int mid = s+e>>1;
return min(solve(tree, node<<1, s, mid, l, r), solve(tree, node<<1|1, mid+1, e, l, r));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int m, n; cin >> m >> n;
for(int i = 1; i <= m; i++) cin >> wall[i].a >> wall[i].b >> wall[i].c >> wall[i].d;
for(int i = 1; i <= m; i++) cp.push_back(pi(wall[i].c, i));
cp.push_back(pi(1, 0)); cp.push_back(pi(n, 0));
sort(all(cp)); comp(cp);
int sz = cp.size();
for(int i = 1; i <= m; i++) {
wall[i].a = lower_bound(all(cp), pi(wall[i].a, 0))-cp.begin();
wall[i].b = lower_bound(all(cp), pi(wall[i].b+1, 0))-cp.begin()-1;
wall[i].c = lower_bound(all(cp), pi(wall[i].c, i))-cp.begin();
}
tree1.resize(1<<20, LLONG_MAX);
tree2.resize(1<<20, LLONG_MAX);
update(tree1, 1, 0, sz-1, 0, 0); update(tree2, 1, 0, sz-1, sz-1, 0);
for(int i = 1; i <= m; i++) {
ll x = solve(tree1, 1, 0, sz-1, wall[i].a, wall[i].b);
if(x < LLONG_MAX) update(tree1, 1, 0, sz-1, wall[i].c, x+wall[i].d);
x = solve(tree2, 1, 0, sz-1, wall[i].a, wall[i].b);
if(x < LLONG_MAX) update(tree2, 1, 0, sz-1, wall[i].c, x+wall[i].d);
}
ll res = min(solve(tree1, 1, 0, sz-1, sz-1, sz-1), solve(tree2, 1, 0, sz-1, 0, 0));
for(int i = 1; i <= m; i++) {
ll x = solve(tree1, 1, 0, sz-1, wall[i].c, wall[i].c), y = solve(tree2, 1, 0, sz-1, wall[i].c, wall[i].c);
if(x < LLONG_MAX && y < LLONG_MAX) res = min(res, x+y-wall[i].d);
}
cout << (res == LLONG_MAX ? -1 : res);
}
Compilation message
pinball.cpp: In function 'void update(std::vector<long long int>&, int, int, int, int, ll)':
pinball.cpp:32:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = s+e>>1;
| ~^~
pinball.cpp: In function 'll solve(std::vector<long long int>&, int, int, int, int, int)':
pinball.cpp:41:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = s+e>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16884 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16884 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16728 KB |
Output is correct |
11 |
Correct |
4 ms |
16728 KB |
Output is correct |
12 |
Correct |
4 ms |
16732 KB |
Output is correct |
13 |
Correct |
5 ms |
16836 KB |
Output is correct |
14 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16884 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16728 KB |
Output is correct |
11 |
Correct |
4 ms |
16728 KB |
Output is correct |
12 |
Correct |
4 ms |
16732 KB |
Output is correct |
13 |
Correct |
5 ms |
16836 KB |
Output is correct |
14 |
Correct |
4 ms |
16732 KB |
Output is correct |
15 |
Correct |
4 ms |
16732 KB |
Output is correct |
16 |
Correct |
4 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16860 KB |
Output is correct |
18 |
Correct |
5 ms |
16732 KB |
Output is correct |
19 |
Correct |
5 ms |
16968 KB |
Output is correct |
20 |
Correct |
5 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16732 KB |
Output is correct |
22 |
Correct |
5 ms |
16732 KB |
Output is correct |
23 |
Correct |
5 ms |
16860 KB |
Output is correct |
24 |
Correct |
5 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16884 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
4 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16728 KB |
Output is correct |
11 |
Correct |
4 ms |
16728 KB |
Output is correct |
12 |
Correct |
4 ms |
16732 KB |
Output is correct |
13 |
Correct |
5 ms |
16836 KB |
Output is correct |
14 |
Correct |
4 ms |
16732 KB |
Output is correct |
15 |
Correct |
4 ms |
16732 KB |
Output is correct |
16 |
Correct |
4 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16860 KB |
Output is correct |
18 |
Correct |
5 ms |
16732 KB |
Output is correct |
19 |
Correct |
5 ms |
16968 KB |
Output is correct |
20 |
Correct |
5 ms |
16732 KB |
Output is correct |
21 |
Correct |
4 ms |
16732 KB |
Output is correct |
22 |
Correct |
5 ms |
16732 KB |
Output is correct |
23 |
Correct |
5 ms |
16860 KB |
Output is correct |
24 |
Correct |
5 ms |
16728 KB |
Output is correct |
25 |
Correct |
18 ms |
17500 KB |
Output is correct |
26 |
Correct |
45 ms |
18400 KB |
Output is correct |
27 |
Correct |
126 ms |
21132 KB |
Output is correct |
28 |
Correct |
198 ms |
22988 KB |
Output is correct |
29 |
Correct |
84 ms |
19920 KB |
Output is correct |
30 |
Correct |
187 ms |
23212 KB |
Output is correct |
31 |
Correct |
184 ms |
23244 KB |
Output is correct |
32 |
Correct |
191 ms |
23008 KB |
Output is correct |
33 |
Correct |
26 ms |
18144 KB |
Output is correct |
34 |
Correct |
80 ms |
20108 KB |
Output is correct |
35 |
Correct |
120 ms |
23244 KB |
Output is correct |
36 |
Correct |
169 ms |
23296 KB |
Output is correct |
37 |
Correct |
139 ms |
23244 KB |
Output is correct |
38 |
Correct |
138 ms |
23060 KB |
Output is correct |