#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);
set<int> allPts;
allPts.insert(1); allPts.insert(n);
for_(i, 0, m) {
cin >> l[i] >> r[i] >> c[i] >> cost[i];
allPts.insert(l[i]); allPts.insert(r[i]); allPts.insert(c[i]);
}
//sort(allPts.begin(), allPts.end());
unordered_map<int, int> id;
int pt = 0;
for (int i: allPts) id[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.rbegin()]+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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
416 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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
416 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 |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
416 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 |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
25 |
Correct |
28 ms |
3200 KB |
Output is correct |
26 |
Correct |
91 ms |
9220 KB |
Output is correct |
27 |
Correct |
274 ms |
17052 KB |
Output is correct |
28 |
Correct |
166 ms |
2552 KB |
Output is correct |
29 |
Correct |
181 ms |
15132 KB |
Output is correct |
30 |
Correct |
233 ms |
5240 KB |
Output is correct |
31 |
Correct |
495 ms |
28116 KB |
Output is correct |
32 |
Correct |
385 ms |
20380 KB |
Output is correct |
33 |
Correct |
52 ms |
9476 KB |
Output is correct |
34 |
Correct |
215 ms |
24640 KB |
Output is correct |
35 |
Correct |
363 ms |
48488 KB |
Output is correct |
36 |
Correct |
685 ms |
48768 KB |
Output is correct |
37 |
Correct |
503 ms |
48620 KB |
Output is correct |
38 |
Correct |
487 ms |
48616 KB |
Output is correct |