#include <cstdio>
#include <cstring>
#include <map>
#include <tuple>
typedef long long i64;
const int N = 50000 + 10;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m, a[N], b[N];
int f[4][20][N];
int init() {
int q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= m; ++i) scanf("%d", &b[i]);
memset(f, 0x3f, sizeof f);
for (int i = 1; i <= n; ++i) f[0][0][i] = f[2][0][i] = a[i];
for (int i = 1; i <= m; ++i) f[1][0][i] = f[3][0][i] = b[i];
for (int i = 1; i < 20; ++i) {
for (int j = (1 << (i - 1)); j <= n; ++j) f[0][i][j] = std::max(f[0][i - 1][j], f[0][i - 1][j - (1 << (i - 1))]);
for (int j = (1 << (i - 1)); j <= m; ++j) f[3][i][j] = std::max(f[3][i - 1][j], f[3][i - 1][j - (1 << (i - 1))]);
for (int j = m - (1 << (i - 1)); j > 0; --j) f[1][i][j] = std::max(f[1][i - 1][j], f[1][i - 1][j + (1 << (i - 1))]);
for (int j = n - (1 << (i - 1)); j > 0; --j) f[2][i][j] = std::max(f[2][i - 1][j], f[2][i - 1][j + (1 << (i - 1))]);
}
return q;
}
std::map<std::tuple<int, int, int>, i64> mem;
i64 dfs(int x, int y, int k) {
x += dx[k], y += dy[k];
if (!(0 < x && x <= n && 0 < y && y <= m)) return 0;
i64 &res = mem[std::make_tuple(x, y, k)];
if (res) return res;
if (k & 1) {
int cur = a[x];
for (int i = 19; i >= 0; --i) if (f[k][i][y] && f[k][i][y] <= cur) y += dy[k] << i, res += 1 << i;
if (0 < y && y <= m) res += std::max(dfs(x, y, 0), dfs(x, y, 2)) + 1;
} else {
int cur = b[y];
for (int i = 19; i >= 0; --i) if (f[k][i][x] && f[k][i][x] <= cur) x += dx[k] << i, res += 1 << i;
if (0 < x && x <= n) res += std::max(dfs(x, y, 1), dfs(x, y, 3)) + 1;
}
return res;
}
int main() {
for (int q = init(); q--;) {
int x, y;
scanf("%d%d", &x, &y);
i64 ans = dfs(x, y, 0);
for (int i = 1; i < 4; ++i) ans = std::max(ans, dfs(x, y, i));
printf("%lld\n", ans);
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int init()':
abduction2.cpp:16:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &m, &q);
^
abduction2.cpp:17:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
^
abduction2.cpp:18:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= m; ++i) scanf("%d", &b[i]);
^
abduction2.cpp: In function 'int main()':
abduction2.cpp:53:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17196 KB |
Output is correct |
2 |
Correct |
0 ms |
17196 KB |
Output is correct |
3 |
Correct |
0 ms |
17196 KB |
Output is correct |
4 |
Correct |
0 ms |
17196 KB |
Output is correct |
5 |
Correct |
0 ms |
17196 KB |
Output is correct |
6 |
Correct |
3 ms |
17196 KB |
Output is correct |
7 |
Correct |
0 ms |
17196 KB |
Output is correct |
8 |
Correct |
0 ms |
17196 KB |
Output is correct |
9 |
Correct |
3 ms |
17196 KB |
Output is correct |
10 |
Correct |
0 ms |
17196 KB |
Output is correct |
11 |
Correct |
0 ms |
17196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17196 KB |
Output is correct |
2 |
Correct |
0 ms |
17196 KB |
Output is correct |
3 |
Correct |
0 ms |
17196 KB |
Output is correct |
4 |
Correct |
0 ms |
17196 KB |
Output is correct |
5 |
Correct |
0 ms |
17196 KB |
Output is correct |
6 |
Correct |
3 ms |
17196 KB |
Output is correct |
7 |
Correct |
0 ms |
17196 KB |
Output is correct |
8 |
Correct |
0 ms |
17196 KB |
Output is correct |
9 |
Correct |
3 ms |
17196 KB |
Output is correct |
10 |
Correct |
0 ms |
17196 KB |
Output is correct |
11 |
Correct |
0 ms |
17196 KB |
Output is correct |
12 |
Correct |
3 ms |
17196 KB |
Output is correct |
13 |
Correct |
0 ms |
17196 KB |
Output is correct |
14 |
Correct |
3 ms |
17196 KB |
Output is correct |
15 |
Correct |
0 ms |
17196 KB |
Output is correct |
16 |
Correct |
0 ms |
17196 KB |
Output is correct |
17 |
Correct |
0 ms |
17196 KB |
Output is correct |
18 |
Correct |
0 ms |
17196 KB |
Output is correct |
19 |
Correct |
3 ms |
17592 KB |
Output is correct |
20 |
Correct |
6 ms |
17724 KB |
Output is correct |
21 |
Correct |
3 ms |
17592 KB |
Output is correct |
22 |
Correct |
6 ms |
17988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17196 KB |
Output is correct |
2 |
Correct |
0 ms |
17196 KB |
Output is correct |
3 |
Correct |
0 ms |
17196 KB |
Output is correct |
4 |
Correct |
0 ms |
17196 KB |
Output is correct |
5 |
Correct |
0 ms |
17196 KB |
Output is correct |
6 |
Correct |
3 ms |
17196 KB |
Output is correct |
7 |
Correct |
0 ms |
17196 KB |
Output is correct |
8 |
Correct |
0 ms |
17196 KB |
Output is correct |
9 |
Correct |
3 ms |
17196 KB |
Output is correct |
10 |
Correct |
0 ms |
17196 KB |
Output is correct |
11 |
Correct |
0 ms |
17196 KB |
Output is correct |
12 |
Correct |
3 ms |
17196 KB |
Output is correct |
13 |
Correct |
0 ms |
17196 KB |
Output is correct |
14 |
Correct |
3 ms |
17196 KB |
Output is correct |
15 |
Correct |
0 ms |
17196 KB |
Output is correct |
16 |
Correct |
0 ms |
17196 KB |
Output is correct |
17 |
Correct |
0 ms |
17196 KB |
Output is correct |
18 |
Correct |
0 ms |
17196 KB |
Output is correct |
19 |
Correct |
3 ms |
17592 KB |
Output is correct |
20 |
Correct |
6 ms |
17724 KB |
Output is correct |
21 |
Correct |
3 ms |
17592 KB |
Output is correct |
22 |
Correct |
6 ms |
17988 KB |
Output is correct |
23 |
Correct |
33 ms |
17196 KB |
Output is correct |
24 |
Correct |
23 ms |
17196 KB |
Output is correct |
25 |
Correct |
26 ms |
17196 KB |
Output is correct |
26 |
Correct |
26 ms |
17196 KB |
Output is correct |
27 |
Correct |
26 ms |
17196 KB |
Output is correct |
28 |
Correct |
49 ms |
23720 KB |
Output is correct |
29 |
Correct |
26 ms |
18092 KB |
Output is correct |
30 |
Correct |
103 ms |
27812 KB |
Output is correct |
31 |
Correct |
149 ms |
30952 KB |
Output is correct |
32 |
Correct |
26 ms |
17724 KB |
Output is correct |
33 |
Correct |
63 ms |
20424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
17592 KB |
Output is correct |
2 |
Correct |
0 ms |
17592 KB |
Output is correct |
3 |
Correct |
3 ms |
17592 KB |
Output is correct |
4 |
Correct |
3 ms |
17592 KB |
Output is correct |
5 |
Correct |
6 ms |
17592 KB |
Output is correct |
6 |
Correct |
3 ms |
17460 KB |
Output is correct |
7 |
Correct |
0 ms |
17460 KB |
Output is correct |
8 |
Correct |
9 ms |
17988 KB |
Output is correct |
9 |
Correct |
13 ms |
17988 KB |
Output is correct |
10 |
Correct |
3 ms |
17988 KB |
Output is correct |
11 |
Correct |
6 ms |
18252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
17196 KB |
Output is correct |
2 |
Correct |
0 ms |
17196 KB |
Output is correct |
3 |
Correct |
0 ms |
17196 KB |
Output is correct |
4 |
Correct |
0 ms |
17196 KB |
Output is correct |
5 |
Correct |
0 ms |
17196 KB |
Output is correct |
6 |
Correct |
3 ms |
17196 KB |
Output is correct |
7 |
Correct |
0 ms |
17196 KB |
Output is correct |
8 |
Correct |
0 ms |
17196 KB |
Output is correct |
9 |
Correct |
3 ms |
17196 KB |
Output is correct |
10 |
Correct |
0 ms |
17196 KB |
Output is correct |
11 |
Correct |
0 ms |
17196 KB |
Output is correct |
12 |
Correct |
3 ms |
17196 KB |
Output is correct |
13 |
Correct |
0 ms |
17196 KB |
Output is correct |
14 |
Correct |
3 ms |
17196 KB |
Output is correct |
15 |
Correct |
0 ms |
17196 KB |
Output is correct |
16 |
Correct |
0 ms |
17196 KB |
Output is correct |
17 |
Correct |
0 ms |
17196 KB |
Output is correct |
18 |
Correct |
0 ms |
17196 KB |
Output is correct |
19 |
Correct |
3 ms |
17592 KB |
Output is correct |
20 |
Correct |
6 ms |
17724 KB |
Output is correct |
21 |
Correct |
3 ms |
17592 KB |
Output is correct |
22 |
Correct |
6 ms |
17988 KB |
Output is correct |
23 |
Correct |
33 ms |
17196 KB |
Output is correct |
24 |
Correct |
23 ms |
17196 KB |
Output is correct |
25 |
Correct |
26 ms |
17196 KB |
Output is correct |
26 |
Correct |
26 ms |
17196 KB |
Output is correct |
27 |
Correct |
26 ms |
17196 KB |
Output is correct |
28 |
Correct |
49 ms |
23720 KB |
Output is correct |
29 |
Correct |
26 ms |
18092 KB |
Output is correct |
30 |
Correct |
103 ms |
27812 KB |
Output is correct |
31 |
Correct |
149 ms |
30952 KB |
Output is correct |
32 |
Correct |
26 ms |
17724 KB |
Output is correct |
33 |
Correct |
63 ms |
20424 KB |
Output is correct |
34 |
Correct |
6 ms |
17592 KB |
Output is correct |
35 |
Correct |
0 ms |
17592 KB |
Output is correct |
36 |
Correct |
3 ms |
17592 KB |
Output is correct |
37 |
Correct |
3 ms |
17592 KB |
Output is correct |
38 |
Correct |
6 ms |
17592 KB |
Output is correct |
39 |
Correct |
3 ms |
17460 KB |
Output is correct |
40 |
Correct |
0 ms |
17460 KB |
Output is correct |
41 |
Correct |
9 ms |
17988 KB |
Output is correct |
42 |
Correct |
13 ms |
17988 KB |
Output is correct |
43 |
Correct |
3 ms |
17988 KB |
Output is correct |
44 |
Correct |
6 ms |
18252 KB |
Output is correct |
45 |
Correct |
39 ms |
17724 KB |
Output is correct |
46 |
Correct |
39 ms |
17724 KB |
Output is correct |
47 |
Correct |
43 ms |
17856 KB |
Output is correct |
48 |
Correct |
39 ms |
17724 KB |
Output is correct |
49 |
Correct |
33 ms |
17724 KB |
Output is correct |
50 |
Correct |
79 ms |
24232 KB |
Output is correct |
51 |
Correct |
79 ms |
24824 KB |
Output is correct |
52 |
Correct |
193 ms |
36372 KB |
Output is correct |
53 |
Correct |
213 ms |
35652 KB |
Output is correct |
54 |
Correct |
176 ms |
34596 KB |
Output is correct |
55 |
Correct |
263 ms |
43748 KB |
Output is correct |
56 |
Correct |
2566 ms |
266808 KB |
Output is correct |
57 |
Correct |
503 ms |
81816 KB |
Output is correct |
58 |
Correct |
536 ms |
78536 KB |
Output is correct |
59 |
Correct |
493 ms |
78016 KB |
Output is correct |