#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
p += n;
seg[p] = min(seg[p], val); for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), c(n);
vector<long long> L(n), R(n), d(n);
vector<int> total = {0, m-1};
for(int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
c[i]--;
total.push_back(a[i]);
total.push_back(b[i]);
total.push_back(c[i]);
}
sort(total.begin(), total.end());
total.erase(unique(total.begin(), total.end()), total.end());
for(auto &x : a){
x = lower_bound(total.begin(), total.end(), x) - total.begin();
}
for(auto &x : b){
x = lower_bound(total.begin(), total.end(), x) - total.begin();
}
for(auto &x : c){
x = lower_bound(total.begin(), total.end(), x) - total.begin();
}
int val = total.size();
for(int t = 0; t < 2; t++) {
Seg<long long> SegTree;
SegTree.init(val);
SegTree.upd(0, 0);
for(int i = 0; i < n; i++) {
L[i] = SegTree.query(a[i], b[i]) + d[i];
SegTree.upd(c[i], L[i]);
a[i] = val - 1 - a[i];
b[i] = val - 1 - b[i];
c[i] = val - 1 - c[i];
swap(a[i], b[i]);
}
L.swap(R);
}
long long ans = (long long)1e18 + 10;
for(int i = 0; i < n; i++){
ans = min(ans, L[i] + R[i] - d[i]);
}
cout << (ans == (long long)1e18 + 10 ? -1 : ans) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
224 KB |
Output is correct |
17 |
Correct |
2 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
444 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
1 ms |
224 KB |
Output is correct |
17 |
Correct |
2 ms |
312 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
444 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
440 KB |
Output is correct |
25 |
Correct |
17 ms |
1456 KB |
Output is correct |
26 |
Correct |
52 ms |
3328 KB |
Output is correct |
27 |
Correct |
136 ms |
7784 KB |
Output is correct |
28 |
Correct |
167 ms |
9584 KB |
Output is correct |
29 |
Correct |
98 ms |
6140 KB |
Output is correct |
30 |
Correct |
187 ms |
9548 KB |
Output is correct |
31 |
Correct |
204 ms |
11568 KB |
Output is correct |
32 |
Correct |
198 ms |
10808 KB |
Output is correct |
33 |
Correct |
30 ms |
2880 KB |
Output is correct |
34 |
Correct |
96 ms |
7032 KB |
Output is correct |
35 |
Correct |
161 ms |
13680 KB |
Output is correct |
36 |
Correct |
235 ms |
13688 KB |
Output is correct |
37 |
Correct |
196 ms |
13620 KB |
Output is correct |
38 |
Correct |
185 ms |
13640 KB |
Output is correct |