#include <bits/stdc++.h>
#define endl '\n'
#define debug cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll hs = 199, inf = 1e18;
const ldb eps = 1e-9, pi = acos(-1);
using namespace std;
struct segtree {
int n;
vector<ll> tree;
segtree() {}
segtree(int size) {
n = max(2, size);
while (n != (n & -n)) n += (n & -n);
tree.resize(2 * n, inf);
}
void fix(int x) {
tree[x] = min(tree[2 * x + 1], tree[2 * x + 2]);
}
void upd(int pos, ll x) {
pos += n - 1;
tree[pos] = min(tree[pos], x);
pos = (pos - 1) / 2;
while (pos) {
fix(pos);
pos = (pos - 1) / 2;
}
fix(0);
}
ll query(int l, int r) {
ll rtn = inf;
for (l += n - 1, r += n - 1; l < r; l = (l - 1) / 2, r = (r - 1) / 2) {
if (!(l & 1)) rtn = min(rtn, tree[l++]);
if (r & 1) rtn = min(rtn, tree[r--]);
}
if (l == r) rtn = min(rtn, tree[l]);
return rtn;
}
ll operator [](int pos) {
return tree[pos + n - 1];
}
};
struct obj {
ll l, r, p, c;
obj() {}
obj(ll lll, ll rr, ll pp, ll cc) {
l = lll;
r = rr;
p = pp;
c = cc;
}
};
int n, m;
vector<obj> a;
segtree L, R;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
a.resize(n);
map<int, int> comp;
comp[1] = comp[m] = 0;
for (int i = 0, par[4]; i < n; i++) {
cin >> par[0] >> par[1] >> par[2] >> par[3];
comp[par[0]] = comp[par[1]] = comp[par[2]] = 0;
a[i] = obj(par[0], par[1], par[2], par[3]);
}
int x = 0;
for (auto &i : comp) i.second = x++;
for (auto &i : a) {
i.l = comp[i.l];
i.r = comp[i.r];
i.p = comp[i.p];
}
m = comp.size();
L = R = segtree(m);
L.upd(0, 0LL);
R.upd(m - 1, 0LL);
ll ans = inf;
for (const auto &i : a) {
ans = min(ans, R.query(i.l, i.r) + L.query(i.l, i.r) + i.c);
R.upd(i.p, i.c + R.query(i.l, i.r));
L.upd(i.p, i.c + L.query(i.l, i.r));
}
if (ans == inf) finish(-1);
finish(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
648 KB |
Output is correct |
13 |
Correct |
2 ms |
656 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
648 KB |
Output is correct |
13 |
Correct |
2 ms |
656 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
4 ms |
832 KB |
Output is correct |
18 |
Correct |
4 ms |
832 KB |
Output is correct |
19 |
Correct |
4 ms |
1040 KB |
Output is correct |
20 |
Correct |
4 ms |
1040 KB |
Output is correct |
21 |
Correct |
3 ms |
1040 KB |
Output is correct |
22 |
Correct |
5 ms |
1136 KB |
Output is correct |
23 |
Correct |
4 ms |
1176 KB |
Output is correct |
24 |
Correct |
5 ms |
1216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
500 KB |
Output is correct |
3 |
Correct |
2 ms |
500 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
2 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
2 ms |
632 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
648 KB |
Output is correct |
13 |
Correct |
2 ms |
656 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
2 ms |
676 KB |
Output is correct |
17 |
Correct |
4 ms |
832 KB |
Output is correct |
18 |
Correct |
4 ms |
832 KB |
Output is correct |
19 |
Correct |
4 ms |
1040 KB |
Output is correct |
20 |
Correct |
4 ms |
1040 KB |
Output is correct |
21 |
Correct |
3 ms |
1040 KB |
Output is correct |
22 |
Correct |
5 ms |
1136 KB |
Output is correct |
23 |
Correct |
4 ms |
1176 KB |
Output is correct |
24 |
Correct |
5 ms |
1216 KB |
Output is correct |
25 |
Correct |
28 ms |
3400 KB |
Output is correct |
26 |
Correct |
103 ms |
7564 KB |
Output is correct |
27 |
Correct |
249 ms |
15020 KB |
Output is correct |
28 |
Correct |
151 ms |
15020 KB |
Output is correct |
29 |
Correct |
181 ms |
15020 KB |
Output is correct |
30 |
Correct |
267 ms |
15020 KB |
Output is correct |
31 |
Correct |
490 ms |
23172 KB |
Output is correct |
32 |
Correct |
426 ms |
23172 KB |
Output is correct |
33 |
Correct |
42 ms |
23172 KB |
Output is correct |
34 |
Correct |
192 ms |
24588 KB |
Output is correct |
35 |
Correct |
251 ms |
41328 KB |
Output is correct |
36 |
Correct |
594 ms |
41612 KB |
Output is correct |
37 |
Correct |
549 ms |
41612 KB |
Output is correct |
38 |
Correct |
387 ms |
41612 KB |
Output is correct |