#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
ll INF = 1e15;
class SegmentTree {
private:
vector<ll> tree; int n;
ll bound = INF;
ll merge(ll a, ll b) {
return min(a, b);
}
void update(int i, ll val, int l, int r, int p) {
if (l > i or r < i) return;
if (l == r) {
tree[p] = min(tree[p], val);
return;
}
int mid = (l + r) / 2;
int c1 = 2*p+1, c2 = 2*p+2;
update(i, val, l, mid, c1); update(i, val, mid+1, r, c2);
tree[p] = merge(tree[c1], tree[c2]);
}
ll mn(int i, int j, int l, int r, int p) {
if (l > j or r < i) return bound;
if (l >= i and r <= j) return tree[p];
int mid = (l + r) / 2;
int c1 = 2*p+1, c2 = 2*p+2;
return merge(mn(i, j, l, mid, c1), mn(i, j, mid+1, r, c2));
}
public:
SegmentTree(int N) {
n = N;
tree.resize(4*n, INF);
}
ll mn(int i, int j) {
return mn(i, j, 0, n-1, 0);
}
void update(int i, ll val) {
update(i, val, 0, n-1, 0);
}
};
int main() {
#ifndef ONLINE_JUDGE
//freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int m, n; cin >> m >> n;
vector<ll> cost(m); vi l(m), r(m), c(m);
vi allPts;
allPts.push_back(1); allPts.push_back(n);
for_(i, 0, m) {
cin >> l[i] >> r[i] >> c[i] >> cost[i];
allPts.push_back(l[i]); allPts.push_back(r[i]); allPts.push_back(c[i]);
}
sort(allPts.begin(), allPts.end());
unordered_map<int, int> id;
int pt = 0;
for_(i, 0, allPts.size()) if (i == 0 or allPts[i] != allPts[i-1]) id[allPts[i]] = pt++;
for_(i, 0, m) {
l[i] = id[l[i]]; r[i] = id[r[i]]; c[i] = id[c[i]];
}
int pointCount = id[allPts.back()]+1;
SegmentTree rCost(pointCount), lCost(pointCount);
ll ans = INF;
for_(i, 0, m) {
ll lc = lCost.mn(l[i], r[i]), rc = rCost.mn(l[i], r[i]);
if (r[i] == pointCount-1) rc = 0;
if (l[i] == 0) lc = 0;
if (lc != INF) lCost.update(c[i], lc+cost[i]);
if (rc != INF) rCost.update(c[i], rc+cost[i]);
ans = min(ans, lc+rc+cost[i]);
}
cout << (ans == INF ? -1 : ans) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
288 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
25 |
Correct |
22 ms |
2816 KB |
Output is correct |
26 |
Correct |
65 ms |
7792 KB |
Output is correct |
27 |
Correct |
196 ms |
15600 KB |
Output is correct |
28 |
Correct |
160 ms |
8048 KB |
Output is correct |
29 |
Correct |
132 ms |
13424 KB |
Output is correct |
30 |
Correct |
202 ms |
9324 KB |
Output is correct |
31 |
Correct |
332 ms |
25352 KB |
Output is correct |
32 |
Correct |
290 ms |
19696 KB |
Output is correct |
33 |
Correct |
42 ms |
7680 KB |
Output is correct |
34 |
Correct |
151 ms |
19860 KB |
Output is correct |
35 |
Correct |
293 ms |
39576 KB |
Output is correct |
36 |
Correct |
403 ms |
39576 KB |
Output is correct |
37 |
Correct |
343 ms |
39576 KB |
Output is correct |
38 |
Correct |
343 ms |
39604 KB |
Output is correct |