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 int64;
const int maxn = 1e5 + 2;
int64 a[maxn], b[maxn], c[maxn], cost[maxn];
vector <int64> coord;
int64 sgt[2][12 * maxn]; // sgt[0] -> l, sgt[1] -> r;
int64 n,m;
int64 query (int x, int l, int r, int il, int ir, int idx) {
if (il <= l && r <= ir) return sgt[idx][x];
if (l > ir || r < il) return 1e15;
int mid = (l + r) / 2;
return min(query(x * 2, l, mid, il, ir, idx), query(x * 2 + 1, mid + 1, r, il, ir, idx));
}
int64 update (int x, int l, int r, int pos, int64 nval, int idx) {
if (l == pos && r == pos) return sgt[idx][x] = min(sgt[idx][x], nval);
if (l > pos || r < pos) return sgt[idx][x];
int mid = (l + r) / 2;
return sgt[idx][x] = min(update(x * 2, l, mid, pos, nval, idx), update(x * 2 + 1, mid + 1, r, pos, nval, idx));
}
int main() {
cin >> n >> m;
bool f1 = 0, f2 = 0;
for (int i = 0; i < 12 * maxn; i++){
sgt[0][i] = sgt[1][i] = 1e15;
}
for (int i = 0; i < n; i++){
cin >> a[i] >> b[i] >> c[i] >> cost[i];
if (a[i] == 1) f1 = 1;
if (b[i] == m) f2 = 1;
coord.push_back(a[i]);
coord.push_back(b[i]);
coord.push_back(c[i]);
}
if (!f1 || !f2){
cout << -1;
return 0;
}
sort(coord.begin(), coord.end());
coord.erase(unique(coord.begin(), coord.end()), coord.end());
m = 0;
for (int i = 0; i < n; i++){
a[i] = lower_bound(coord.begin(), coord.end(), a[i]) - coord.begin();
b[i] = lower_bound(coord.begin(), coord.end(), b[i]) - coord.begin();
c[i] = lower_bound(coord.begin(), coord.end(), c[i]) - coord.begin();
m = max(m, b[i]);
}
int64 rs = 1e15;
update(1, 0, n * 3, 0, 0, 0);
update(1, 0, n * 3, m, 0, 1);
for (int i = 0; i < n; i++){
rs = min(rs, query(1, 0, n * 3, a[i], b[i], 0) + query(1, 0, n * 3, a[i], b[i], 1) + cost[i]);
update(1, 0, n * 3, c[i], query(1, 0, n * 3, a[i], b[i], 0) + cost[i], 0);
update(1, 0, n * 3, c[i], query(1, 0, n * 3, a[i], b[i], 1) + cost[i], 1);
}
cout << (rs >= 1e15 ? -1: rs);
}
# | 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... |