#include <bits/stdc++.h>
using namespace std;
struct tree {
typedef long long T;
T f(T a, T b) { return min(a, b); }
vector<T> s;
int n;
T unit = 0;
tree(int nn, T fil) : s(2 * nn, fil), n(nn), unit(fil) {}
void update(int p, T v)
{
p += n;
for(s[p] = min(s[p], v); p /= 2;) {
s[p] = f(s[2 * p], s[2 * p + 1]);
}
}
T query(int a, int b)
{
T ra = unit, rb = unit;
for(a += n, b += n + 1; a < b; a /= 2, b /= 2) {
if(a % 2) ra = f(ra, s[a++]);
if(b % 2) rb = f(rb, s[--b]);
}
return f(ra, rb);
}
};
#define make_unique(v) sort((v).begin(), (v).end()), (v).erase(unique((v).begin(), (v).end()), (v).end())
struct scaler {
vector<int> all;
void insert(int x) { all.emplace_back(x); }
void init() { make_unique(all); }
int get(int x) { return lower_bound(all.begin(), all.end(), x) - all.begin(); }
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int m, n;
cin >> m >> n;
scaler s;
vector<int> a(m + 1), b(m + 1), c(m + 1), d(m + 1);
for(int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
s.insert(a[i]), s.insert(b[i]), s.insert(c[i]);
}
s.insert(1), s.insert(n);
s.init();
for(int i = 1; i <= m; ++i) {
a[i] = s.get(a[i]), b[i] = s.get(b[i]), c[i] = s.get(c[i]);
}
int sz = (int)s.all.size();
const long long oo = 1e18 + 5;
tree one(sz + 123, oo), two(sz + 123, oo);
one.update(s.get(0), 0);
two.update(s.get(n), 0);
long long ans = oo;
for(int i = 1; i <= m; ++i) {
long long dp1 = one.query(a[i], b[i]);
one.update(c[i], dp1 + d[i]);
long long dp2 = two.query(a[i], b[i]);
two.update(c[i], dp2 + d[i]);
ans = min(ans, dp1 + dp2 + d[i]);
}
cout << (ans == oo ? -1 : ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
12 ms |
1492 KB |
Output is correct |
26 |
Correct |
32 ms |
3696 KB |
Output is correct |
27 |
Correct |
102 ms |
7964 KB |
Output is correct |
28 |
Correct |
104 ms |
7756 KB |
Output is correct |
29 |
Correct |
80 ms |
6564 KB |
Output is correct |
30 |
Correct |
121 ms |
7620 KB |
Output is correct |
31 |
Correct |
150 ms |
12360 KB |
Output is correct |
32 |
Correct |
160 ms |
10872 KB |
Output is correct |
33 |
Correct |
25 ms |
3460 KB |
Output is correct |
34 |
Correct |
96 ms |
8452 KB |
Output is correct |
35 |
Correct |
106 ms |
16460 KB |
Output is correct |
36 |
Correct |
184 ms |
16460 KB |
Output is correct |
37 |
Correct |
163 ms |
16424 KB |
Output is correct |
38 |
Correct |
157 ms |
16404 KB |
Output is correct |