This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
template < class T, T (*f) (T, T), T (*e) ()>
struct segment_tree {
vector <T> it;
int n;
void update(int u, T p) {
u += n - 1;
it[u] = f(it[u], p);
for(; u >>= 1; ) {
it[u] = f(it[u << 1], it[u << 1 | 1]);
}
}
void set(int u, T p) {
u--;
for (it[u += n] = p; u >>= 1; ) {
it[u] = f(it[u << 1], it[u << 1 | 1]);
}
}
T get(int p) {
return it[p += n - 1];
}
T get(int l, int r) {
l--;
T resl = e(), resr = e();
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if(l & 1) resl = f(resl, it[l++]);
if(r & 1) resr = f(it[--r], resr);
}
return f(resl, resr);
}
segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
fill(it.begin(), it.end(), T{});
}
};
namespace _min_ {
template <class T> T op(T a, T b) { return a < b ? a : b; }
template <class T> T e() { return numeric_limits<T>::max(); }
};
void you_make_it(void) {
int n, m; cin >> n >> m;
vector <tuple <int, int, int, int>> A(n);
vector <int> compress;
for (auto &[a, b, c, d] : A) {
cin >> a >> b >> c >> d;
compress.emplace_back(a);
compress.emplace_back(b);
compress.emplace_back(c);
}
sort(ALL(compress));
if(*compress.begin() != 1 || compress.end()[-1] != m) {
cout << -1;
return;
}
compress.erase(unique(ALL(compress)), compress.end());
m = (int) compress.size();
segment_tree <long long, _min_::op, _min_::e> L(m), R(m);
const long long INF = 1e18;
FOR(i, 1, m) L.set(i, INF), R.set(i, INF);
auto update = [&] (int &x) {
x = lower_bound(ALL(compress), x) - compress.begin() + 1;
};
long long ans = INF;
for (auto &[a, b, c, d] : A) {
update(a), update(b), update(c);
if(a == 1 && b == m) {
minimize(ans, d);
continue;
}
if(a == 1) {
auto f = R.get(a, b);
minimize(ans, f + d);
R.update(c, f + d);
L.update(c, d);
} else if(b == m) {
auto f = L.get(a, b);
minimize(ans, f + d);
L.update(c, f + d);
R.update(c, d);
} else {
auto l = L.get(a, b), r = R.get(a, b);
minimize(ans, l + r + d);
L.update(c, l + d);
R.update(c, r + d);
}
}
cout << (ans >= INF ? -1 : ans);
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
pinball.cpp: In instantiation of 'segment_tree<T, f, e>::segment_tree(int) [with T = long long int; T (* f)(T, T) = _min_::op<long long int>; T (* e)() = _min_::e<long long int>]':
pinball.cpp:79:54: required from here
pinball.cpp:21:9: warning: 'segment_tree<long long int, _min_::op<long long int>, _min_::e<long long int> >::n' will be initialized after [-Wreorder]
21 | int n;
| ^
pinball.cpp:20:16: warning: 'std::vector<long long int, std::allocator<long long int> > segment_tree<long long int, _min_::op<long long int>, _min_::e<long long int> >::it' [-Wreorder]
20 | vector <T> it;
| ^~
pinball.cpp:52:5: warning: when initialized here [-Wreorder]
52 | segment_tree(int n = 0) : n(n), it(n << 1 | 1) {
| ^~~~~~~~~~~~
# | 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... |