#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
struct SegmentTree {
int n;
vector <ll> t;
void modify(int id, ll val, int l, int r, int v) {
if (l == r) {
t[v] = min(t[v], val);
} else {
int m = (l + r) >> 1;
if (id <= m) modify(id, val, l, m, 2 * v + 1);
else modify(id, val, m + 1, r, 2 * v + 2);
t[v] = min(t[2 * v + 1], t[2 * v + 2]);
}
}
ll get(int ql, int qr, int l, int r, int v) {
if (qr < l || r < ql) return Inf;
if (ql <= l && r <= qr) return t[v];
int m = (l + r) >> 1;
return min(get(ql, qr, l, m, 2 * v + 1), get(ql, qr, m + 1, r, 2 * v + 2));
}
SegmentTree(int nn) {
n = nn;
t.resize(4 * n, Inf);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> a(n), b(n), c(n), d(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
{
vector <int> cmp;
for (int i = 0; i < n; ++i) {
cmp.push_back(a[i]);
cmp.push_back(b[i]);
cmp.push_back(c[i]);
}
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
int m = cmp.size();
auto go = [&] (int &x) {
x = lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin() + 1;
};
for (int i = 0; i < n; ++i) {
go(a[i]);
go(b[i]);
go(c[i]);
}
}
vector <ll> dpL(n, Inf), dpR(n, Inf);
SegmentTree trL(m + 1), trR(m + 1);
ll ans = Inf;
for (int i = 0; i < n; ++i) {
ll costL = trL.get(a[i], b[i], 0, m, 0), costR = trR.get(a[i], b[i], 0, m, 0);
if (a[i] == 1) costL = 0;
if (b[i] == m) costR = 0;
ans = min(ans, costL + costR + d[i]);
if (a[i] == 1) {
dpL[i] = d[i];
} else {
dpL[i] = min(dpL[i], d[i] + trL.get(a[i], b[i], 0, m, 0));
}
if (b[i] == m) {
dpR[i] = d[i];
} else {
dpR[i] = min(dpR[i], d[i] + trR.get(a[i], b[i], 0, m, 0));
}
//cout << c[i] << ' ' << dpL[i] << ' ' << dpR[i] << endl;
trL.modify(c[i], dpL[i], 0, m, 0);
trR.modify(c[i], dpR[i], 0, m, 0);
}
if (ans != Inf) cout << ans << '\n';
else cout << "-1\n";
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:71:9: warning: unused variable 'm' [-Wunused-variable]
int m = cmp.size();
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |