#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, pi> pii;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll m, n, a, b, c, d, ans = LLINF;
vpii v;
vi coord;
struct node {
ll s, e, m, v;
node *l, *r;
node(ll S, ll E) {
s = S, e = E, m = (s+e)/2, v = LLINF;
if (s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void up(ll x, ll nv) {
if (s == e) {v = min(v, nv); return;}
else if (x <= m) l->up(x, nv);
else r->up(x, nv);
v = min(l->v, r->v);
}
ll query(ll x, ll y) {
if (s == x and e == y) return v;
else if (y <= m) return l->query(x, y);
else if (x > m) return r->query(x, y);
else return min(l->query(x, m), r->query(m+1, y));
}
} *lseg, *rseg;
int main() {
fastio; cin >> m >> n;
FOR(i, 1, m) {
cin >> a >> b >> c >> d;
v.pb(pii(pi(a, b), pi(c, d)));
coord.pb(a);
coord.pb(b);
coord.pb(c);
}
coord.pb(1); coord.pb(n);
sort(all(coord));
coord.resize(unique(all(coord)) - coord.begin());
n = coord.size();
lseg = new node(1, coord.size());
rseg = new node(1, coord.size());
for (auto it:v) {
a = it.f.f, b = it.f.s, c = it.s.f, d = it.s.s;
a = lower_bound(all(coord), a) - coord.begin() + 1;
b = lower_bound(all(coord), b) - coord.begin() + 1;
c = lower_bound(all(coord), c) - coord.begin() + 1;
ll lv = (a == 1 ? 0 : lseg->query(a, b)) + d;
ll rv = (b == n ? 0 : rseg->query(a, b)) + d;
lseg->up(c, lv);
rseg->up(c, rv);
ans = min(ans, lv + rv - d);
}
cout << (ans == LLINF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
488 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
488 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
4 ms |
1152 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
3 ms |
1152 KB |
Output is correct |
23 |
Correct |
4 ms |
1152 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
488 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
4 ms |
1152 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
3 ms |
1152 KB |
Output is correct |
23 |
Correct |
4 ms |
1152 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
31 ms |
5496 KB |
Output is correct |
26 |
Correct |
127 ms |
16608 KB |
Output is correct |
27 |
Correct |
364 ms |
32196 KB |
Output is correct |
28 |
Correct |
161 ms |
10964 KB |
Output is correct |
29 |
Correct |
280 ms |
27992 KB |
Output is correct |
30 |
Correct |
259 ms |
14156 KB |
Output is correct |
31 |
Correct |
602 ms |
51800 KB |
Output is correct |
32 |
Correct |
542 ms |
39952 KB |
Output is correct |
33 |
Correct |
57 ms |
16880 KB |
Output is correct |
34 |
Correct |
277 ms |
42596 KB |
Output is correct |
35 |
Correct |
295 ms |
84680 KB |
Output is correct |
36 |
Correct |
705 ms |
84800 KB |
Output is correct |
37 |
Correct |
482 ms |
84812 KB |
Output is correct |
38 |
Correct |
453 ms |
84808 KB |
Output is correct |