/*
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
14 ms |
364 KB |
Output is correct |
26 |
Correct |
40 ms |
364 KB |
Output is correct |
27 |
Correct |
112 ms |
372 KB |
Output is correct |
28 |
Correct |
178 ms |
364 KB |
Output is correct |
29 |
Correct |
84 ms |
492 KB |
Output is correct |
30 |
Correct |
168 ms |
364 KB |
Output is correct |
31 |
Correct |
191 ms |
364 KB |
Output is correct |
32 |
Correct |
170 ms |
364 KB |
Output is correct |
33 |
Correct |
34 ms |
1132 KB |
Output is correct |
34 |
Correct |
91 ms |
364 KB |
Output is correct |
35 |
Correct |
198 ms |
5484 KB |
Output is correct |
36 |
Correct |
173 ms |
364 KB |
Output is correct |
37 |
Correct |
215 ms |
1132 KB |
Output is correct |
38 |
Correct |
185 ms |
1772 KB |
Output is correct |