#include <bits/stdc++.h>
using namespace std;
int H, W, Q;
map<int,int> memo;
int mxl[50010][16], mxr[50010][16], mxu[50010][16], mxd[50010][16];
int A[50010], B[50010];
int dfs(int x, int y, int dir) {
if (memo.find(x*W*2 + y*2 + dir) != memo.end()) return memo[x*W*2 + y*2 + dir];
int cur = 0;
if (dir == 0) {
int u = x - 1, d = x + 1;
for (int lv=15; lv>=0; lv--) {
if (u - (1 << lv) >= -1 && mxu[u][lv] < B[y])
u -= (1 << lv);
if (d + (1 << lv) <= H && mxd[d][lv] < B[y])
d += (1 << lv);
}
if (u != -1) cur = max(cur, dfs(u, y, 1) + x - u);
else cur = max(cur, x);
if (d != H) cur = max(cur, dfs(d, y, 1) + d - x);
else cur = max(cur, H - x - 1);
}
else {
int l = y - 1, r = y + 1;
for (int lv=15; lv>=0; lv--) {
if (l - (1 << lv) >= -1 && mxl[l][lv] < A[x])
l -= (1 << lv);
if (r + (1 << lv) <= W && mxr[r][lv] < A[x])
r += (1 << lv);
}
if (l != -1) cur = max(cur, dfs(x, l, 0) + y - l);
else cur = max(cur, y);
if (r != W) cur = max(cur, dfs(x, r, 0) + r - y);
else cur = max(cur, W - y - 1);
}
memo[x*W*2 + y*2 + dir] = cur;
return cur;
}
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
cin >> H >> W >> Q;
for (int i=0; i<H; i++) {
cin >> A[i];
mxu[i][0] = mxd[i][0] = A[i];
}
for (int i=0; i<W; i++) {
cin >> B[i];
mxl[i][0] = mxr[i][0] = B[i];
}
for (int lv=1; lv<=15; lv++) {
for (int i=0; i<H; i++) {
if (i - (1 << lv) >= -1)
mxu[i][lv] = max(mxu[i][lv-1], mxu[i-(1<<(lv-1))][lv-1]);
if (i + (1 << lv) <= H)
mxd[i][lv] = max(mxd[i][lv-1], mxd[i+(1<<(lv-1))][lv-1]);
}
}
for (int lv=1; lv<=15; lv++) {
for (int i=0; i<W; i++) {
if (i - (1 << lv) >= -1)
mxl[i][lv] = max(mxl[i][lv-1], mxl[i-(1<<(lv-1))][lv-1]);
if (i + (1 << lv) <= W)
mxr[i][lv] = max(mxr[i][lv-1], mxr[i+(1<<(lv-1))][lv-1]);
}
}
while (Q --) {
int x, y;
cin >> x >> y;
x --; y --;
cout << max(dfs(x, y, 0), dfs(x, y, 1)) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8660 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8536 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8660 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8536 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8676 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8660 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8536 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8676 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
23 |
Correct |
15 ms |
14172 KB |
Output is correct |
24 |
Correct |
20 ms |
14236 KB |
Output is correct |
25 |
Correct |
16 ms |
14308 KB |
Output is correct |
26 |
Correct |
14 ms |
14172 KB |
Output is correct |
27 |
Correct |
13 ms |
14424 KB |
Output is correct |
28 |
Correct |
22 ms |
18268 KB |
Output is correct |
29 |
Correct |
16 ms |
14684 KB |
Output is correct |
30 |
Correct |
42 ms |
19028 KB |
Output is correct |
31 |
Correct |
45 ms |
20548 KB |
Output is correct |
32 |
Correct |
17 ms |
14304 KB |
Output is correct |
33 |
Correct |
23 ms |
15632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
3 ms |
8796 KB |
Output is correct |
4 |
Correct |
3 ms |
8764 KB |
Output is correct |
5 |
Correct |
3 ms |
9032 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
4 ms |
8924 KB |
Output is correct |
9 |
Correct |
4 ms |
9304 KB |
Output is correct |
10 |
Correct |
3 ms |
8796 KB |
Output is correct |
11 |
Correct |
5 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8660 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8536 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8676 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
3 ms |
8796 KB |
Output is correct |
22 |
Correct |
3 ms |
9052 KB |
Output is correct |
23 |
Correct |
15 ms |
14172 KB |
Output is correct |
24 |
Correct |
20 ms |
14236 KB |
Output is correct |
25 |
Correct |
16 ms |
14308 KB |
Output is correct |
26 |
Correct |
14 ms |
14172 KB |
Output is correct |
27 |
Correct |
13 ms |
14424 KB |
Output is correct |
28 |
Correct |
22 ms |
18268 KB |
Output is correct |
29 |
Correct |
16 ms |
14684 KB |
Output is correct |
30 |
Correct |
42 ms |
19028 KB |
Output is correct |
31 |
Correct |
45 ms |
20548 KB |
Output is correct |
32 |
Correct |
17 ms |
14304 KB |
Output is correct |
33 |
Correct |
23 ms |
15632 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
3 ms |
8796 KB |
Output is correct |
37 |
Correct |
3 ms |
8764 KB |
Output is correct |
38 |
Correct |
3 ms |
9032 KB |
Output is correct |
39 |
Correct |
2 ms |
8792 KB |
Output is correct |
40 |
Correct |
3 ms |
8796 KB |
Output is correct |
41 |
Correct |
4 ms |
8924 KB |
Output is correct |
42 |
Correct |
4 ms |
9304 KB |
Output is correct |
43 |
Correct |
3 ms |
8796 KB |
Output is correct |
44 |
Correct |
5 ms |
9048 KB |
Output is correct |
45 |
Correct |
16 ms |
14556 KB |
Output is correct |
46 |
Correct |
15 ms |
14428 KB |
Output is correct |
47 |
Correct |
15 ms |
14428 KB |
Output is correct |
48 |
Correct |
18 ms |
14428 KB |
Output is correct |
49 |
Correct |
18 ms |
14424 KB |
Output is correct |
50 |
Correct |
24 ms |
17668 KB |
Output is correct |
51 |
Correct |
24 ms |
18268 KB |
Output is correct |
52 |
Correct |
60 ms |
22100 KB |
Output is correct |
53 |
Correct |
59 ms |
21668 KB |
Output is correct |
54 |
Correct |
63 ms |
21332 KB |
Output is correct |
55 |
Incorrect |
84 ms |
25292 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |