-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpinball.cpp
65 lines (58 loc) · 1.45 KB
/
pinball.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Solution: O(M logM)
Have two strictly increasing maps to store the required cost for left boundaries and right boundaries.
(Strictly increasing allows retrieval of min cost for a device in O(logN) time)
*/
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct si_map{
map<int, int> mp;
void insert(int x, int v){
auto it = mp.lower_bound(x);
if(it != mp.end() && it->second <= v){
return;
}
if(it != mp.begin()){
it--;
vector<int> rm;
while(true){
if(it->second < v){
break;
}
rm.push_back(it->first);
if(it == mp.begin()){
break;
}
it--;
}
for(int i : rm){
mp.erase(i);
}
}
mp[x] = v;
}
int qry(int x){
auto it = mp.lower_bound(x);
if(it == mp.end()){
return 1e16;
}
return it->second;
}
};
int M, N;
si_map l, r;
int32_t main(){
cin >> M >> N;
int ans = 1e16;
l.insert(1, 0);
r.insert(-N, 0);
for(int i = 0; i < M; i++){
int a, b, c, d; cin >> a >> b >> c >> d;
int lcost = l.qry(a), rcost = r.qry(-b);
ans = min(ans, lcost + rcost + d);
l.insert(c, lcost + d);
r.insert(-c, rcost + d);
}
cout << (ans == 1e16 ? -1 : ans) << endl;
}