// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
using ll = long long;
const ll INFL = ll(1e18) + 10;
struct Seg {
const ll ID = INFL; ll comb(ll a, ll b) { return min(a, b); }
vector<ll> seg; int n;
void init(int _n) {
n = _n;
seg.assign(2*n, ID);
}
void pull(int x) { seg[x] = comb(seg[2*x], seg[2*x+1]); }
void set(int p, ll x) {
p += n; seg[p] = min(seg[p], x); for(p /= 2; p; p /= 2) pull(p);
}
ll query(int l, int r) {
ll ra, rb; ra = 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() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
vector<int> A(N), B(N), C(N);
vector<ll> L(N), R(N), D(N);
vector<int> X = {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];
X.push_back(A[i]); X.push_back(B[i]); X.push_back(C[i]);
}
sort(begin(X), end(X)); X.erase(unique(begin(X), end(X)), end(X));
for(auto &x : A) x = lower_bound(begin(X), end(X), x) - begin(X);
for(auto &x : B) x = lower_bound(begin(X), end(X), x) - begin(X);
for(auto &x : C) x = lower_bound(begin(X), end(X), x) - begin(X);
int K = size(X);
for(int t = 0; t < 2; t++) {
Seg st; st.init(K);
st.set(0, 0);
for(int i = 0; i < N; i++) {
L[i] = st.query(A[i], B[i]) + D[i];
st.set(C[i], L[i]);
A[i] = K-1-A[i];
B[i] = K-1-B[i];
C[i] = K-1-C[i];
swap(A[i], B[i]);
}
L.swap(R);
}
ll ans = INFL;
// for(int i = 0; i < N; i++) cout << L[i] << " " << R[i] << nl;
for(int i = 0; i < N; i++) ans = min(ans, L[i] + R[i] - D[i]);
cout << (ans == INFL ? -1 : ans) << nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
400 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
456 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
400 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
456 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
11 ms |
1484 KB |
Output is correct |
26 |
Correct |
32 ms |
3320 KB |
Output is correct |
27 |
Correct |
91 ms |
7804 KB |
Output is correct |
28 |
Correct |
110 ms |
9576 KB |
Output is correct |
29 |
Correct |
65 ms |
6088 KB |
Output is correct |
30 |
Correct |
113 ms |
9612 KB |
Output is correct |
31 |
Correct |
145 ms |
11676 KB |
Output is correct |
32 |
Correct |
150 ms |
10856 KB |
Output is correct |
33 |
Correct |
19 ms |
2984 KB |
Output is correct |
34 |
Correct |
67 ms |
7196 KB |
Output is correct |
35 |
Correct |
94 ms |
13632 KB |
Output is correct |
36 |
Correct |
140 ms |
13724 KB |
Output is correct |
37 |
Correct |
143 ms |
13708 KB |
Output is correct |
38 |
Correct |
121 ms |
13640 KB |
Output is correct |