// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC optimize("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(xyxy, yxy) (xyxy) = max((xyxy), (yxy))
#define smin(xyxy, yxy) (xyxy) = min((xyxy), (yxy))
#define all(xyxy) (xyxy).begin(), (xyxy).end()
#define len(xyxy) ((int)(xyxy).size())
const int maxn = 1e5 + 5;
const ll inf = 1e15 + 7;
int n, m, a[maxn], b[maxn], c[maxn], d[maxn];
vector<int> vec;
pll node[maxn * 4 * 3];
void build(int l = 0, int r = n, int id = 1){
if(l == r - 1){
if(l == 0) node[id] = mp(0, inf);
else if(l == n - 1) node[id] = mp(inf, 0);
else node[id] = mp(inf, inf);
return;
}
int mid = (l + r) / 2;
build(l, mid, id * 2);
build(mid, r, id * 2 + 1);
node[id] = mp(min(node[id * 2].F, node[id * 2 + 1].F), min(node[id * 2].S, node[id * 2 + 1].S));
}
pll get(int s, int f, int l = 0, int r = n, int id = 1){
if(s <= l && r <= f) return node[id];
if(f <= l || r <= s) return mp(inf, inf);
int mid = (l + r) / 2;
pll x = get(s, f, l, mid, id * 2), y = get(s, f, mid, r, id * 2 + 1);
return mp(min(x.F, y.F), min(x.S, y.S));
}
void upd(int p, pll x, int l = 0, int r = n, int id = 1){
if(l == r - 1){
smin(node[id].F, x.F);
smin(node[id].S, x.S);
return;
}
int mid = (l + r) / 2;
if(p < mid) upd(p, x, l, mid, id * 2);
else upd(p, x, mid, r, id * 2 + 1);
node[id] = mp(min(node[id * 2].F, node[id * 2 + 1].F), min(node[id * 2].S, node[id * 2 + 1].S));
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> m >> n;
ll mx = 0, mn = inf;
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
vec.pb(a[i]);
vec.pb(c[i]);
vec.pb(b[i]);
smin(mn, 1ll * a[i]);
smax(mx, 1ll * b[i]);
}
if(mx != n || mn != 1){
cout << -1 << '\n';
return 0;
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
n = vec.size();
if(n == 1){
cout << 0 << '\n';
return 0;
}
for(int i = 0; i < m; i++){
a[i] = lower_bound(all(vec), a[i]) - vec.begin();
b[i] = lower_bound(all(vec), b[i]) - vec.begin();
c[i] = lower_bound(all(vec), c[i]) - vec.begin();
}
build();
ll ans = inf;
for(int i = 0; i < m; i++){
pll x = get(a[i], b[i] + 1);
smin(ans, d[i] + x.F + x.S);
upd(c[i], mp(x.F + d[i], x.S + d[i]));
}
if(ans < inf) cout << ans << '\n';
else cout << -1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
480 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
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 |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
480 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
480 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
540 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
480 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
540 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
12 ms |
3164 KB |
Output is correct |
26 |
Correct |
30 ms |
4304 KB |
Output is correct |
27 |
Correct |
84 ms |
9304 KB |
Output is correct |
28 |
Correct |
85 ms |
7436 KB |
Output is correct |
29 |
Correct |
61 ms |
8136 KB |
Output is correct |
30 |
Correct |
104 ms |
9412 KB |
Output is correct |
31 |
Correct |
126 ms |
15560 KB |
Output is correct |
32 |
Correct |
122 ms |
11272 KB |
Output is correct |
33 |
Correct |
18 ms |
3796 KB |
Output is correct |
34 |
Correct |
60 ms |
12236 KB |
Output is correct |
35 |
Correct |
91 ms |
23748 KB |
Output is correct |
36 |
Correct |
137 ms |
23756 KB |
Output is correct |
37 |
Correct |
120 ms |
23684 KB |
Output is correct |
38 |
Correct |
117 ms |
23748 KB |
Output is correct |