#include <bits/stdc++.h>
using namespace std;
#define forn(i, a, b) for(int i=a; i<=b; ++i)
#define ford(i, a, b) for(int i=a; i>=b; --i)
#define repn(i, a, b) for(int i=a; i <b; ++i)
#define repd(i, a, b) for(int i=(int)a-1; i>=b; --i)
const int maxLog = 19;
const int maxN = 5e4 +10;
int H, W, Q;
int A[maxN], B[maxN];
int pw[maxLog];
int col[maxN][maxLog], row[maxN][maxLog];
map< tuple<int, int, int>, long long > dp;
int max(int a, int b, int c, int d) {
return max( max(a, b), max(c, d) );
}
long long DFS(int cur_x, int cur_y, int dir) {
tuple<int, int, int> cur_state = {cur_x, cur_y, dir};
if (dp.count(cur_state)) return dp[cur_state];
long long &ans = dp[cur_state];
int sign, nxt, tmp;
if (dir == 0 || dir == 1) {
sign = (dir == 0 ? -1 : 1);
nxt = -1;
tmp = cur_x;
ford(i, 16, 0) {
nxt = cur_x + pw[i] * sign - sign;
if (nxt < 1 || nxt > H) continue;
if ( (sign == 1 ? row[cur_x][i] : row[nxt][i]) > B[cur_y] ) continue;
cur_x = nxt + sign;
}
if (cur_x < 1 || cur_x > H) {
cur_x -= sign;
return ans = abs(cur_x - tmp);
}
return ans = abs(cur_x - tmp) + max( DFS(cur_x, cur_y, 2), DFS(cur_x, cur_y, 3) );
}
sign = (dir == 2 ? -1 : 1);
nxt = -1;
tmp = cur_y;
ford(i, 16, 0) {
nxt = cur_y + pw[i] * sign - sign;
if (nxt < 1 || nxt > W) continue;
if ( (sign == 1 ? col[cur_y][i] : col[nxt][i]) > A[cur_x] ) continue;
cur_y = nxt + sign;
}
if (cur_y < 1 || cur_y > W) {
cur_y -= sign;
return ans = abs(cur_y - tmp);
}
return ans = abs(cur_y - tmp) + max( DFS(cur_x, cur_y, 0), DFS(cur_x, cur_y, 1) );
}
int main() {
// freopen("a.inp", "r", stdin);
// freopen("a.out", "w", stdout);
ios::sync_with_stdio(0);
pw[0] = 1;
forn(i, 1, 17) pw[i] = pw[i-1] * 2;
cin >> H >> W >> Q;
forn(i, 1, H) cin >> A[i];
forn(i, 1, W) cin >> B[i];
forn(i, 1, H) row[i][0] = A[i];
forn(i, 1, W) col[i][0] = B[i];
forn(lg, 1, 16) forn(i, 1, H-pw[lg]+1)
row[i][lg] = max(row[i][lg-1], row[i+pw[lg-1]][lg-1]);
forn(lg, 1, 16) forn(i, 1, W-pw[lg]+1)
col[i][lg] = max(col[i][lg-1], col[i+pw[lg-1]][lg-1]);
for(int S, T; Q; Q--) {
cin >> S >> T;
long long ans = 0;
if (S < H) ans = max(ans, DFS(S+1, T, 1) + 1);
if (S > 1) ans = max(ans, DFS(S-1, T, 0) + 1);
if (T < W) ans = max(ans, DFS(S, T+1, 3) + 1);
if (T > 1) ans = max(ans, DFS(S, T-1, 2) + 1);
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
3 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
1144 KB |
Output is correct |
21 |
Correct |
6 ms |
1144 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
3 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
1144 KB |
Output is correct |
21 |
Correct |
6 ms |
1144 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
29 ms |
8184 KB |
Output is correct |
24 |
Correct |
29 ms |
8188 KB |
Output is correct |
25 |
Correct |
29 ms |
8184 KB |
Output is correct |
26 |
Correct |
29 ms |
8184 KB |
Output is correct |
27 |
Correct |
29 ms |
8184 KB |
Output is correct |
28 |
Correct |
64 ms |
14328 KB |
Output is correct |
29 |
Correct |
35 ms |
9084 KB |
Output is correct |
30 |
Correct |
173 ms |
18664 KB |
Output is correct |
31 |
Correct |
227 ms |
21804 KB |
Output is correct |
32 |
Correct |
34 ms |
8696 KB |
Output is correct |
33 |
Correct |
62 ms |
11384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
1016 KB |
Output is correct |
3 |
Correct |
8 ms |
1016 KB |
Output is correct |
4 |
Correct |
7 ms |
1016 KB |
Output is correct |
5 |
Correct |
8 ms |
1016 KB |
Output is correct |
6 |
Correct |
5 ms |
1016 KB |
Output is correct |
7 |
Correct |
5 ms |
1016 KB |
Output is correct |
8 |
Correct |
11 ms |
1528 KB |
Output is correct |
9 |
Correct |
11 ms |
1528 KB |
Output is correct |
10 |
Correct |
11 ms |
1400 KB |
Output is correct |
11 |
Correct |
13 ms |
1760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
3 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
1144 KB |
Output is correct |
21 |
Correct |
6 ms |
1144 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
29 ms |
8184 KB |
Output is correct |
24 |
Correct |
29 ms |
8188 KB |
Output is correct |
25 |
Correct |
29 ms |
8184 KB |
Output is correct |
26 |
Correct |
29 ms |
8184 KB |
Output is correct |
27 |
Correct |
29 ms |
8184 KB |
Output is correct |
28 |
Correct |
64 ms |
14328 KB |
Output is correct |
29 |
Correct |
35 ms |
9084 KB |
Output is correct |
30 |
Correct |
173 ms |
18664 KB |
Output is correct |
31 |
Correct |
227 ms |
21804 KB |
Output is correct |
32 |
Correct |
34 ms |
8696 KB |
Output is correct |
33 |
Correct |
62 ms |
11384 KB |
Output is correct |
34 |
Correct |
7 ms |
1016 KB |
Output is correct |
35 |
Correct |
7 ms |
1016 KB |
Output is correct |
36 |
Correct |
8 ms |
1016 KB |
Output is correct |
37 |
Correct |
7 ms |
1016 KB |
Output is correct |
38 |
Correct |
8 ms |
1016 KB |
Output is correct |
39 |
Correct |
5 ms |
1016 KB |
Output is correct |
40 |
Correct |
5 ms |
1016 KB |
Output is correct |
41 |
Correct |
11 ms |
1528 KB |
Output is correct |
42 |
Correct |
11 ms |
1528 KB |
Output is correct |
43 |
Correct |
11 ms |
1400 KB |
Output is correct |
44 |
Correct |
13 ms |
1760 KB |
Output is correct |
45 |
Correct |
37 ms |
8696 KB |
Output is correct |
46 |
Correct |
37 ms |
8756 KB |
Output is correct |
47 |
Correct |
38 ms |
8796 KB |
Output is correct |
48 |
Correct |
36 ms |
8696 KB |
Output is correct |
49 |
Correct |
37 ms |
8712 KB |
Output is correct |
50 |
Correct |
72 ms |
14972 KB |
Output is correct |
51 |
Correct |
81 ms |
15600 KB |
Output is correct |
52 |
Correct |
276 ms |
27256 KB |
Output is correct |
53 |
Correct |
271 ms |
26540 KB |
Output is correct |
54 |
Correct |
267 ms |
25336 KB |
Output is correct |
55 |
Correct |
448 ms |
34640 KB |
Output is correct |
56 |
Correct |
3688 ms |
259032 KB |
Output is correct |
57 |
Correct |
866 ms |
73552 KB |
Output is correct |
58 |
Correct |
799 ms |
70340 KB |
Output is correct |
59 |
Correct |
802 ms |
69972 KB |
Output is correct |