#include <bits/stdc++.h>
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
const int N = 3e5 + 5;
int n, m;
int a[N], b[N], c[N], d[N];
struct SEG {
struct node {
long long mn = 1e18;
node() {}
node operator + (const node& o) const {
node res;
res.mn = min(mn, o.mn);
return res;
}
} it[N << 2];
SEG() {
for(int i = 0; i < (N << 2); ++i) it[i] = node();
}
void update(int k, int l, int r, int pos, long long val) {
if(l == r) {
it[k].mn = min(it[k].mn, val);
return;
}
int mid = l + r >> 1;
if(pos <= mid) update(k << 1, l, mid, pos, val);
else update(k << 1 | 1, mid + 1, r, pos, val);
it[k] = it[k << 1] + it[k <<1 | 1];
}
long long get(int k, int l, int r,int u, int v) {
if(l > v || r < u) return 1e18;
if(u <= l && r <= v) return it[k].mn;
int mid = l + r >> 1;
return min(get(k << 1, l, mid, u, v), get(k << 1 | 1, mid + 1, r, u, v));
}
} sl, sr;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
file("A") else file("task");
cin >> m >> n;
int sz;
long long ans = 1e18;
vector<int> zip;
for(int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
zip.emplace_back(a[i]);
zip.emplace_back(b[i]);
zip.emplace_back(c[i]);
}
zip.emplace_back(1);
zip.emplace_back(n);
sort(zip.begin(), zip.end());
zip.resize(unique(zip.begin(), zip.end()) - zip.begin());
sz = zip.size();
sl.update(1, 1, sz, 1, 0);
sr.update(1, 1, sz, sz, 0);
for(int i = 1; i <= m; ++i) {
a[i] = lower_bound(zip.begin(), zip.end(), a[i]) - zip.begin() + 1;
b[i] = lower_bound(zip.begin(), zip.end(), b[i]) - zip.begin() + 1;
c[i] = lower_bound(zip.begin(), zip.end(), c[i]) - zip.begin() + 1;
long long cl = sl.get(1, 1, sz, a[i], b[i]);
long long cr = sr.get(1, 1, sz, a[i], b[i]);
ans = min(ans, cl + cr + d[i]);
sl.update(1, 1, sz, c[i], cl + d[i]);
sr.update(1, 1, sz, c[i], cr + d[i]);
}
if(ans > (long long)1e17) ans = -1;
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
2 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:44:5: note: in expansion of macro 'file'
44 | file("A") else file("task");
| ^~~~
pinball.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
2 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:44:5: note: in expansion of macro 'file'
44 | file("A") else file("task");
| ^~~~
pinball.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
2 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:44:20: note: in expansion of macro 'file'
44 | file("A") else file("task");
| ^~~~
pinball.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
2 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:44:20: note: in expansion of macro 'file'
44 | file("A") else file("task");
| ^~~~
# | 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... |