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;
typedef long long ll;
typedef pair < int, int > PII;
#define F first
#define S second
#define mkp make_pair
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define left(a) a << 1
#define right(a) a << 1 | 1
const ll ool = 1e18 + 9;
const int N = 3e5 + 3, oo = 1e9 + 9;
int n, m, a[N], b[N], c[N], cost[N];
ll d[N], ans = ool;
vector < PII > vec;
struct SegTree {
ll mn[N * 4], add[N * 4];
void recalc(int v) {
mn[v] = min(mn[left(v)], mn[right(v)]);
}
void build(int v, int tl, int tr) {
add[v] = ool;
if (tl == tr - 1) {
mn[v] = ool;
return;
}
int mid = (tl + tr) >> 1;
build(left(v), tl, mid);
build(right(v), mid, tr);
recalc(v);
}
void push(int v, int tl, int tr) {
if (tl < tr - 1) {
add[left(v)] = min(add[left(v)], add[v]);
add[right(v)] = min(add[right(v)], add[v]);
}
mn[v] = min(mn[v], add[v]);
add[v] = ool;
}
void upd(int v, int tl, int tr, int ql, int qr, ll val) {
push(v, tl, tr);
if (tr <= ql || qr <= tl) return;
if (ql <= tl && tr <= qr) {
add[v] = min(add[v], val);
push(v, tl, tr);
return;
}
int mid = (tl + tr) >> 1;
upd(left(v), tl, mid, ql, qr, val);
upd(right(v), mid, tr, ql, qr, val);
recalc(v);
}
ll get(int v, int tl, int tr, int ql, int qr) {
push(v, tl, tr);
if (tr <= ql || qr <= tl) return ool;
if (ql <= tl && tr <= qr) return mn[v];
int mid = (tl + tr) >> 1;
return min(get(left(v), tl, mid, ql, qr), get(right(v), mid, tr, ql, qr));
}
} t[2];
int main() {
#ifdef krauch
freopen("input.txt", "r", stdin);
#endif
scanf("%d%d", &m, &n);
vec.eb(1, -1);
vec.eb(n, -1);
forn(i, 1, m) {
scanf("%d%d%d%d", a + i, b + i, c + i, cost + i);
cost[2 * m - i + 1] = cost[i];
vec.eb(a[i], i);
vec.eb(b[i], i + m);
vec.eb(c[i], i + 2 * m);
}
sort(all(vec));
int cnt = 0;
forn(i, 0, sz(vec) - 1) {
if (!i || vec[i].F != vec[i - 1].F) ++cnt;
if (vec[i].S == -1) continue;
if (vec[i].S <= m) a[vec[i].S] = cnt;
else if (vec[i].S <= 2 * m) b[vec[i].S - m] = cnt;
else c[vec[i].S - 2 * m] = cnt;
}
n = cnt;
t[0].build(1, 1, n + 1);
t[1].build(1, 1, n + 1);
forn(i, 1, m) {
d[i] = ool;
if (a[i] == 1) d[i] = cost[i];
/*forn(j, 1, i - 1) {
if (a[i] <= c[j] && c[j] <= b[i]) d[i] = min(d[i], d[j] + cost[i]);
}*/
d[i] = min(t[0].get(1, 1, n + 1, a[i], b[i] + 1) + cost[i], d[i]);
t[0].upd(1, 1, n + 1, c[i], c[i] + 1, d[i]);
d[2 * m - i + 1] = d[i];
a[2 * m - i + 1] = a[i];
b[2 * m - i + 1] = b[i];
c[2 * m - i + 1] = c[i];
}
forn(i, m + 1, 2 * m) {
/*forn(j, m + 1, i - 1) {
if (a[j] <= c[i] && c[i] <= b[j]) d[i] = min(d[i], d[j] + cost[i]);
}*/
d[i] = min(d[i], t[1].get(1, 1, n + 1, c[i], c[i] + 1) + cost[i]);
t[1].upd(1, 1, n + 1, a[i], b[i] + 1, d[i]);
if (b[i] == n) ans = min(ans, d[i]);
}
cout << (ans == ool ? -1 : ans) << "\n";
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:82:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &m, &n);
^
pinball.cpp:86:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", a + i, b + i, c + i, cost + i);
^
# | 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... |