#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;
#define N 200005
#define ALL(x) x.begin(), x.end()
int m, n;
struct pinball { ll a, b, c, d; } a[N];
int compress()
{
vector<ll> crd = {1,n};
for (int i = 1; i <= m; ++i) crd.push_back(a[i].a), crd.push_back(a[i].b), crd.push_back(a[i].c);
sort(ALL(crd));
crd.erase(unique(ALL(crd)), crd.end());
for (int i = 1; i <= m; ++i) a[i].a = lower_bound(ALL(crd), a[i].a) - crd.begin(),
a[i].b = lower_bound(ALL(crd), a[i].b) - crd.begin(),
a[i].c = lower_bound(ALL(crd), a[i].c) - crd.begin();
return crd.size();
}
struct min_segtree
{
vector<ll> t;
min_segtree(int n) : t(n<<1, 1e18) {};
ll qry(int l, int r)
{
ll z = 1e18;
for (l += n, r += n + 1; l < r; l/=2,r/=2)
{
if(l&1)z=min(z,t[l++]);
if(r&1)z=min(t[--r],z);
}
return z;
}
void set(int p, ll k) { for (p+=n,t[p]=min(t[p],k);p/=2;)t[p]=min(t[p*2], t[p*2+1]); }
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> m >> n;
for (int i = 1; i <= m; ++i) cin >> a[i].a >> a[i].b >> a[i].c >> a[i].d;
n = compress();
min_segtree tl(n), tr(n);
ll z = 1e18;
tl.set(0, 0);
tr.set(n-1, 0);
for (int i = 1; i <= m; ++i)
{
z = min(z, tl.qry(a[i].a, a[i].b) + tr.qry(a[i].a, a[i].b) + a[i].d);
tl.set(a[i].c, tl.qry(a[i].a, a[i].b) + a[i].d);
tr.set(a[i].c, tr.qry(a[i].a, a[i].b) + a[i].d);
}
cout << (z < 1e18? z:-1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
472 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 |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
472 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
472 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
472 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
12 ms |
3572 KB |
Output is correct |
26 |
Correct |
36 ms |
5312 KB |
Output is correct |
27 |
Correct |
97 ms |
8284 KB |
Output is correct |
28 |
Correct |
93 ms |
13032 KB |
Output is correct |
29 |
Correct |
70 ms |
7488 KB |
Output is correct |
30 |
Correct |
116 ms |
14284 KB |
Output is correct |
31 |
Correct |
154 ms |
14540 KB |
Output is correct |
32 |
Correct |
141 ms |
13516 KB |
Output is correct |
33 |
Correct |
16 ms |
5376 KB |
Output is correct |
34 |
Correct |
63 ms |
9448 KB |
Output is correct |
35 |
Correct |
87 ms |
18136 KB |
Output is correct |
36 |
Correct |
150 ms |
17944 KB |
Output is correct |
37 |
Correct |
110 ms |
17968 KB |
Output is correct |
38 |
Correct |
104 ms |
17944 KB |
Output is correct |