#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
#define MAX 2010100
#define INF ((ll)1<<62)
#define ln '\n'
struct dat {
ll a, b, c, d;
};
dat arr[MAX];
vector<ll> xpos;
ll s;
ll tree[MAX];
ll lll[MAX], rrr[MAX];
ll l[MAX], r[MAX];
void init(ll x = 1) {
if (x >= s) {
tree[x] = INF;
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
}
void update(ll x, ll a) {
x += s - 1;
tree[x] = min(tree[x], a);
x /= 2;
while (x) {
tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
x /= 2;
}
}
ll query(ll low, ll high, ll loc = 1) {
if (l[loc] == low && r[loc] == high) return tree[loc];
if (high <= r[loc * 2]) return query(low, high, loc * 2);
if (low >= l[loc * 2 + 1]) return query(low, high, loc * 2 + 1);
return min(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1));
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll M, N;
cin >> M >> N;
ll i;
for (i = 1; i <= M; i++) cin >> arr[i].a >> arr[i].b >> arr[i].c >> arr[i].d;
xpos.push_back(0), xpos.push_back(N);
xpos.push_back(1);
for (i = 1; i <= M; i++) xpos.push_back(arr[i].a), xpos.push_back(arr[i].b), xpos.push_back(arr[i].c);
sort(xpos.begin(), xpos.end());
xpos.erase(unique(xpos.begin(), xpos.end()), xpos.end());
for (i = 1; i <= M; i++) {
arr[i].a = distance(xpos.begin(), lower_bound(xpos.begin(), xpos.end(), arr[i].a));
arr[i].b = distance(xpos.begin(), lower_bound(xpos.begin(), xpos.end(), arr[i].b));
arr[i].c = distance(xpos.begin(), lower_bound(xpos.begin(), xpos.end(), arr[i].c));
}
N = xpos.size() - 1;
s = (ll)1 << (ll)ceil(log2(N));
init();
update(1, 0);
for (i = 1; i <= M; i++) {
ll res = query(arr[i].a, arr[i].b);
if (res == INF) lll[i] = INF;
else lll[i] = res + arr[i].d;
update(arr[i].c, lll[i]);
}
init();
update(N, 0);
for (i = 1; i <= M; i++) {
ll res = query(arr[i].a, arr[i].b);
if (res == INF) rrr[i] = INF;
else rrr[i] = res + arr[i].d;
update(arr[i].c, rrr[i]);
}
ll ans = INF;
for (i = 1; i <= M; i++) ans = min(ans, lll[i] + rrr[i] - arr[i].d);
if (ans >= INF) cout << -1 << ln;
else cout << ans << ln;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
748 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
748 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
748 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
748 KB |
Output is correct |
23 |
Correct |
2 ms |
748 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
18 ms |
3056 KB |
Output is correct |
26 |
Correct |
52 ms |
6268 KB |
Output is correct |
27 |
Correct |
160 ms |
14180 KB |
Output is correct |
28 |
Correct |
127 ms |
11492 KB |
Output is correct |
29 |
Correct |
111 ms |
12132 KB |
Output is correct |
30 |
Correct |
167 ms |
13020 KB |
Output is correct |
31 |
Correct |
277 ms |
23752 KB |
Output is correct |
32 |
Correct |
246 ms |
17608 KB |
Output is correct |
33 |
Correct |
30 ms |
5740 KB |
Output is correct |
34 |
Correct |
118 ms |
18276 KB |
Output is correct |
35 |
Correct |
165 ms |
36060 KB |
Output is correct |
36 |
Correct |
315 ms |
36188 KB |
Output is correct |
37 |
Correct |
248 ms |
36080 KB |
Output is correct |
38 |
Correct |
236 ms |
36048 KB |
Output is correct |