#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define int long long
const int N = 1e5+5;
struct interval {
int l, r, pos, cost;
};
//-------------------------------
interval a[N];
int n, len;
vector<int> vals;
struct SegmentTree {
ll seg[N*8];
SegmentTree() {
memset(seg, 0x3f, sizeof(seg));
}
void update(int pos, ll val, int l = 0, int r = vals.size() - 1, int id = 1) {
if (l > pos || r < pos) return;
if (l == r) {
seg[id] = min(seg[id], val);
return;
}
int mid = (l+r) / 2;
update(pos, val, l, mid, id*2);
update(pos, val, mid+1, r, id*2 + 1);
seg[id] = min(seg[id*2], seg[id*2 + 1]);
}
ll get(int u, int v, int l = 0, int r = vals.size() - 1, int id = 1) {
if (l > v || r < u) return 4e18;
if (l >= u && r <= v) return seg[id];
int mid = (l+r) / 2;
return min(get(u, v, l, mid, id*2),
get(u, v, mid+1, r, id*2 + 1));
}
} lTree, rTree;
int idx(int val) {
return lower_bound(vals.begin(), vals.end(), val) - vals.begin();
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> len;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r >> a[i].pos >> a[i].cost;
vals.push_back(a[i].l);
vals.push_back(a[i].r);
}
vals.push_back(1); vals.push_back(len);
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
for (int i = 1; i <= n; i++) {
a[i].l = idx(a[i].l); a[i].r = idx(a[i].r); a[i].pos = idx(a[i].pos);
}
ll ans = 4e18;
lTree.update(idx(1), 0); rTree.update(idx(len), 0);
for (int i = 1; i <= n; i++) {
ll v1 = lTree.get(a[i].l, a[i].r) + a[i].cost;
ll v2 = rTree.get(a[i].l, a[i].r) + a[i].cost;
ans = min(ans, v1+v2 - a[i].cost);
lTree.update(a[i].pos, v1);
rTree.update(a[i].pos, v2);
}
cout << (ans > 1e18 ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14996 KB |
Output is correct |
9 |
Correct |
2 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
15000 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14996 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14996 KB |
Output is correct |
9 |
Correct |
2 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
15000 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14996 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
2 ms |
14940 KB |
Output is correct |
16 |
Correct |
2 ms |
14952 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
4 ms |
15052 KB |
Output is correct |
20 |
Correct |
3 ms |
15008 KB |
Output is correct |
21 |
Correct |
2 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
2 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14996 KB |
Output is correct |
9 |
Correct |
2 ms |
14936 KB |
Output is correct |
10 |
Correct |
3 ms |
15000 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14996 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
2 ms |
14940 KB |
Output is correct |
16 |
Correct |
2 ms |
14952 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 KB |
Output is correct |
18 |
Correct |
3 ms |
15196 KB |
Output is correct |
19 |
Correct |
4 ms |
15052 KB |
Output is correct |
20 |
Correct |
3 ms |
15008 KB |
Output is correct |
21 |
Correct |
2 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14940 KB |
Output is correct |
25 |
Correct |
14 ms |
15584 KB |
Output is correct |
26 |
Correct |
38 ms |
16364 KB |
Output is correct |
27 |
Correct |
108 ms |
19672 KB |
Output is correct |
28 |
Correct |
104 ms |
21448 KB |
Output is correct |
29 |
Correct |
83 ms |
17620 KB |
Output is correct |
30 |
Correct |
132 ms |
21448 KB |
Output is correct |
31 |
Correct |
170 ms |
21624 KB |
Output is correct |
32 |
Correct |
160 ms |
21592 KB |
Output is correct |
33 |
Incorrect |
21 ms |
16096 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |