#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int MAXN = 100005;
int n, m, a[MAXN], b[MAXN], c[MAXN];
ll d[MAXN];
vector<ll> v;
vector<vector<ll>> dp;
void compress() {
set<ll> s;
for (int i = 0; i < n; i++) {
if (s.find(c[i]) != s.end()) continue;
s.insert(c[i]);
v.push_back(c[i]);
}
sort(v.begin(), v.end());
}
ll query(int tree, int node, int l, int r, int L, int R) {
if (r < L or l >= R) return INF;
int mid = (l + r) / 2;
if (L <= l and r < R) return dp[tree][node];
else return min(query(tree, 2 * node, l, mid, L, R), query(tree, (2 * node) + 1, mid + 1, r, L, R));
}
void update(int tree, int node, int l, int r, int pos, ll x) {
if (l == r) dp[tree][node] = min(dp[tree][node], x);
else {
int mid = (l + r) / 2;
if (pos > mid) update(tree, (2 * node) + 1, mid + 1, r, pos, x);
else update(tree, 2 * node, l, mid, pos, x);
dp[tree][node] = min(dp[tree][2 * node], dp[tree][(2 * node) + 1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
compress();
int sz = v.size();
dp.resize(2, vector<ll>(4 * sz, INF));
ll ans = INF;
for (int i = 0; i < n; i++) {
ll ansl = INF, ansr = INF;
if (a[i] == 1) ansl = d[i];
if (b[i] == m) ansr = d[i];
int l = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
int r = upper_bound(v.begin(), v.end(), b[i]) - v.begin();
ansl = min(ansl, query(0, 1, 0, sz - 1, l, r) + d[i]);
ansr = min(ansr, query(1, 1, 0, sz - 1, l, r) + d[i]);
int pos = lower_bound(v.begin(), v.end(), c[i]) - v.begin();
if (ansl != INF and ansr != INF) ans = min(ans, ansl + ansr - d[i]);
update(0, 1, 0, sz - 1, pos, ansl);
update(1, 1, 0, sz - 1, pos, ansr);
}
cout << (ans != INF ? ans : -1) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
372 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
372 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
16 ms |
1604 KB |
Output is correct |
26 |
Correct |
49 ms |
3996 KB |
Output is correct |
27 |
Correct |
133 ms |
8508 KB |
Output is correct |
28 |
Correct |
115 ms |
2860 KB |
Output is correct |
29 |
Correct |
92 ms |
7000 KB |
Output is correct |
30 |
Correct |
146 ms |
3948 KB |
Output is correct |
31 |
Correct |
200 ms |
13104 KB |
Output is correct |
32 |
Correct |
191 ms |
11344 KB |
Output is correct |
33 |
Correct |
29 ms |
3040 KB |
Output is correct |
34 |
Correct |
101 ms |
8928 KB |
Output is correct |
35 |
Correct |
147 ms |
17436 KB |
Output is correct |
36 |
Correct |
220 ms |
17464 KB |
Output is correct |
37 |
Correct |
189 ms |
17536 KB |
Output is correct |
38 |
Correct |
188 ms |
17552 KB |
Output is correct |