#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const ll oo = 1e18;
const int T = 12e5 + 1;
const int MAXM = 1e5 + 1;
int m, n, a[MAXM], b[MAXM], c[MAXM], w[MAXM];
ll tr[T], d[MAXM], d1[MAXM], rs = oo;
vi v;
void upd(int i, int l, int r, int q, ll v) {
if (r < q || l > q) return;
if (l == r) {
tr[i] = min(tr[i], v);
return;
}
int mid = l + (r - l) / 2;
upd(i * 2, l, mid, q, v);
upd(i * 2 + 1, mid + 1, r, q, v);
tr[i] = min(tr[i * 2], tr[i * 2 + 1]);
}
ll get(int i, int l, int r, int ql, int qr) {
if (r < ql || l > qr) return oo;
if (ql <= l && r <= qr) return tr[i];
int mid = l + (r - l) / 2;
return min(get(i * 2, l, mid, ql, qr), get(i * 2 + 1, mid + 1, r, ql, qr));
}
int getpos(int x) { return lb(all(v), x) - v.bg(); }
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> m >> n;
v.pb(0);
FOR(i, 1, m) {
cin >> a[i] >> b[i] >> c[i] >> w[i];
v.pb(a[i]);
v.pb(b[i]);
v.pb(c[i]);
}
sort(all(v));
FOR(i, 1, T - 1) tr[i] = oo;
FOR(i, 1, m) {
d[i] = oo;
if (a[i] == 1) d[i] = w[i];
int pa = getpos(a[i]), pb = getpos(b[i]);
d[i] = min(d[i], get(1, 1, sz(v) - 1, pa, pb) + w[i]);
upd(1, 1, sz(v) - 1, getpos(c[i]), d[i]);
}
FOR(i, 1, T - 1) tr[i] = oo;
FOR(i, 1, m) {
d1[i] = oo;
if (b[i] == n) d1[i] = w[i];
int pa = getpos(a[i]), pb = getpos(b[i]);
d1[i] = min(d1[i], get(1, 1, sz(v) - 1, pa, pb) + w[i]);
upd(1, 1, sz(v) - 1, getpos(c[i]), d1[i]);
}
FOR(i, 1, m) rs = min(rs, d[i] + d1[i] - w[i]);
cout << (rs < oo? rs : -1);
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:81:5: note: in expansion of macro 'FOR'
81 | FOR(i, 1, m) {
| ^~~
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:88:5: note: in expansion of macro 'FOR'
88 | FOR(i, 1, T - 1) tr[i] = oo;
| ^~~
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:89:5: note: in expansion of macro 'FOR'
89 | FOR(i, 1, m) {
| ^~~
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:96:5: note: in expansion of macro 'FOR'
96 | FOR(i, 1, T - 1) tr[i] = oo;
| ^~~
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:97:5: note: in expansion of macro 'FOR'
97 | FOR(i, 1, m) {
| ^~~
pinball.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
pinball.cpp:104:5: note: in expansion of macro 'FOR'
104 | FOR(i, 1, m) rs = min(rs, d[i] + d1[i] - w[i]);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9696 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9696 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9668 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
9816 KB |
Output is correct |
19 |
Correct |
10 ms |
9812 KB |
Output is correct |
20 |
Correct |
9 ms |
9816 KB |
Output is correct |
21 |
Correct |
9 ms |
9684 KB |
Output is correct |
22 |
Correct |
7 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9744 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9696 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9668 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
9816 KB |
Output is correct |
19 |
Correct |
10 ms |
9812 KB |
Output is correct |
20 |
Correct |
9 ms |
9816 KB |
Output is correct |
21 |
Correct |
9 ms |
9684 KB |
Output is correct |
22 |
Correct |
7 ms |
9812 KB |
Output is correct |
23 |
Correct |
7 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
9812 KB |
Output is correct |
25 |
Correct |
26 ms |
10476 KB |
Output is correct |
26 |
Correct |
64 ms |
11776 KB |
Output is correct |
27 |
Correct |
188 ms |
15400 KB |
Output is correct |
28 |
Correct |
280 ms |
17976 KB |
Output is correct |
29 |
Correct |
134 ms |
13888 KB |
Output is correct |
30 |
Correct |
278 ms |
17968 KB |
Output is correct |
31 |
Correct |
302 ms |
17964 KB |
Output is correct |
32 |
Correct |
295 ms |
17992 KB |
Output is correct |
33 |
Correct |
42 ms |
11340 KB |
Output is correct |
34 |
Correct |
132 ms |
13936 KB |
Output is correct |
35 |
Correct |
178 ms |
17976 KB |
Output is correct |
36 |
Correct |
301 ms |
17976 KB |
Output is correct |
37 |
Correct |
248 ms |
17972 KB |
Output is correct |
38 |
Correct |
241 ms |
17904 KB |
Output is correct |