#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 305;
#define int ll
const int inf = 1ll << 55;
int R, C, n;
vector<pair<int,int>> loc;
int pre_ly[MAX_N][MAX_N], pre_ry[MAX_N][MAX_N];
void pre_proc() {
for (int i = 0;i < n;++i) {
auto [x, y] = loc[i];
set<int> ys;
ys.insert(-inf);
ys.insert(inf);
for (int j = i+1;j < n;++j) {
auto [cx, cy] = loc[j];
if (cx == x) continue;
auto it = ys.lower_bound(cy);
int ly = *prev(it), ry = *it; ys.insert(cy);
pre_ly[i][j] = ly, pre_ry[i][j] = ry;
}
}
}
int cal_up(int ul) {
int dnmn = 0;
{
vector<int> ys;
for (auto [x, y] : loc)
ys.pb(y - ul);
sort(AI(ys));
int lst_end = 0;
for (int y : ys) {
chmax(dnmn, y - 1 - lst_end);
lst_end = y + ul;
}
chmax(dnmn, C - lst_end);
}
vector<vector<tuple<int,int,int>>> must;
vector<tuple<int,int,int>> op2;
vector<int> sep;
sep.reserve(n * n * 5);
DE(ul);
auto must_op = [&]() {
must.pb();
must.back().reserve(n * n * 5);
//sort(AI(loc));
for (int i = 0;i < n;++i) {
auto [x, y] = loc[i];
auto allmx = [&](int l, int r, int v) {
chmax(l, dnmn);
chmin(r, C);
if (l > r) return;
sep.pb(l), sep.pb(r+1);
must.back().pb(l, r+1, v - x - 1);
};
int nl = 1, nr = C;
for (int j = i+1;j < n;++j) {
auto [cx, cy] = loc[j];
if (cx == x) continue;
if (cy <= y) chmax(nl, cy + 1);
if (cy >= y) chmin(nr, cy - ul - 1);
}
if (nl > nr) continue;
if (nl == 1) allmx(0, C, R + 1);
else if (nl <= nr) allmx(0, nr - nl, R + 1);
}
};
auto all_op = [&]() {
for (int i = 0;i < n;++i) {
auto [x, y] = loc[i];
auto segmx = [&](int l, int r, int v) {
chmax(l, dnmn);
chmin(r, C);
if (l > r) return;
sep.pb(l), sep.pb(r+1);
op2.pb(l, r+1, v - x - 1);
};
for (int j = i+1;j < n;++j) {
auto [cx, cy] = loc[j];
if (cx == x) continue;
int ly = pre_ly[i][j], ry = pre_ry[i][j];
int s = 0, e = min(C - ly - 1, (ry - ul - 1) - ly - 1);
if (cy >= y) chmax(s, cy - ul - y);
else chmax(s, y - ul - cy);
if (y <= ly || ry <= y || s > e || ry == cy || ry - ul <= 1) continue;
segmx(s, e, cx);
}
}
};
all_op();
must_op();
for (auto &[x, y] : loc)
x = R - x + 1;
reverse(AI(loc));
must_op();
for (auto &[x, y] : loc)
x = R - x + 1;
reverse(AI(loc));
sep.pb(C+1);
sort(AI(sep)); sep.erase(unique(AI(sep)), end(sep));
priority_queue<pair<int,int>> pq, pq2[2];
pq.emplace(0, inf);
pq2[0].emplace(0, inf);
pq2[1].emplace(0, inf);
sort(AI(op2));
sort(AI(must[0]));
sort(AI(must[1]));
int h{}, h2[2]{};
int res = inf;
for (int i = 0;i+1 < sep.size();++i) {
int M = sep[i];
auto add_in = [&](auto &pq, auto &op, int &i) {
for (;i < op.size();++i) {
auto [l, r, v] = op[i];
if (l > M) break;
pq.emplace(v, r);
}
};
auto pops = [&](auto &pq) -> ll{
while (pq.top().second <= sep[i])
pq.pop();
return pq.top().first;
};
add_in(pq, op2, h);
add_in(pq2[0], must[0], h2[0]);
add_in(pq2[1], must[1], h2[1]);
chmin(res, sep[i] + ul +
max(pops(pq), pops(pq2[0]) + pops(pq2[1])));
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> R >> C >> n; loc.resize(n);
for (auto &[x, y] : loc) {
cin >> x >> y;
}
sort(AI(loc));
int res = inf;
vector<int> ys;
for (auto [x, y] : loc) ys.pb(y);
sort(AI(ys)); ys.erase(unique(AI(ys)), end(ys));
pre_proc();
for (int y : ys) {
int cur = cal_up(y - 1);
chmin(res, cur);
}
cout << res << '\n';
}
Compilation message
cultivation.cpp: In function 'll cal_up(ll)':
cultivation.cpp:15:17: warning: statement has no effect [-Wunused-value]
15 | #define DE(...) 0
| ^
cultivation.cpp:70:2: note: in expansion of macro 'DE'
70 | DE(ul);
| ^~
cultivation.cpp:159:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for (int i = 0;i+1 < sep.size();++i) {
| ~~~~^~~~~~~~~~~~
cultivation.cpp: In instantiation of 'cal_up(ll)::<lambda(auto:23&, auto:24&, ll&)> [with auto:23 = std::priority_queue<std::pair<long long int, long long int> >; auto:24 = std::vector<std::tuple<long long int, long long int, long long int> >; ll = long long int]':
cultivation.cpp:174:20: required from here
cultivation.cpp:162:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
162 | for (;i < op.size();++i) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
700 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
11 ms |
1268 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
20 ms |
1792 KB |
Output is correct |
25 |
Correct |
13 ms |
1480 KB |
Output is correct |
26 |
Correct |
37 ms |
2272 KB |
Output is correct |
27 |
Correct |
39 ms |
2376 KB |
Output is correct |
28 |
Correct |
20 ms |
1712 KB |
Output is correct |
29 |
Correct |
36 ms |
2284 KB |
Output is correct |
30 |
Correct |
45 ms |
2316 KB |
Output is correct |
31 |
Correct |
46 ms |
2200 KB |
Output is correct |
32 |
Correct |
46 ms |
2188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
700 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
11 ms |
1268 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
20 ms |
1792 KB |
Output is correct |
25 |
Correct |
13 ms |
1480 KB |
Output is correct |
26 |
Correct |
37 ms |
2272 KB |
Output is correct |
27 |
Correct |
39 ms |
2376 KB |
Output is correct |
28 |
Correct |
20 ms |
1712 KB |
Output is correct |
29 |
Correct |
36 ms |
2284 KB |
Output is correct |
30 |
Correct |
45 ms |
2316 KB |
Output is correct |
31 |
Correct |
46 ms |
2200 KB |
Output is correct |
32 |
Correct |
46 ms |
2188 KB |
Output is correct |
33 |
Correct |
712 ms |
2496 KB |
Output is correct |
34 |
Correct |
132 ms |
3000 KB |
Output is correct |
35 |
Correct |
247 ms |
2572 KB |
Output is correct |
36 |
Correct |
271 ms |
2428 KB |
Output is correct |
37 |
Correct |
274 ms |
2572 KB |
Output is correct |
38 |
Correct |
268 ms |
2444 KB |
Output is correct |
39 |
Correct |
258 ms |
2452 KB |
Output is correct |
40 |
Correct |
296 ms |
2552 KB |
Output is correct |
41 |
Correct |
108 ms |
2608 KB |
Output is correct |
42 |
Correct |
116 ms |
2040 KB |
Output is correct |
43 |
Correct |
243 ms |
2240 KB |
Output is correct |
44 |
Correct |
166 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
16 ms |
1644 KB |
Output is correct |
22 |
Correct |
16 ms |
1264 KB |
Output is correct |
23 |
Correct |
14 ms |
1260 KB |
Output is correct |
24 |
Correct |
13 ms |
1636 KB |
Output is correct |
25 |
Correct |
18 ms |
1324 KB |
Output is correct |
26 |
Correct |
10 ms |
1000 KB |
Output is correct |
27 |
Correct |
10 ms |
1216 KB |
Output is correct |
28 |
Correct |
20 ms |
1228 KB |
Output is correct |
29 |
Correct |
18 ms |
1272 KB |
Output is correct |
30 |
Correct |
16 ms |
1300 KB |
Output is correct |
31 |
Correct |
16 ms |
1252 KB |
Output is correct |
32 |
Correct |
15 ms |
1280 KB |
Output is correct |
33 |
Correct |
9 ms |
1216 KB |
Output is correct |
34 |
Correct |
12 ms |
1240 KB |
Output is correct |
35 |
Correct |
11 ms |
1212 KB |
Output is correct |
36 |
Correct |
15 ms |
1692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
3 ms |
700 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
676 KB |
Output is correct |
22 |
Correct |
11 ms |
1268 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
20 ms |
1792 KB |
Output is correct |
25 |
Correct |
13 ms |
1480 KB |
Output is correct |
26 |
Correct |
37 ms |
2272 KB |
Output is correct |
27 |
Correct |
39 ms |
2376 KB |
Output is correct |
28 |
Correct |
20 ms |
1712 KB |
Output is correct |
29 |
Correct |
36 ms |
2284 KB |
Output is correct |
30 |
Correct |
45 ms |
2316 KB |
Output is correct |
31 |
Correct |
46 ms |
2200 KB |
Output is correct |
32 |
Correct |
46 ms |
2188 KB |
Output is correct |
33 |
Correct |
712 ms |
2496 KB |
Output is correct |
34 |
Correct |
132 ms |
3000 KB |
Output is correct |
35 |
Correct |
247 ms |
2572 KB |
Output is correct |
36 |
Correct |
271 ms |
2428 KB |
Output is correct |
37 |
Correct |
274 ms |
2572 KB |
Output is correct |
38 |
Correct |
268 ms |
2444 KB |
Output is correct |
39 |
Correct |
258 ms |
2452 KB |
Output is correct |
40 |
Correct |
296 ms |
2552 KB |
Output is correct |
41 |
Correct |
108 ms |
2608 KB |
Output is correct |
42 |
Correct |
116 ms |
2040 KB |
Output is correct |
43 |
Correct |
243 ms |
2240 KB |
Output is correct |
44 |
Correct |
166 ms |
2496 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
2 ms |
460 KB |
Output is correct |
50 |
Correct |
1 ms |
460 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
2 ms |
460 KB |
Output is correct |
56 |
Correct |
1 ms |
460 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
460 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
460 KB |
Output is correct |
61 |
Correct |
1 ms |
460 KB |
Output is correct |
62 |
Correct |
1 ms |
460 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
16 ms |
1644 KB |
Output is correct |
66 |
Correct |
16 ms |
1264 KB |
Output is correct |
67 |
Correct |
14 ms |
1260 KB |
Output is correct |
68 |
Correct |
13 ms |
1636 KB |
Output is correct |
69 |
Correct |
18 ms |
1324 KB |
Output is correct |
70 |
Correct |
10 ms |
1000 KB |
Output is correct |
71 |
Correct |
10 ms |
1216 KB |
Output is correct |
72 |
Correct |
20 ms |
1228 KB |
Output is correct |
73 |
Correct |
18 ms |
1272 KB |
Output is correct |
74 |
Correct |
16 ms |
1300 KB |
Output is correct |
75 |
Correct |
16 ms |
1252 KB |
Output is correct |
76 |
Correct |
15 ms |
1280 KB |
Output is correct |
77 |
Correct |
9 ms |
1216 KB |
Output is correct |
78 |
Correct |
12 ms |
1240 KB |
Output is correct |
79 |
Correct |
11 ms |
1212 KB |
Output is correct |
80 |
Correct |
15 ms |
1692 KB |
Output is correct |
81 |
Correct |
108 ms |
2448 KB |
Output is correct |
82 |
Correct |
110 ms |
2916 KB |
Output is correct |
83 |
Correct |
155 ms |
2476 KB |
Output is correct |
84 |
Correct |
80 ms |
3284 KB |
Output is correct |
85 |
Correct |
249 ms |
2544 KB |
Output is correct |
86 |
Correct |
279 ms |
2728 KB |
Output is correct |
87 |
Correct |
235 ms |
2456 KB |
Output is correct |
88 |
Correct |
217 ms |
2800 KB |
Output is correct |
89 |
Correct |
223 ms |
2556 KB |
Output is correct |
90 |
Correct |
161 ms |
2784 KB |
Output is correct |
91 |
Correct |
227 ms |
2556 KB |
Output is correct |
92 |
Correct |
244 ms |
2960 KB |
Output is correct |
93 |
Correct |
195 ms |
2612 KB |
Output is correct |
94 |
Correct |
146 ms |
2788 KB |
Output is correct |
95 |
Correct |
250 ms |
2856 KB |
Output is correct |
96 |
Correct |
204 ms |
2784 KB |
Output is correct |
97 |
Correct |
191 ms |
2652 KB |
Output is correct |
98 |
Correct |
448 ms |
2552 KB |
Output is correct |