This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* In the name of God, aka Allah */
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18;
const int N = 1e5 + 50;
int n, m, k, a[N][3], d[N];
pii P[N];
ll seg[2][N<<2];
void build(int c, int u=1, int tl=1, int tr=k) {
if (tl == tr) {
seg[c][u] = (tl == 1+c*(k-1)? 0: inf);
return;
}
int md = (tl+tr)>>1;
int lc = u<<1;
int rc = u<<1|1;
build(c, lc, tl, md), build(c, rc, md+1, tr);
seg[c][u] = min(seg[c][lc], seg[c][rc]);
}
void upd(int c, int id, ll x, int u=1, int tl=1, int tr=k) {
if (id < tl || tr < id) return;
if (tl == tr) {
seg[c][u] = min(seg[c][u], x);
return;
}
int md = (tl+tr)>>1;
int lc = u<<1;
int rc = u<<1|1;
upd(c, id, x, lc, tl, md), upd(c, id, x, rc, md+1, tr);
seg[c][u] = min(seg[c][lc], seg[c][rc]);
}
ll get(int c, int l, int r, int u=1, int tl=1, int tr=k) {
if (r < tl || tr < l) return inf;
if (l <= tl && tr <= r) return seg[c][u];
int md = (tl+tr)>>1;
int lc = u<<1;
int rc = u<<1|1;
return min(get(c, l, r, lc, tl, md), get(c, l, r, rc, md+1, tr));
}
void solve() {
cin >> n >> m;
if (n == 1) return cout << 0 << endl, void();
for (int i = 1; i <= n; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2] >> d[i];
P[3*i] = {a[i][0], 3*i};
P[3*i+1] = {a[i][1], 3*i+1};
P[3*i+2] = {a[i][2], 3*i+2};
}
P[1] = {1, 0};
P[2] = {m, 0};
sort(P+1, P+3*n+3);
for (int i = 1; i <= 3*n+2; i++) {
k += (P[i].F != P[i-1].F);
if (P[i].S) a[P[i].S/3][P[i].S%3] = k;
}
build(0);
build(1);
ll ans = inf;
for (int i = 1; i <= n; i++) {
ans = min(ans, get(0, a[i][0], a[i][1])+get(1, a[i][0], a[i][1])+d[i]);
upd(0, a[i][2], get(0, a[i][0], a[i][1])+d[i]);
upd(1, a[i][2], get(1, a[i][0], a[i][1])+d[i]);
}
cout << (ans >= inf? -1 : ans) << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
solve();
return 0;
}
# | 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... |