#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using i32 = int;
using u32 = unsigned;
using i16 = short;
using u16 = unsigned short;
using ld = long double;
using ii = pair<int, int>;
const i64 INF = 1e15L;
int n, m;
vector<i64> st;
i64 query(int l, int r) {
i64 ans = INF;
for(l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if(l & 1) ans = min(ans, st[l++]);
if(~r & 1) ans = min(ans, st[r--]);
}
return ans;
}
void upd(int k, i64 x) {
k += n;
st[k] = min(st[k], x);
for(k >>= 1; k; k >>= 1) st[k] = min(st[k << 1], st[(k << 1) | 1]);
}
void solve() {
cin >> n >> m;
vector<int> a(n), b(n), c(n), d(n);
vector<i64> dp(n, 0);
i64 ans = INF;
for(int i = 0; i < n; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i];
auto x = c;
sort(x.begin(), x.end());
st.assign(2 * n + 1, INF);
for(int i = 0; i < n; ++i) {
if(a[i] == 1) {
int k = lower_bound(x.begin(), x.end(), c[i]) - x.begin();
upd(k, d[i]);
dp[i] = d[i];
} else {
int l = lower_bound(x.begin(), x.end(), a[i]) - x.begin();
int r = upper_bound(x.begin(), x.end(), b[i]) - x.begin() - 1;
dp[i] = d[i] + query(l, r);
upd(lower_bound(x.begin(), x.end(), c[i]) - x.begin(), dp[i]);
}
if(b[i] == m) ans = min(ans, dp[i]);
}
fill(st.begin(), st.end(), INF);
for(int i = 0; i < n; ++i) {
if(b[i] == m) {
int k = lower_bound(x.begin(), x.end(), c[i]) - x.begin();
upd(k, d[i]);
dp[i] += d[i];
} else {
int l = lower_bound(x.begin(), x.end(), a[i]) - x.begin();
int r = upper_bound(x.begin(), x.end(), b[i]) - x.begin() - 1;
i64 q = query(l, r);
dp[i] += q;
upd(lower_bound(x.begin(), x.end(), c[i]) - x.begin(), d[i] + q);
}
ans = min(ans, dp[i]);
}
if(ans >= INF) ans = -1;
cout << ans << '\n';
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
25 |
Correct |
12 ms |
904 KB |
Output is correct |
26 |
Correct |
32 ms |
2092 KB |
Output is correct |
27 |
Correct |
103 ms |
5524 KB |
Output is correct |
28 |
Correct |
139 ms |
8036 KB |
Output is correct |
29 |
Correct |
73 ms |
4164 KB |
Output is correct |
30 |
Correct |
154 ms |
8152 KB |
Output is correct |
31 |
Correct |
151 ms |
8092 KB |
Output is correct |
32 |
Correct |
151 ms |
8172 KB |
Output is correct |
33 |
Correct |
18 ms |
1732 KB |
Output is correct |
34 |
Correct |
64 ms |
4268 KB |
Output is correct |
35 |
Correct |
92 ms |
8084 KB |
Output is correct |
36 |
Correct |
148 ms |
8060 KB |
Output is correct |
37 |
Correct |
128 ms |
8108 KB |
Output is correct |
38 |
Correct |
123 ms |
8068 KB |
Output is correct |