#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
using namespace std;
template<class T> void Min(T &a, const T &b) { a = min(a, b); }
const int64_t INF = (int64_t)1e18 + 10;
struct SegmTree {
struct Node {
int l = -1, r = -1;
int64_t val = INF;
};
int n;
vector<Node> t = vector<Node>(1);
SegmTree(int n_) : n(n_) {}
void Alloc(int &node) {
if (node != -1) return;
node = t.size();
t.emplace_back();
}
void Recalc(int node) {
t[node].val = INF;
int l = t[node].l, r = t[node].r;
if (l != -1) Min(t[node].val, t[l].val);
if (r != -1) Min(t[node].val, t[r].val);
}
void Update(int node, int left, int right, int pos, int64_t val) {
if (left == right) {
Min(t[node].val, val);
return;
}
int mid = left + (right - left) / 2;
if (pos <= mid) {
Alloc(t[node].l);
Update(t[node].l, left, mid, pos, val);
} else {
Alloc(t[node].r);
Update(t[node].r, mid + 1, right, pos, val);
}
Recalc(node);
}
int64_t Query(int node, int left, int right, int x, int y) {
if (y < left || x > right || node == -1) return INF;
if (x <= left && right <= y) {
return t[node].val;
}
int mid = left + (right - left) / 2;
return min(Query(t[node].l, left, mid, x, y),
Query(t[node].r, mid + 1, right, x, y));
}
void Update(int pos, int64_t val) {
Update(0, 0, n - 1, pos, val);
}
int64_t Query(int l, int r) {
return Query(0, 0, n - 1, l, r);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<int> lft(n), rgt(n), pos(n), cost(n);
for (int i = 0; i < n; ++i) {
cin >> lft[i] >> rgt[i] >> pos[i] >> cost[i];
--lft[i], --rgt[i], --pos[i];
}
// final position is monotonic
// L[i][j] = min cost to be at cell (i, j) if we start in col 1
// R[i][j] = _"_ col m
SegmTree L(m), R(m);
L.Update(0, 0);
R.Update(m - 1, 0);
int64_t ans = INF;
for (int i = 0; i < n; ++i) {
int l = lft[i], r = rgt[i], p = pos[i], c = cost[i];
int64_t curr_l = L.Query(l, r), curr_r = R.Query(l, r);
ans = min(ans, curr_l + curr_r + c);
L.Update(p, curr_l + c);
R.Update(p, curr_r + c);
}
if (ans == INF) ans = -1;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
1020 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
1404 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
1340 KB |
Output is correct |
23 |
Correct |
3 ms |
1404 KB |
Output is correct |
24 |
Correct |
4 ms |
1404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
1020 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
1404 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
1340 KB |
Output is correct |
23 |
Correct |
3 ms |
1404 KB |
Output is correct |
24 |
Correct |
4 ms |
1404 KB |
Output is correct |
25 |
Correct |
23 ms |
2796 KB |
Output is correct |
26 |
Correct |
80 ms |
9368 KB |
Output is correct |
27 |
Correct |
292 ms |
28856 KB |
Output is correct |
28 |
Correct |
308 ms |
6388 KB |
Output is correct |
29 |
Correct |
226 ms |
27956 KB |
Output is correct |
30 |
Correct |
385 ms |
12252 KB |
Output is correct |
31 |
Correct |
478 ms |
38664 KB |
Output is correct |
32 |
Correct |
537 ms |
32300 KB |
Output is correct |
33 |
Correct |
46 ms |
7720 KB |
Output is correct |
34 |
Correct |
198 ms |
27892 KB |
Output is correct |
35 |
Correct |
293 ms |
55312 KB |
Output is correct |
36 |
Correct |
459 ms |
55488 KB |
Output is correct |
37 |
Correct |
405 ms |
55512 KB |
Output is correct |
38 |
Correct |
370 ms |
55188 KB |
Output is correct |