#include <bits/stdc++.h>
/*
Brute to win
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int m, n, sz;
struct item {
int l, r, x, c;
};
item a[N];
vector <int> comp;
struct ST {
vector <int> st;
ST(int _n) {
st.assign(4 * (_n + 1), inf);
}
void update(int i, int l, int r, int pos, int val) {
if (l == r) {
st[i] = min(st[i], val);
return;
}
int mid = (l + r) / 2;
if (pos <= mid) update(2 * i, l, mid, pos, val);
else update(2 * i + 1, mid + 1, r, pos, val);
st[i] = min(st[2 * i], st[2 * i + 1]);
}
int get(int i, int l, int r, int u, int v) {
if (u > r || v < l) return inf;
if (u <= l && r <= v) return st[i];
int mid = (l + r) / 2;
return min(get(2 * i, l, mid, u, v), get(2 * i + 1, mid + 1, r, u, v));
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> m >> n;
comp.push_back(1);
comp.push_back(n);
for (int i = 1; i <= m; i++) {
cin >> a[i].l >> a[i].r >> a[i].x >> a[i].c;
comp.push_back(a[i].l);
comp.push_back(a[i].r);
comp.push_back(a[i].x);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
sz = comp.size();
for (int i = 1; i <= m; i++) {
a[i].l = lower_bound(comp.begin(), comp.end(), a[i].l) - comp.begin() + 1;
a[i].r = lower_bound(comp.begin(), comp.end(), a[i].r) - comp.begin() + 1;
a[i].x = lower_bound(comp.begin(), comp.end(), a[i].x) - comp.begin() + 1;
}
ST st(sz), st1(sz);
st.update(1, 1, sz, 1, 0);
st1.update(1, 1, sz, sz, 0);
int ans = inf;
for (int i = 1; i <= m; i++) {
auto val = st.get(1, 1, sz, a[i].l, a[i].r);
if (val < inf) st.update(1, 1, sz, a[i].x, val + a[i].c);
auto val1 = st1.get(1, 1, sz, a[i].l, a[i].r);
if (val1 < inf) st1.update(1, 1, sz, a[i].x, val1 + a[i].c);
if (val < inf && val1 < inf) ans = min(ans, val + val1 + a[i].c);
}
cout << (ans >= inf ? -1 : ans);
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
pinball.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |