#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long ll;
constexpr int LIM = 1e5;
constexpr int base = (1 << 19);
int lft[LIM + 10];
int rgt[LIM + 10];
int mid[LIM + 10];
int cost[LIM + 10];
ll dp[LIM + 10][2];
ll tri[2 * base];
map<int, int>con;
void reset() {
for(int i = 0; i < 2 * base; i++) {
tri[i] = 1e18;
}
}
void upd(int v, ll x) {
v += base;
while(v > 0) {
tri[v] = min(tri[v], x);
v /= 2;
}
}
ll que(int l, int r) {
l += base;
r += base;
ll ans = 1e18;
while(l <= r) {
if(l & 1) {
ans = min(ans, tri[l]);
}
if(!(r & 1)) {
ans = min(ans, tri[r]);
}
l = (l + 1) / 2;
r = (r - 1) / 2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> lft[i] >> rgt[i] >> mid[i] >> cost[i];
con[lft[i]] = 0;
con[rgt[i]] = 0;
con[mid[i]] = 0;
}
con[1] = 0;
con[m] = 0;
int k = 1;
for(auto x : con) {
con[x.st] = k++;
}
reset();
upd(1, 0);
for(int i = 1; i <= n; i++) {
dp[i][0] = que(con[lft[i]], con[rgt[i]]) + cost[i];
upd(con[mid[i]], dp[i][0]);
}
reset();
upd(con.size(), 0);
for(int i = 1; i <= n; i++) {
dp[i][1] = que(con[lft[i]], con[rgt[i]]) + cost[i];
upd(con[mid[i]], dp[i][1]);
}
ll ans = 1e18;
for(int i = 1; i <= n; i++) {
//cout << dp[i][0] << ' ' << dp[i][1] << '\n';
ans = min(ans, dp[i][0] + dp[i][1] - cost[i]);
}
if(ans == 1e18) {
cout << -1 << '\n';
}
else {
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
7 ms |
8520 KB |
Output is correct |
3 |
Correct |
6 ms |
8552 KB |
Output is correct |
4 |
Correct |
5 ms |
8580 KB |
Output is correct |
5 |
Correct |
5 ms |
8504 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
7 ms |
8520 KB |
Output is correct |
3 |
Correct |
6 ms |
8552 KB |
Output is correct |
4 |
Correct |
5 ms |
8580 KB |
Output is correct |
5 |
Correct |
5 ms |
8504 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8532 KB |
Output is correct |
11 |
Correct |
5 ms |
8528 KB |
Output is correct |
12 |
Correct |
7 ms |
8532 KB |
Output is correct |
13 |
Correct |
6 ms |
8492 KB |
Output is correct |
14 |
Correct |
6 ms |
8584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
7 ms |
8520 KB |
Output is correct |
3 |
Correct |
6 ms |
8552 KB |
Output is correct |
4 |
Correct |
5 ms |
8580 KB |
Output is correct |
5 |
Correct |
5 ms |
8504 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8532 KB |
Output is correct |
11 |
Correct |
5 ms |
8528 KB |
Output is correct |
12 |
Correct |
7 ms |
8532 KB |
Output is correct |
13 |
Correct |
6 ms |
8492 KB |
Output is correct |
14 |
Correct |
6 ms |
8584 KB |
Output is correct |
15 |
Correct |
4 ms |
8532 KB |
Output is correct |
16 |
Correct |
6 ms |
8532 KB |
Output is correct |
17 |
Correct |
6 ms |
8668 KB |
Output is correct |
18 |
Correct |
6 ms |
8536 KB |
Output is correct |
19 |
Correct |
8 ms |
8660 KB |
Output is correct |
20 |
Correct |
7 ms |
8660 KB |
Output is correct |
21 |
Correct |
6 ms |
8660 KB |
Output is correct |
22 |
Correct |
8 ms |
8660 KB |
Output is correct |
23 |
Correct |
6 ms |
8660 KB |
Output is correct |
24 |
Correct |
6 ms |
8744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
7 ms |
8520 KB |
Output is correct |
3 |
Correct |
6 ms |
8552 KB |
Output is correct |
4 |
Correct |
5 ms |
8580 KB |
Output is correct |
5 |
Correct |
5 ms |
8504 KB |
Output is correct |
6 |
Correct |
5 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8532 KB |
Output is correct |
9 |
Correct |
5 ms |
8532 KB |
Output is correct |
10 |
Correct |
5 ms |
8532 KB |
Output is correct |
11 |
Correct |
5 ms |
8528 KB |
Output is correct |
12 |
Correct |
7 ms |
8532 KB |
Output is correct |
13 |
Correct |
6 ms |
8492 KB |
Output is correct |
14 |
Correct |
6 ms |
8584 KB |
Output is correct |
15 |
Correct |
4 ms |
8532 KB |
Output is correct |
16 |
Correct |
6 ms |
8532 KB |
Output is correct |
17 |
Correct |
6 ms |
8668 KB |
Output is correct |
18 |
Correct |
6 ms |
8536 KB |
Output is correct |
19 |
Correct |
8 ms |
8660 KB |
Output is correct |
20 |
Correct |
7 ms |
8660 KB |
Output is correct |
21 |
Correct |
6 ms |
8660 KB |
Output is correct |
22 |
Correct |
8 ms |
8660 KB |
Output is correct |
23 |
Correct |
6 ms |
8660 KB |
Output is correct |
24 |
Correct |
6 ms |
8744 KB |
Output is correct |
25 |
Correct |
33 ms |
9840 KB |
Output is correct |
26 |
Correct |
109 ms |
12804 KB |
Output is correct |
27 |
Correct |
297 ms |
18092 KB |
Output is correct |
28 |
Correct |
124 ms |
15432 KB |
Output is correct |
29 |
Correct |
160 ms |
16228 KB |
Output is correct |
30 |
Correct |
215 ms |
16468 KB |
Output is correct |
31 |
Correct |
467 ms |
23316 KB |
Output is correct |
32 |
Correct |
393 ms |
21224 KB |
Output is correct |
33 |
Correct |
44 ms |
12588 KB |
Output is correct |
34 |
Correct |
243 ms |
19080 KB |
Output is correct |
35 |
Correct |
203 ms |
29588 KB |
Output is correct |
36 |
Correct |
533 ms |
29648 KB |
Output is correct |
37 |
Correct |
448 ms |
29640 KB |
Output is correct |
38 |
Correct |
393 ms |
29584 KB |
Output is correct |