#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int pw[17];
map<tuple<int, int, int>, int> dp;
int n, m, h[50001][17], v[50001][17];
int dfs(int x, int y, int dir) {
if (dp.count({x, y, dir})) return dp[{x, y, dir}];
int &ans = dp[{x, y, dir}];
if (dir & 1) { // Up or down (x changes)
int sign = dir - 2;
int nxt_x = x;
for (int i = 16; ~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 = 16; ~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);
pw[0] = 1;
for (int i = 1; i < 17; i++) pw[i] = pw[i - 1] << 1;
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 < 17; j++) {
for (int i = 1; i <= n - pw[j] + 1; i++) {
h[i][j] = max(h[i][j - 1], h[i + pw[j - 1]][j - 1]);
}
for (int i = 1; i <= m - pw[j] + 1; i++) {
v[i][j] = max(v[i][j - 1], v[i + pw[j - 1]][j - 1]);
}
}
while (q--) {
int x, y;
cin >> x >> y;
int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
2 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 |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
4 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 |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
2 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 |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
4 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 |
1536 KB |
Output is correct |
23 |
Correct |
25 ms |
7928 KB |
Output is correct |
24 |
Correct |
24 ms |
7936 KB |
Output is correct |
25 |
Correct |
24 ms |
7936 KB |
Output is correct |
26 |
Correct |
21 ms |
7936 KB |
Output is correct |
27 |
Correct |
27 ms |
7936 KB |
Output is correct |
28 |
Correct |
54 ms |
14584 KB |
Output is correct |
29 |
Correct |
27 ms |
8952 KB |
Output is correct |
30 |
Correct |
165 ms |
18936 KB |
Output is correct |
31 |
Correct |
208 ms |
21848 KB |
Output is correct |
32 |
Correct |
39 ms |
8568 KB |
Output is correct |
33 |
Correct |
56 ms |
11256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1024 KB |
Output is correct |
2 |
Correct |
6 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
5 ms |
1024 KB |
Output is correct |
5 |
Correct |
7 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 |
9 ms |
1536 KB |
Output is correct |
9 |
Correct |
10 ms |
1536 KB |
Output is correct |
10 |
Correct |
8 ms |
1536 KB |
Output is correct |
11 |
Correct |
10 ms |
1664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 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 |
2 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 |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
4 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 |
1536 KB |
Output is correct |
23 |
Correct |
25 ms |
7928 KB |
Output is correct |
24 |
Correct |
24 ms |
7936 KB |
Output is correct |
25 |
Correct |
24 ms |
7936 KB |
Output is correct |
26 |
Correct |
21 ms |
7936 KB |
Output is correct |
27 |
Correct |
27 ms |
7936 KB |
Output is correct |
28 |
Correct |
54 ms |
14584 KB |
Output is correct |
29 |
Correct |
27 ms |
8952 KB |
Output is correct |
30 |
Correct |
165 ms |
18936 KB |
Output is correct |
31 |
Correct |
208 ms |
21848 KB |
Output is correct |
32 |
Correct |
39 ms |
8568 KB |
Output is correct |
33 |
Correct |
56 ms |
11256 KB |
Output is correct |
34 |
Correct |
5 ms |
1024 KB |
Output is correct |
35 |
Correct |
6 ms |
1024 KB |
Output is correct |
36 |
Correct |
5 ms |
1024 KB |
Output is correct |
37 |
Correct |
5 ms |
1024 KB |
Output is correct |
38 |
Correct |
7 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 |
9 ms |
1536 KB |
Output is correct |
42 |
Correct |
10 ms |
1536 KB |
Output is correct |
43 |
Correct |
8 ms |
1536 KB |
Output is correct |
44 |
Correct |
10 ms |
1664 KB |
Output is correct |
45 |
Correct |
31 ms |
8576 KB |
Output is correct |
46 |
Correct |
28 ms |
8576 KB |
Output is correct |
47 |
Correct |
29 ms |
8568 KB |
Output is correct |
48 |
Correct |
37 ms |
8568 KB |
Output is correct |
49 |
Correct |
32 ms |
8572 KB |
Output is correct |
50 |
Correct |
74 ms |
15096 KB |
Output is correct |
51 |
Correct |
64 ms |
15736 KB |
Output is correct |
52 |
Correct |
249 ms |
27256 KB |
Output is correct |
53 |
Correct |
240 ms |
26488 KB |
Output is correct |
54 |
Correct |
226 ms |
25336 KB |
Output is correct |
55 |
Incorrect |
333 ms |
34680 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |