#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int T = 300002;
const long long inf = 1e18;
struct segment_tree {
int n;
vector<long long> t;
segment_tree (){
}
segment_tree (int _n){
n = _n;
t = vector<long long>(n * 4 + 1007, inf);
}
private:
void upd (int id, int l, int r, int k, long long x){
if (l == r){
t[id] = min(t[id], x);
return;
}
int m = l + r >> 1;
if (k <= m){
upd(id * 2, l, m, k, x);
} else {
upd(id * 2 + 1, m + 1, r, k, x);
}
t[id] = min(t[id * 2], t[id * 2 + 1]);
return;
}
long long get (int id, int l, int r, int u, int v){
if (l > v || r < u){
return inf;
}
if (l >= u && r <= v){
return t[id];
}
int m = l + r >> 1;
return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
public:
void upd (int k, long long x){
upd(1, 1, n, k, x);
return;
}
long long get (int l, int r){
return get(1, 1, n, l, r);
}
} t1, t2;
int n, m, a[N], b[N], c[N], d[N];
long long ans = inf;
vector<int> ns;
int pts (int x){
return lower_bound(ns.begin(), ns.end(), x) - ns.begin() + 1;
}
int32_t main (){
ios::sync_with_stdio(false); cin.tie(nullptr);
if (fopen ("test.inp", "r")){
freopen ("test.inp", "r", stdin);
freopen ("test.out", "w", stdout);
}
cin >> n >> m;
ns.push_back(1);
ns.push_back(m);
for (int i = 1; i <= n; ++i){
cin >> a[i] >> b[i] >> c[i] >> d[i];
ns.push_back(a[i]);
ns.push_back(b[i]);
ns.push_back(c[i]);
}
sort(ns.begin(), ns.end());
ns.erase(unique(ns.begin(), ns.end()), ns.end());
t1 = t2 = segment_tree(ns.size());
t1.upd(1, 0);
t2.upd(ns.size(), 0);
for (int i = 1; i <= n; ++i){
a[i] = pts(a[i]);
b[i] = pts(b[i]);
c[i] = pts(c[i]);
long long X = t1.get(a[i], b[i]);
long long Y = t2.get(a[i], b[i]);
ans = min(ans, X + Y + 1ll * d[i]);
t1.upd(c[i], X + 1ll * d[i]);
t2.upd(c[i], Y + 1ll * d[i]);
}
cout << (ans == inf ? -1 : ans) << "\n";
}
Compilation message (stderr)
pinball.cpp: In function 'int32_t main()':
pinball.cpp:70:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen ("test.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:71:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen ("test.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... |