#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const ll INF = 1e18;
int rows, cols;
struct kek {
ll l;
ll r;
ll c;
ll d;
};
kek a[N];
ll aib1[3 * N], aib2[3 * N];
ll dp1[N], dp2[N];
void update1(int i, ll val) {
for (; i < 3 * N; i += i & -i) aib1[i] = min(aib1[i], val);
}
void update2(int i, ll val) {
for (; i >= 1; i -= i & -i) aib2[i] = min(aib2[i], val);
}
ll query1(int i) {
ll ans = INF;
for (; i >= 1; i -= i & -i) ans = min(ans, aib1[i]);
return ans;
}
ll query2(int i) {
ll ans = INF;
for (; i < 3 * N; i += i & -i) ans = min(ans, aib2[i]);
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> rows >> cols;
for (int i = 1; i <= rows; ++i) cin >> a[i].l >> a[i].r >> a[i].c >> a[i].d;
bool ok = 0;
for (int i = 1; i <= rows; ++i) ok |= (a[i].l == 1);
if (!ok) {
cout << "-1\n";
return 0;
}
ok = 0;
for (int i = 1; i <= rows; ++i) ok |= (a[i].r == cols);
if (!ok) {
cout << "-1\n";
return 0;
}
{
vector<vector<int>> vals;
for (int i = 1; i <= rows; ++i) {
vals.push_back({a[i].l, 0, i});
vals.push_back({a[i].r, 1, i});
vals.push_back({a[i].c, 2, i});
}
sort(vals.begin(), vals.end());
//cout << "DEBUG: " << "\n";
//for (auto it : vals) {
//cout << it[0] << " " << it[1] << " " << it[2] << "\n";
//}
int cnt = 1;
for (int i = 0; i < vals.size(); ++i) {
if (i > 0 && vals[i][0] != vals[i - 1][0]) ++cnt;
auto it = vals[i];
if (it[1] == 0) a[it[2]].l = cnt;
if (it[1] == 1) a[it[2]].r = cnt;
if (it[1] == 2) a[it[2]].c = cnt;
}
}
//cout << "DEBUG: " << "\n";
//for (int i = 1; i <= rows; ++i) {
//cout << a[i].l << " " << a[i].r << " " << a[i].c << "\n";
//}
ll mx = 0;
for (int i = 1; i <= rows; ++i) {
mx = max(mx, a[i].l);
mx = max(mx, a[i].r);
mx = max(mx, a[i].c);
dp1[i] = INF;
dp2[i] = INF;
}
for (int i = 1; i < 3 * N; ++i) {
aib1[i] = INF;
aib2[i] = INF;
}
for (int i = 1; i <= rows; ++i) {
if (a[i].l == 1) dp1[i] = a[i].d;
if (a[i].r == mx) dp2[i] = a[i].d;
}
//for (int i = 1; i <= rows; ++i) cout << dp1[i] << " " << dp2[i] << "\n";
//cout << "-------\n";
ll ans = INF;
for (int i = 1; i <= rows; ++i) {
//if (i == 3) {
//cout << query2(a[i].l) << "\n";
//}
dp1[i] = min(dp1[i], query2(a[i].l) + a[i].d);
dp2[i] = min(dp2[i], query1(a[i].r) + a[i].d);
//cout << "UPDATAM " << a[i].c << " " << dp1[i] << "\n";
update2(a[i].c, dp1[i]);
update1(a[i].c, dp2[i]);
ans = min(ans, dp1[i] + dp2[i] - a[i].d);
//cout << dp1[i] << " " << dp2[i] << "\n";
}
if (ans == INF) ans = -1;
cout << ans << endl;
return 0;
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:61:34: warning: narrowing conversion of 'a[i].kek::l' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
61 | vals.push_back({a[i].l, 0, i});
| ~~~~~^
pinball.cpp:61:34: warning: narrowing conversion of 'a[i].kek::l' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
pinball.cpp:62:34: warning: narrowing conversion of 'a[i].kek::r' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
62 | vals.push_back({a[i].r, 1, i});
| ~~~~~^
pinball.cpp:62:34: warning: narrowing conversion of 'a[i].kek::r' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
pinball.cpp:63:34: warning: narrowing conversion of 'a[i].kek::c' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
63 | vals.push_back({a[i].c, 2, i});
| ~~~~~^
pinball.cpp:63:34: warning: narrowing conversion of 'a[i].kek::c' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
pinball.cpp:71:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i = 0; i < vals.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6500 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
6608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6500 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
6608 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6500 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
6608 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
6744 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6656 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6500 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
6608 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
6744 KB |
Output is correct |
18 |
Correct |
3 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
3 ms |
6656 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6700 KB |
Output is correct |
25 |
Correct |
11 ms |
9808 KB |
Output is correct |
26 |
Correct |
29 ms |
12012 KB |
Output is correct |
27 |
Correct |
79 ms |
18840 KB |
Output is correct |
28 |
Correct |
141 ms |
30968 KB |
Output is correct |
29 |
Correct |
54 ms |
17004 KB |
Output is correct |
30 |
Correct |
146 ms |
31232 KB |
Output is correct |
31 |
Correct |
174 ms |
31080 KB |
Output is correct |
32 |
Correct |
132 ms |
30720 KB |
Output is correct |
33 |
Correct |
18 ms |
11392 KB |
Output is correct |
34 |
Correct |
63 ms |
16900 KB |
Output is correct |
35 |
Correct |
92 ms |
31920 KB |
Output is correct |
36 |
Correct |
143 ms |
30424 KB |
Output is correct |
37 |
Correct |
121 ms |
30464 KB |
Output is correct |
38 |
Correct |
126 ms |
30152 KB |
Output is correct |