#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
struct SegmentTree {
int n;
vector <ll> t;
void modify(int id, ll val, int l, int r, int v) {
if (l == r) {
t[v] = min(t[v], val);
} else {
int m = (l + r) >> 1;
if (id <= m) modify(id, val, l, m, 2 * v + 1);
else modify(id, val, m + 1, r, 2 * v + 2);
t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
}
ll get(int ql, int qr, int l, int r, int v) {
if (qr < l || r < ql) return Inf;
if (ql <= l && r <= qr) return t[v];
int m = (l + r) >> 1;
return min(get(ql, qr, l, m, 2 * v + 1), get(ql, qr, m + 1, r, 2 * v + 2));
}
SegmentTree(int nn) {
n = nn;
t.resize(4 * n, Inf);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
vector <ll> dpL(n, Inf), dpR(n, Inf);
SegmentTree trL(m + 1), trR(m + 1);
ll ans = Inf;
for (int i = 0; i < n; ++i) {
ll costL = trL.get(a[i], b[i], 0, m, 0), costR = trR.get(a[i], b[i], 0, m, 0);
if (a[i] == 1) costL = 0;
if (b[i] == m) costR = 0;
ans = min(ans, costL + costR + d[i]);
if (a[i] == 1) {
dpL[i] = d[i];
} else {
dpL[i] = min(dpL[i], d[i] + trL.get(a[i], b[i], 0, m, 0));
}
if (b[i] == m) {
dpR[i] = d[i];
} else {
dpR[i] = min(dpR[i], d[i] + trR.get(a[i], b[i], 0, m, 0));
}
//cout << c[i] << ' ' << dpL[i] << ' ' << dpR[i] << endl;
trL.modify(c[i], dpL[i], 0, m, 0);
trR.modify(c[i], dpR[i], 0, m, 0);
}
if (ans != Inf) cout << ans << '\n';
else cout << "-1\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
31 ms |
62976 KB |
Output is correct |
10 |
Runtime error |
261 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
31 ms |
62976 KB |
Output is correct |
10 |
Runtime error |
261 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
31 ms |
62976 KB |
Output is correct |
10 |
Runtime error |
261 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
11 |
Halted |
0 ms |
0 KB |
- |