#include <bits/stdc++.h>
#define pii pair<ll, ll>
typedef long long ll;
using namespace std;
const int SZ = 1 << 19, MAX = 100007;
ll A[MAX], B[MAX], C[MAX], W[MAX], L[MAX], R[MAX];
struct Seg {
ll T[SZ << 1];
Seg() {
memset(T, 0x3f, sizeof(T));
}
void Update(int i, ll v) {
i += SZ - 1;
T[i] = min(T[i], v);
while (i >>= 1) T[i] = min(T[i * 2], T[i * 2 + 1]);
}
ll Query(int L, int R, int sL = 1, int sR = SZ, int n = 1) {
if (R < sL || sR < L) return 1e18;
if (L <= sL && sR <= R) return T[n];
int mid = (sL + sR) >> 1;
return min(Query(L, R, sL, mid, n * 2), Query(L, R, mid + 1, sR, n * 2 + 1));
}
} TL, TR;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> U = {0, 1, M};
for (int i = 1; i <= N; ++i) {
cin >> A[i] >> B[i] >> C[i] >> W[i];
U.push_back(A[i]);
U.push_back(B[i]);
U.push_back(C[i]);
}
sort(U.begin(), U.end());
U.erase(unique(U.begin(), U.end()), U.end());
for (int i = 1; i <= N; ++i) {
A[i] = lower_bound(U.begin(), U.end(), A[i]) - U.begin();
B[i] = lower_bound(U.begin(), U.end(), B[i]) - U.begin();
C[i] = lower_bound(U.begin(), U.end(), C[i]) - U.begin();
}
memset(L, 0x3f, sizeof(L));
memset(R, 0x3f, sizeof(R));
TL.Update(1, 0);
TR.Update(U.size() - 1, 0);
for (int i = 1; i <= N; ++i) {
ll ul = TL.Query(A[i], B[i]);
ll ur = TR.Query(A[i], B[i]);
TL.Update(C[i], L[i] = ul + W[i]);
TR.Update(C[i], R[i] = ur + W[i]);
}
ll ans = 1e18;
for (int i = 1; i <= N; ++i) ans = min(ans, L[i] + R[i] - W[i]);
cout << (ans == 1e18 ? -1 : ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
3 ms |
20968 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
3 ms |
21072 KB |
Output is correct |
5 |
Correct |
3 ms |
21072 KB |
Output is correct |
6 |
Correct |
3 ms |
21072 KB |
Output is correct |
7 |
Correct |
3 ms |
21072 KB |
Output is correct |
8 |
Correct |
3 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
3 ms |
20968 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
3 ms |
21072 KB |
Output is correct |
5 |
Correct |
3 ms |
21072 KB |
Output is correct |
6 |
Correct |
3 ms |
21072 KB |
Output is correct |
7 |
Correct |
3 ms |
21072 KB |
Output is correct |
8 |
Correct |
3 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21072 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
3 ms |
20968 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
3 ms |
21072 KB |
Output is correct |
5 |
Correct |
3 ms |
21072 KB |
Output is correct |
6 |
Correct |
3 ms |
21072 KB |
Output is correct |
7 |
Correct |
3 ms |
21072 KB |
Output is correct |
8 |
Correct |
3 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21072 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
15 |
Correct |
4 ms |
21072 KB |
Output is correct |
16 |
Correct |
4 ms |
21152 KB |
Output is correct |
17 |
Correct |
4 ms |
21072 KB |
Output is correct |
18 |
Correct |
4 ms |
21072 KB |
Output is correct |
19 |
Correct |
5 ms |
21072 KB |
Output is correct |
20 |
Correct |
5 ms |
21072 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
6 ms |
21072 KB |
Output is correct |
23 |
Correct |
5 ms |
21072 KB |
Output is correct |
24 |
Correct |
4 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
21072 KB |
Output is correct |
2 |
Correct |
3 ms |
20968 KB |
Output is correct |
3 |
Correct |
4 ms |
21072 KB |
Output is correct |
4 |
Correct |
3 ms |
21072 KB |
Output is correct |
5 |
Correct |
3 ms |
21072 KB |
Output is correct |
6 |
Correct |
3 ms |
21072 KB |
Output is correct |
7 |
Correct |
3 ms |
21072 KB |
Output is correct |
8 |
Correct |
3 ms |
21072 KB |
Output is correct |
9 |
Correct |
4 ms |
21072 KB |
Output is correct |
10 |
Correct |
4 ms |
21072 KB |
Output is correct |
11 |
Correct |
4 ms |
21072 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21072 KB |
Output is correct |
14 |
Correct |
4 ms |
21072 KB |
Output is correct |
15 |
Correct |
4 ms |
21072 KB |
Output is correct |
16 |
Correct |
4 ms |
21152 KB |
Output is correct |
17 |
Correct |
4 ms |
21072 KB |
Output is correct |
18 |
Correct |
4 ms |
21072 KB |
Output is correct |
19 |
Correct |
5 ms |
21072 KB |
Output is correct |
20 |
Correct |
5 ms |
21072 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
6 ms |
21072 KB |
Output is correct |
23 |
Correct |
5 ms |
21072 KB |
Output is correct |
24 |
Correct |
4 ms |
21072 KB |
Output is correct |
25 |
Correct |
15 ms |
21584 KB |
Output is correct |
26 |
Correct |
36 ms |
22380 KB |
Output is correct |
27 |
Correct |
100 ms |
25288 KB |
Output is correct |
28 |
Correct |
105 ms |
26556 KB |
Output is correct |
29 |
Correct |
90 ms |
23708 KB |
Output is correct |
30 |
Correct |
129 ms |
26556 KB |
Output is correct |
31 |
Correct |
143 ms |
26728 KB |
Output is correct |
32 |
Correct |
145 ms |
26740 KB |
Output is correct |
33 |
Correct |
21 ms |
22288 KB |
Output is correct |
34 |
Correct |
68 ms |
23752 KB |
Output is correct |
35 |
Correct |
109 ms |
26708 KB |
Output is correct |
36 |
Correct |
155 ms |
26556 KB |
Output is correct |
37 |
Correct |
125 ms |
26812 KB |
Output is correct |
38 |
Correct |
133 ms |
26564 KB |
Output is correct |