#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define F first
#define S second
#define pii pair<int, int>
#define all(A) A.begin(), A.end()
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAXN = 2e5 + 10;
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int mod = 998244353;
const int SQ = 1000;
const int LG = 63;
///////////////////////////////////////
struct baze
{
int l;
int r;
int c;
int d;
};
baze a[MAXN];
int shokri1[3 * MAXN], shokri2[3 * MAXN], dp1[MAXN], dp2[MAXN], rows, cols;
void upd1(int i, int val)
{
for (; i < 3 * MAXN; i += i & -i)
shokri1[i] = min(shokri1[i], val);
}
void upd2(int i, int val)
{
for (; i >= 1; i -= i & -i)
shokri2[i] = min(shokri2[i], val);
}
int que1(int i)
{
int ans = INF;
for (; i >= 1; i -= i & -i)
ans = min(ans, shokri1[i]);
return ans;
}
int que2(int i)
{
int ans = INF;
for (; i < 3 * MAXN; i += i & -i)
ans = min(ans, shokri2[i]);
return ans;
}
signed main()
{
fast;
cin >> rows >> cols;
for (int i = 1; i <= rows; ++i)
cin >> a[i].l >> a[i].r >> a[i].c >> a[i].d;
bool flag = false;
for (int i = 1; i <= rows; ++i)
flag |= (a[i].l == 1);
if (!flag)
return cout << -1, 0;
flag = false;
for (int i = 1; i <= rows; ++i)
flag |= (a[i].r == cols);
if (!flag)
return cout << -1, 0;
{
vector<vector<int>> vec;
for (int i = 1; i <= rows; ++i)
vec.pb({a[i].l, 0, i}), vec.pb({a[i].r, 1, i}), vec.pb({a[i].c, 2, i});
sort(all(vec));
int cnt = 1;
for (int i = 0; i < vec.size(); ++i)
{
if (i > 0 && vec[i][0] != vec[i - 1][0])
++cnt;
auto it = vec[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;
}
}
int 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 * MAXN; ++i)
shokri1[i] = INF, shokri2[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;
}
int ans = INF;
for (int i = 1; i <= rows; ++i)
{
dp1[i] = min(dp1[i], que2(a[i].l) + a[i].d);
dp2[i] = min(dp2[i], que1(a[i].r) + a[i].d);
upd2(a[i].c, dp1[i]);
upd1(a[i].c, dp2[i]);
ans = min(ans, dp1[i] + dp2[i] - a[i].d);
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:83:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < vec.size(); ++i)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14876 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14876 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
1 ms |
2472 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
2 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
3 ms |
14684 KB |
Output is correct |
10 |
Correct |
3 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14876 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
1 ms |
2472 KB |
Output is correct |
17 |
Correct |
3 ms |
14936 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
2 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14936 KB |
Output is correct |
25 |
Correct |
11 ms |
16052 KB |
Output is correct |
26 |
Correct |
26 ms |
18208 KB |
Output is correct |
27 |
Correct |
75 ms |
25948 KB |
Output is correct |
28 |
Correct |
119 ms |
30056 KB |
Output is correct |
29 |
Correct |
66 ms |
21424 KB |
Output is correct |
30 |
Correct |
124 ms |
30324 KB |
Output is correct |
31 |
Correct |
116 ms |
30228 KB |
Output is correct |
32 |
Correct |
116 ms |
30716 KB |
Output is correct |
33 |
Correct |
17 ms |
17456 KB |
Output is correct |
34 |
Correct |
55 ms |
21428 KB |
Output is correct |
35 |
Correct |
92 ms |
30208 KB |
Output is correct |
36 |
Correct |
115 ms |
30196 KB |
Output is correct |
37 |
Correct |
108 ms |
30204 KB |
Output is correct |
38 |
Correct |
103 ms |
30472 KB |
Output is correct |