#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define allit(a) (a).begin(), (a).end()
#define sz(a) ((int) (a).size())
#define cut(s) {cout << s << '\n'; return 0;}
using namespace std;
using ll = long long;
using vi = vector<int>;
namespace pd = __gnu_pbds;
template<typename K> using ordered_set = pd::tree<K, pd::null_type, less<K>, pd::rb_tree_tag, pd::tree_order_statistics_node_update>;
template<typename... T> using hash_table = pd::gp_hash_table<T...>;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
const int INF = 1e9;
const ll INFL = 1e18;
const int N = 1000;
const int M = 10;
struct device {
int left, right, center, cost;
};
ostream& operator <<(ostream& os, device& a) {
return os << "{" << a.left << "," << a.right << ", " << a.center << " $" << a.cost << "}";
}
struct SegTree {
int n;
vector<ll> tree;
SegTree(int n) : n(n), tree(2*n, INFL) {}
void update(int i, ll v) {
i += n;
tree[i] = min(tree[i], v);
for (i /= 2; i >= 1; i /= 2) tree[i] = min(tree[i*2], tree[i*2+1]);
}
ll query(int l, int r) {
l += n;
r += n;
ll res = INFL;
while (l <= r) {
if (l % 2 == 1) res = min(res, tree[l++]);
if (r % 2 == 0) res = min(res, tree[r--]);
l /= 2; r /= 2;
}
return res;
}
};
int nextPower2(int x) {
x --;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return 1+x;
}
int solve() {
int n, m;
cin >> m >> n;
vector<device> dev(m);
for (int i = 0; i < m; i++) {
cin >> dev[i].left >> dev[i].right >> dev[i].center >> dev[i].cost;
}
{
//compress
ordered_set<ll> values;
for (int i = 0; i < m; i++) {
values.insert(dev[i].left);
values.insert(dev[i].right);
values.insert(dev[i].center);
}
values.insert(1);
values.insert(n);
for (int i = 0; i < m; i++) {
dev[i].left = values.order_of_key(dev[i].left);
dev[i].right = values.order_of_key(dev[i].right);
dev[i].center = values.order_of_key(dev[i].center);
}
n = values.order_of_key(n);
}
SegTree mnLeft(nextPower2(n+1)), mnRight(nextPower2(n+1));
function<ll(int)> dpLeft = [&](int i) {
ll res = INFL;
if (dev[i].left == 0) res = (ll)dev[i].cost;
else
res = dev[i].cost + mnLeft.query(dev[i].left, dev[i].right);
mnLeft.update(dev[i].center, res);
return res;
};
function<ll(int)> dpRight = [&](int i) {
ll res = INFL;
if (dev[i].right == n) res = dev[i].cost;
else
res = dev[i].cost + mnRight.query(dev[i].left, dev[i].right);
mnRight.update(dev[i].center, res);
return res;
};
ll res = INFL;
for (int i = 0; i < m; i++) {
ll l = dpLeft(i), r = dpRight(i);
if (l < INFL && r < INFL)
res = min(res, l + r - dev[i].cost);
}
cout << (res >= INFL ? -1 : res);
return 0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
//cin >> t;
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
23 ms |
2740 KB |
Output is correct |
26 |
Correct |
54 ms |
5232 KB |
Output is correct |
27 |
Correct |
158 ms |
12136 KB |
Output is correct |
28 |
Correct |
98 ms |
2228 KB |
Output is correct |
29 |
Correct |
122 ms |
9100 KB |
Output is correct |
30 |
Correct |
153 ms |
4188 KB |
Output is correct |
31 |
Correct |
284 ms |
20824 KB |
Output is correct |
32 |
Correct |
237 ms |
11768 KB |
Output is correct |
33 |
Correct |
32 ms |
5380 KB |
Output is correct |
34 |
Correct |
141 ms |
18884 KB |
Output is correct |
35 |
Correct |
181 ms |
20828 KB |
Output is correct |
36 |
Correct |
436 ms |
37564 KB |
Output is correct |
37 |
Correct |
324 ms |
37324 KB |
Output is correct |
38 |
Correct |
298 ms |
37328 KB |
Output is correct |