#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
#define pb push_back
using pi = pair<int, int>;
const int INF = 1e16;
struct SegmentTree {
int n;
vector<int> data;
void update(int u, int st, int dr, int p, int x) {
if (st == dr) data[u] = min(data[u], x);
else {
int mid = (st + dr) / 2;
if (p <= mid) update(2 * u, st, mid, p, x);
else update(2 * u + 1, mid + 1, dr, p, x);
data[u] = min(data[2 * u], data[2 * u + 1]);
}
}
void update(int p, int x) {
update(1, 1, n, p, x);
}
int query(int u, int st, int dr, int l, int r) {
if (st >= l && dr <= r) return data[u];
int mid = (st + dr) / 2, res = INF;
if (l <= mid) res = query(2 * u, st, mid, l, r);
if (mid < r) res = min(res, query(2 * u + 1, mid + 1, dr, l, r));
return res;
}
int query(int l, int r) {
return query(1, 1, n, l, r);
}
SegmentTree() {}
SegmentTree(int n) {
this->n = n;
data.assign(4 * (n + 3), INF);
}
};
struct Device {
int a, b, c, d;
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int m, n;
cin >> m >> n;
vector<Device> d(m);
for (int i = 0; i < m; ++i) cin >> d[i].a >> d[i].b >> d[i].c >> d[i].d;
vector<int> vals{1, n};
for (int i = 0; i < m; ++i) vals.insert(vals.end(), {d[i].a, d[i].b, d[i].c});
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
auto norm = [&](int x) {
return upper_bound(vals.begin(), vals.end(), x) - vals.begin();
};
for (int i = 0; i < m; ++i) {
d[i].a = norm(d[i].a);
d[i].b = norm(d[i].b);
d[i].c = norm(d[i].c);
}
n = vals.size();
array<SegmentTree, 2> dp;
dp[0] = SegmentTree(n);
dp[1] = SegmentTree(n);
dp[0].update(1, 0);
dp[1].update(n, 0);
int ans = INF;
for (int i = 0; i < m; ++i) {
int minl = dp[0].query(d[i].a, d[i].b), minr = dp[1].query(d[i].a, d[i].b);
ans = min(ans, minl + minr + d[i].d);
for (int j = 0; j <= 1; ++j) {
int mini = dp[j].query(d[i].a, d[i].b);
dp[j].update(d[i].c, mini + d[i].d);
}
}
cout << (ans == INF ? -1 : ans) << endl;
}
/*
5 6
2 4 3 5
1 2 2 8
3 6 5 2
4 6 4 7
2 4 3 10
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
576 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
528 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
576 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
528 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
17 ms |
2392 KB |
Output is correct |
26 |
Correct |
53 ms |
6332 KB |
Output is correct |
27 |
Correct |
136 ms |
13080 KB |
Output is correct |
28 |
Correct |
138 ms |
12008 KB |
Output is correct |
29 |
Correct |
109 ms |
10824 KB |
Output is correct |
30 |
Correct |
165 ms |
10940 KB |
Output is correct |
31 |
Correct |
226 ms |
20676 KB |
Output is correct |
32 |
Correct |
208 ms |
17392 KB |
Output is correct |
33 |
Correct |
26 ms |
5840 KB |
Output is correct |
34 |
Correct |
92 ms |
14640 KB |
Output is correct |
35 |
Correct |
146 ms |
29380 KB |
Output is correct |
36 |
Correct |
218 ms |
28648 KB |
Output is correct |
37 |
Correct |
188 ms |
28632 KB |
Output is correct |
38 |
Correct |
189 ms |
29120 KB |
Output is correct |