#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;
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);
ll ans = inf;
for (const auto &i : a) {
ans = min(ans, (i.r == m - 1 ? 0 : R.query(i.l, i.r)) + (i.l == 0 ? 0 : L.query(i.l, i.r)) + i.c);
if (i.r == m - 1)
R.upd(i.p, i.c);
else
R.upd(i.p, i.c + R.query(i.l, i.r));
if (i.l == 0)
L.upd(i.p, i.c);
else
L.upd(i.p, i.c + L.query(i.l, i.r));
}
if (ans > (ll)n * 1e9) finish(-1);
finish(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
508 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |