#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
map<tuple<int, int, int>, ll> dp;
int n, m, h[50001][16], v[50001][16];
ll dfs(int x, int y, int dir) {
if (dp.count({x, y, dir})) return dp[{x, y, dir}];
ll &ans = dp[{x, y, dir}];
if (dir & 1) { // Up or down (x changes)
int sign = dir - 2;
int nxt_x = x;
for (int i = 15; ~i; i--) {
int tmp_x = nxt_x + (1 << i) * sign - sign;
if (tmp_x < 1 || tmp_x > n) continue;
if ((sign == 1 ? h[nxt_x][i] : h[tmp_x][i]) > v[y][0]) continue;
nxt_x = tmp_x + sign;
}
if (nxt_x < 1 || nxt_x > n) return ans = abs(nxt_x - sign - x);
return ans = abs(nxt_x - x) + max(dfs(nxt_x, y, 0), dfs(nxt_x, y, 2));
} else { // Left or right (y changes)
int sign = dir - 1;
int nxt_y = y;
for (int i = 15; ~i; i--) {
int tmp_y = nxt_y + (1 << i) * sign - sign;
if (tmp_y < 1 || tmp_y > m) continue;
if ((sign == 1 ? v[nxt_y][i] : v[tmp_y][i]) > h[x][0]) continue;
nxt_y = tmp_y + sign;
}
if (nxt_y < 1 || nxt_y > m) return ans = abs(nxt_y - sign - y);
return ans = abs(nxt_y - y) + max(dfs(x, nxt_y, 1), dfs(x, nxt_y, 3));
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) cin >> h[i][0];
for (int i = 1; i <= m; i++) cin >> v[i][0];
for (int j = 1; j < 16; j++) {
for (int i = 1; i <= n - (1 << j) + 1; i++) {
h[i][j] = max(h[i][j - 1], h[i + (1 << j - 1)][j - 1]);
}
for (int i = 1; i <= m - (1 << j) + 1; i++) {
v[i][j] = max(v[i][j - 1], v[i + (1 << j - 1)][j - 1]);
}
}
while (q--) {
int x, y;
cin >> x >> y;
ll ans = 0;
if (x < n) ans = max(ans, dfs(x + 1, y, 3) + 1);
if (y < m) ans = max(ans, dfs(x, y + 1, 2) + 1);
if (x > 1) ans = max(ans, dfs(x - 1, y, 1) + 1);
if (y > 1) ans = max(ans, dfs(x, y - 1, 0) + 1);
cout << ans << '\n';
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:45:45: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
h[i][j] = max(h[i][j - 1], h[i + (1 << j - 1)][j - 1]);
~~^~~
abduction2.cpp:48:45: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
v[i][j] = max(v[i][j - 1], v[i + (1 << j - 1)][j - 1]);
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
4 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
4 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1408 KB |
Output is correct |
23 |
Correct |
27 ms |
7524 KB |
Output is correct |
24 |
Correct |
21 ms |
7552 KB |
Output is correct |
25 |
Correct |
21 ms |
7552 KB |
Output is correct |
26 |
Correct |
23 ms |
7548 KB |
Output is correct |
27 |
Correct |
22 ms |
7552 KB |
Output is correct |
28 |
Correct |
51 ms |
14200 KB |
Output is correct |
29 |
Correct |
33 ms |
8440 KB |
Output is correct |
30 |
Correct |
150 ms |
18424 KB |
Output is correct |
31 |
Correct |
181 ms |
21496 KB |
Output is correct |
32 |
Correct |
25 ms |
8056 KB |
Output is correct |
33 |
Correct |
48 ms |
10872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
4 ms |
1024 KB |
Output is correct |
5 |
Correct |
4 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
8 ms |
1536 KB |
Output is correct |
9 |
Correct |
9 ms |
1536 KB |
Output is correct |
10 |
Correct |
8 ms |
1408 KB |
Output is correct |
11 |
Correct |
10 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
1 ms |
640 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
4 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1408 KB |
Output is correct |
23 |
Correct |
27 ms |
7524 KB |
Output is correct |
24 |
Correct |
21 ms |
7552 KB |
Output is correct |
25 |
Correct |
21 ms |
7552 KB |
Output is correct |
26 |
Correct |
23 ms |
7548 KB |
Output is correct |
27 |
Correct |
22 ms |
7552 KB |
Output is correct |
28 |
Correct |
51 ms |
14200 KB |
Output is correct |
29 |
Correct |
33 ms |
8440 KB |
Output is correct |
30 |
Correct |
150 ms |
18424 KB |
Output is correct |
31 |
Correct |
181 ms |
21496 KB |
Output is correct |
32 |
Correct |
25 ms |
8056 KB |
Output is correct |
33 |
Correct |
48 ms |
10872 KB |
Output is correct |
34 |
Correct |
5 ms |
1024 KB |
Output is correct |
35 |
Correct |
4 ms |
1024 KB |
Output is correct |
36 |
Correct |
5 ms |
1024 KB |
Output is correct |
37 |
Correct |
4 ms |
1024 KB |
Output is correct |
38 |
Correct |
4 ms |
1024 KB |
Output is correct |
39 |
Correct |
3 ms |
1024 KB |
Output is correct |
40 |
Correct |
3 ms |
1024 KB |
Output is correct |
41 |
Correct |
8 ms |
1536 KB |
Output is correct |
42 |
Correct |
9 ms |
1536 KB |
Output is correct |
43 |
Correct |
8 ms |
1408 KB |
Output is correct |
44 |
Correct |
10 ms |
1664 KB |
Output is correct |
45 |
Correct |
34 ms |
8056 KB |
Output is correct |
46 |
Correct |
33 ms |
8056 KB |
Output is correct |
47 |
Correct |
31 ms |
8112 KB |
Output is correct |
48 |
Correct |
28 ms |
8148 KB |
Output is correct |
49 |
Correct |
27 ms |
8056 KB |
Output is correct |
50 |
Correct |
57 ms |
14584 KB |
Output is correct |
51 |
Correct |
67 ms |
15480 KB |
Output is correct |
52 |
Correct |
254 ms |
26872 KB |
Output is correct |
53 |
Correct |
237 ms |
26104 KB |
Output is correct |
54 |
Correct |
220 ms |
24952 KB |
Output is correct |
55 |
Correct |
319 ms |
34168 KB |
Output is correct |
56 |
Correct |
3089 ms |
257640 KB |
Output is correct |
57 |
Correct |
698 ms |
72060 KB |
Output is correct |
58 |
Correct |
676 ms |
68984 KB |
Output is correct |
59 |
Correct |
677 ms |
68344 KB |
Output is correct |