#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] = 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 |
Incorrect |
6 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
9684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |