#include <stdio.h>
#include <string.h>
#define N 300
#define N_ (1 << 9) /* N_ = pow2(ceil(log2(N))) */
#define N2 (N * 2 + 5)
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long xx[N], xx_[N2], yy[N], yy_[N], x_, y_; int n;
long long *zz;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (zz[ii[j]] == zz[i_])
j++;
else if (zz[ii[j]] < zz[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
long long stl[N_ * 2], str[N_ * 2], stc[N_ * 2]; int n_;
void pul(int i) {
int l = i << 1, r = l | 1;
stl[i] = min(stl[l], stl[r]);
str[i] = max(str[l], str[r]);
stc[i] = max(stc[l], stc[r]);
if (str[l] != -1 && stl[r] != INF)
stc[i] = max(stc[i], stl[r] - str[l] - 1);
}
int kk[N];
void update(int i, int x) {
kk[i] += x;
if (kk[i] == 0)
stl[n_ + i] = INF, str[n_ + i] = -1, stc[n_ + i] = 0;
else
stl[n_ + i] = yy_[i], str[n_ + i] = yy_[i], stc[n_ + i] = 0;
i += n_;
while (i > 1)
pul(i >>= 1);
}
long long solve(long long w) {
static int ii[N2];
static long long ll[N2], rr[N2], cc[N2], xx1[N2];
static int qul[N2], qur[N2], quc[N2];
int m, h, h_, i, headl, cntl, headr, cntr, headc, cntc;
long long l, r, c, ans;
if (w < 0)
return INF;
for (i = 0; i < n; i++)
xx_[i << 1 | 0] = xx[i], xx_[i << 1 | 1] = xx[i] + w + 1;
for (i = 0; i < n * 2; i++)
ii[i] = i;
zz = xx_, sort(ii, 0, n * 2);
memset(stl, 0x3f, n_ * 2 * sizeof *stl);
memset(str, -1, n_ * 2 * sizeof *str);
memset(stc, 0, n_ * 2 * sizeof *stc);
memset(kk, 0, n * sizeof *kk);
m = 0;
ll[m] = INF, rr[m] = -1, cc[m] = INF, xx1[m] = -INF, m++;
for (h = 0; h < n * 2; h++) {
i = ii[h];
if ((i & 1) == 0)
update(yy[i >> 1], 1);
else
update(yy[i >> 1], -1);
if (h + 1 == n * 2 || xx_[ii[h + 1]] != xx_[ii[h]]) {
if (stl[1] == INF)
ll[m] = INF, rr[m] = -1, cc[m] = INF;
else
ll[m] = stl[1], rr[m] = str[1], cc[m] = stc[1];
xx1[m++] = xx_[ii[h]];
}
}
xx1[m] = INF;
ans = INF;
l = -1, r = INF, c = 0;
for (h = 0; h < m; h++) {
if (xx1[h + 1] <= 0 || x_ <= xx1[h])
continue;
l = max(l, ll[h]), r = min(r, rr[h]), c = max(c, cc[h]);
}
if (l != INF && r != -1 && c != INF)
ans = min(ans, w + max(l + y_ - 1 - r, c));
l = -1, r = INF, c = 0;
for (h = 0; h < m; h++) {
if (xx1[h + 1] <= w || w + x_ <= xx1[h])
continue;
l = max(l, ll[h]), r = min(r, rr[h]), c = max(c, cc[h]);
}
if (l != INF && r != -1 && c != INF)
ans = min(ans, w + max(l + y_ - 1 - r, c));
headl = cntl = 0, headr = cntr = 0, headc = cntc = 0;
for (h = 0, h_ = 0; h < m; h++) {
while (h_ < m && xx1[h_] < xx1[h] + x_) {
while (cntl && ll[qul[headl + cntl - 1]] <= ll[h_])
cntl--;
qul[headl + cntl++] = h_;
while (cntr && rr[qur[headr + cntr - 1]] >= rr[h_])
cntr--;
qur[headr + cntr++] = h_;
while (cntc && cc[quc[headc + cntc - 1]] <= cc[h_])
cntc--;
quc[headc + cntc++] = h_;
h_++;
}
if (xx1[h] >= 0 && xx1[h] <= w) {
l = ll[qul[headl]], r = rr[qur[headr]], c = cc[quc[headc]];
if (l != INF && r != -1 && c != INF)
ans = min(ans, w + max(l + y_ - 1 - r, c));
}
if (qul[headl] == h)
headl++, cntl--;
if (qur[headr] == h)
headr++, cntr--;
if (quc[headc] == h)
headc++, cntc--;
}
return ans;
}
int main() {
static int ii[N];
int h, i, j;
long long y, ans;
scanf("%lld%lld%d", &x_, &y_, &n);
for (i = 0; i < n; i++)
scanf("%lld%lld", &xx[i], &yy[i]), xx[i]--, yy[i]--;
for (i = 0; i < n; i++)
ii[i] = i;
zz = yy, sort(ii, 0, n);
n_ = 0;
for (h = 0; h < n; h++) {
i = ii[h], y = yy[i];
if (n_ == 0 || yy_[n_ - 1] != y)
yy_[n_++] = y;
yy[i] = n_ - 1;
}
n_ = 1;
while (n_ < n)
n_ <<= 1;
ans = INF;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
ans = min(ans, min(solve((x_ - 1 - xx[i]) + (xx[j] - 0)), solve(xx[j] - xx[i] - 1)));
printf("%lld\n", ans);
return 0;
}
Compilation message
cultivation.c: In function 'main':
cultivation.c:152:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | scanf("%lld%lld%d", &x_, &y_, &n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.c:154:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | scanf("%lld%lld", &xx[i], &yy[i]), xx[i]--, yy[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
88 ms |
316 KB |
Output is correct |
19 |
Correct |
8 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
47 ms |
328 KB |
Output is correct |
22 |
Correct |
540 ms |
316 KB |
Output is correct |
23 |
Correct |
10 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2078 ms |
340 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
88 ms |
316 KB |
Output is correct |
19 |
Correct |
8 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
47 ms |
328 KB |
Output is correct |
22 |
Correct |
540 ms |
316 KB |
Output is correct |
23 |
Correct |
10 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2078 ms |
340 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
212 KB |
Output is correct |
3 |
Correct |
6 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
328 KB |
Output is correct |
5 |
Correct |
7 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
332 KB |
Output is correct |
8 |
Correct |
9 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
212 KB |
Output is correct |
13 |
Correct |
4 ms |
212 KB |
Output is correct |
14 |
Correct |
5 ms |
212 KB |
Output is correct |
15 |
Correct |
6 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
212 KB |
Output is correct |
17 |
Correct |
6 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
212 KB |
Output is correct |
19 |
Correct |
6 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
212 KB |
Output is correct |
3 |
Correct |
6 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
328 KB |
Output is correct |
5 |
Correct |
7 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
212 KB |
Output is correct |
7 |
Correct |
6 ms |
332 KB |
Output is correct |
8 |
Correct |
9 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
212 KB |
Output is correct |
13 |
Correct |
4 ms |
212 KB |
Output is correct |
14 |
Correct |
5 ms |
212 KB |
Output is correct |
15 |
Correct |
6 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
212 KB |
Output is correct |
17 |
Correct |
6 ms |
212 KB |
Output is correct |
18 |
Correct |
7 ms |
212 KB |
Output is correct |
19 |
Correct |
6 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
324 KB |
Output is correct |
21 |
Correct |
445 ms |
316 KB |
Output is correct |
22 |
Correct |
449 ms |
328 KB |
Output is correct |
23 |
Correct |
453 ms |
316 KB |
Output is correct |
24 |
Correct |
464 ms |
316 KB |
Output is correct |
25 |
Correct |
459 ms |
212 KB |
Output is correct |
26 |
Correct |
377 ms |
320 KB |
Output is correct |
27 |
Correct |
464 ms |
312 KB |
Output is correct |
28 |
Correct |
449 ms |
320 KB |
Output is correct |
29 |
Correct |
455 ms |
316 KB |
Output is correct |
30 |
Correct |
463 ms |
316 KB |
Output is correct |
31 |
Correct |
464 ms |
316 KB |
Output is correct |
32 |
Correct |
464 ms |
312 KB |
Output is correct |
33 |
Correct |
470 ms |
316 KB |
Output is correct |
34 |
Correct |
457 ms |
316 KB |
Output is correct |
35 |
Correct |
473 ms |
312 KB |
Output is correct |
36 |
Correct |
443 ms |
316 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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
88 ms |
316 KB |
Output is correct |
19 |
Correct |
8 ms |
212 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
47 ms |
328 KB |
Output is correct |
22 |
Correct |
540 ms |
316 KB |
Output is correct |
23 |
Correct |
10 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2078 ms |
340 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |