#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int pw[17];
map<tuple<int, int, int>, ll> dp;
int n, m, h[50001][17], v[50001][17];
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 = 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;
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;
}
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 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 |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
8 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 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 |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
8 ms |
1408 KB |
Output is correct |
23 |
Correct |
21 ms |
7040 KB |
Output is correct |
24 |
Correct |
20 ms |
7040 KB |
Output is correct |
25 |
Correct |
21 ms |
7040 KB |
Output is correct |
26 |
Correct |
21 ms |
7040 KB |
Output is correct |
27 |
Correct |
20 ms |
7040 KB |
Output is correct |
28 |
Correct |
52 ms |
13688 KB |
Output is correct |
29 |
Correct |
25 ms |
7932 KB |
Output is correct |
30 |
Correct |
151 ms |
17728 KB |
Output is correct |
31 |
Correct |
179 ms |
20984 KB |
Output is correct |
32 |
Correct |
25 ms |
7544 KB |
Output is correct |
33 |
Correct |
60 ms |
10360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
1024 KB |
Output is correct |
5 |
Correct |
4 ms |
896 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
8 ms |
1460 KB |
Output is correct |
9 |
Correct |
11 ms |
1408 KB |
Output is correct |
10 |
Correct |
8 ms |
1408 KB |
Output is correct |
11 |
Correct |
9 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 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 |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
8 ms |
1408 KB |
Output is correct |
23 |
Correct |
21 ms |
7040 KB |
Output is correct |
24 |
Correct |
20 ms |
7040 KB |
Output is correct |
25 |
Correct |
21 ms |
7040 KB |
Output is correct |
26 |
Correct |
21 ms |
7040 KB |
Output is correct |
27 |
Correct |
20 ms |
7040 KB |
Output is correct |
28 |
Correct |
52 ms |
13688 KB |
Output is correct |
29 |
Correct |
25 ms |
7932 KB |
Output is correct |
30 |
Correct |
151 ms |
17728 KB |
Output is correct |
31 |
Correct |
179 ms |
20984 KB |
Output is correct |
32 |
Correct |
25 ms |
7544 KB |
Output is correct |
33 |
Correct |
60 ms |
10360 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 |
6 ms |
1024 KB |
Output is correct |
38 |
Correct |
4 ms |
896 KB |
Output is correct |
39 |
Correct |
4 ms |
1024 KB |
Output is correct |
40 |
Correct |
3 ms |
1024 KB |
Output is correct |
41 |
Correct |
8 ms |
1460 KB |
Output is correct |
42 |
Correct |
11 ms |
1408 KB |
Output is correct |
43 |
Correct |
8 ms |
1408 KB |
Output is correct |
44 |
Correct |
9 ms |
1664 KB |
Output is correct |
45 |
Correct |
27 ms |
7544 KB |
Output is correct |
46 |
Correct |
27 ms |
7552 KB |
Output is correct |
47 |
Correct |
28 ms |
7520 KB |
Output is correct |
48 |
Correct |
38 ms |
7544 KB |
Output is correct |
49 |
Correct |
33 ms |
7544 KB |
Output is correct |
50 |
Correct |
58 ms |
14076 KB |
Output is correct |
51 |
Correct |
69 ms |
14840 KB |
Output is correct |
52 |
Correct |
227 ms |
26316 KB |
Output is correct |
53 |
Correct |
233 ms |
25468 KB |
Output is correct |
54 |
Correct |
217 ms |
24440 KB |
Output is correct |
55 |
Correct |
319 ms |
33784 KB |
Output is correct |
56 |
Correct |
3235 ms |
257940 KB |
Output is correct |
57 |
Correct |
760 ms |
72572 KB |
Output is correct |
58 |
Correct |
707 ms |
69240 KB |
Output is correct |
59 |
Correct |
758 ms |
68664 KB |
Output is correct |