#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct SegMin {
vector <ll> seg;
int n;
SegMin (int _n) {
n = _n;
seg.resize (n * 4 + 4, (1ll << 60));
}
void updatePos (int node, int lb, int rb, int pos, ll val) {
if (lb == rb) {
seg[node] = min (seg[node], val);
return;
}
int mid = (lb + rb) / 2;
if (pos <= mid)
updatePos (node * 2, lb, mid, pos, val);
else
updatePos (node * 2 + 1, mid + 1, rb, pos, val);
seg[node] = min (seg[node * 2], seg[node * 2 + 1]);
}
ll queryRange (int node, int lb, int rb, int lQ, int rQ) {
if (lQ <= lb && rb <= rQ)
return seg[node];
int mid = (lb + rb) / 2;
ll ans = (1ll << 60);
if (mid >= lQ)
ans = min (ans, queryRange (node * 2, lb, mid, lQ, rQ));
if (mid < rQ)
ans = min (ans, queryRange (node * 2 + 1, mid + 1, rb, lQ, rQ));
return ans;
}
};
const int N = 1e5;
int a[1 + N], b[1 + N], c[1 + N], d[1 + N];
#define dbg(x) cerr << #x << " " << x << "\n"
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int m, n;
cin >> m >> n;
map <int, int> mp;
mp[1]; mp[n];
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
mp[a[i]]; mp[b[i]]; mp[c[i]];
}
int nr = 0;
for (auto &x : mp)
x.second = ++nr;
for (int i = 1; i <= m; i++) {
a[i] = mp[a[i]]; b[i] = mp[b[i]]; c[i] = mp[c[i]];
}
SegMin pref (nr), suff (nr);
pref.updatePos (1, 1, nr, 1, 0);
suff.updatePos (1, 1, nr, nr, 0);
/// dbg (nr);
ll ans = (1ll << 60);
for (int i = 1; i <= m; i++) {
/// dbg (i);
/// dbg (a[i]); dbg (b[i]); dbg (c[i]);
ll bestL = pref.queryRange (1, 1, nr, a[i], b[i]);
ll bestR = suff.queryRange (1, 1, nr, a[i], b[i]);
ans = min (ans, bestL + bestR + d[i]);
pref.updatePos (1, 1, nr, c[i], bestL + d[i]);
suff.updatePos (1, 1, nr, c[i], bestR + d[i]);
}
if (ans == (1ll << 60))
ans = -1;
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
25 |
Correct |
26 ms |
2688 KB |
Output is correct |
26 |
Correct |
88 ms |
7800 KB |
Output is correct |
27 |
Correct |
292 ms |
15228 KB |
Output is correct |
28 |
Correct |
159 ms |
5880 KB |
Output is correct |
29 |
Correct |
199 ms |
13176 KB |
Output is correct |
30 |
Correct |
238 ms |
7800 KB |
Output is correct |
31 |
Correct |
532 ms |
24312 KB |
Output is correct |
32 |
Correct |
436 ms |
19064 KB |
Output is correct |
33 |
Correct |
49 ms |
7800 KB |
Output is correct |
34 |
Correct |
242 ms |
19448 KB |
Output is correct |
35 |
Correct |
276 ms |
38648 KB |
Output is correct |
36 |
Correct |
678 ms |
38648 KB |
Output is correct |
37 |
Correct |
526 ms |
38776 KB |
Output is correct |
38 |
Correct |
474 ms |
38648 KB |
Output is correct |