#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5, oo = 1e18;
int n, a[N], b[N], c[N], d[N], dp1[N], dp2[N], tot, bound;
map <int, int> inv;
set <int> val;
struct SegmentTree {
int n;
vector <int> st;
SegmentTree() {}
SegmentTree(int _n) {
this -> n = _n;
st.assign(4 * n + 5, oo);
}
int Left(int x) {return (x << 1);}
int Right(int x) {return (x << 1) + 1;}
void update(int id, int L, int R, int i, int val) {
if (L > i || R < i) return;
if (L == R) {
st[id] = min(st[id], val);
return;
}
int mid = (L + R) >> 1;
update(Left(id), L, mid, i, val);
update(Right(id), mid + 1, R, i, val);
st[id] = min(st[Left(id)], st[Right(id)]);
}
int get(int id, int L, int R, int i, int j) {
if (L > j || R < i) return oo;
if (L >= i && R <= j) {
return st[id];
}
int mid = (L + R) >> 1;
return min(get(Left(id), L, mid, i, j), get(Right(id), mid + 1, R, i, j));
}
void update(int i, int val) {
update(1, 1, n, i, val);
}
int get(int i, int j) {
return get(1, 1, n, i, j);
}
};
void solve1() {
SegmentTree solver = SegmentTree(tot);
for (int i = 1; i <= n; ++i)
dp1[i] = oo;
for (int i = 1; i <= n; ++i) {
if (a[i] == 1) {
solver.update(inv[c[i]], d[i]);
dp1[i] = d[i];
continue;
}
int u = *(val.lower_bound(a[i]));
auto it = val.upper_bound(b[i]);
it--;
int v = *it;
dp1[i] = min(dp1[i], solver.get(inv[u], inv[v]) + d[i]);
solver.update(inv[c[i]], solver.get(inv[u], inv[v]) + d[i]);
}
}
void solve2() {
SegmentTree solver = SegmentTree(tot);
for (int i = 1; i <= n; ++i)
dp2[i] = oo;
for (int i = 1; i <= n; ++i) {
if (b[i] == bound) {
solver.update(inv[c[i]], d[i]);
dp2[i] = d[i];
continue;
}
int u = *(val.lower_bound(a[i]));
auto it = val.upper_bound(b[i]);
it--;
int v = *it;
dp2[i] = min(dp2[i], solver.get(inv[u], inv[v]) + d[i]);
solver.update(inv[c[i]], solver.get(inv[u], inv[v]) + d[i]);
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> bound;
val.clear();
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
val.insert(c[i]);
}
for (int v : val) {
inv[v] = ++tot;
}
solve1();
solve2();
int res = oo;
for (int i = 1; i <= n; ++i)
res = min(res, dp1[i] + dp2[i] - d[i]);
if (res == oo) res = -1;
cout << res << endl;
return 0;
}
# |
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 |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
556 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
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 |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
556 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 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 |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
556 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
47 ms |
2072 KB |
Output is correct |
26 |
Correct |
162 ms |
5332 KB |
Output is correct |
27 |
Correct |
579 ms |
12608 KB |
Output is correct |
28 |
Correct |
289 ms |
8952 KB |
Output is correct |
29 |
Correct |
414 ms |
9896 KB |
Output is correct |
30 |
Correct |
474 ms |
10204 KB |
Output is correct |
31 |
Execution timed out |
1008 ms |
18932 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |