#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>, int > dp;
int max(int a, int b, int c, int d) {
return max( max(a, b), max(c, d) );
}
int 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];
int &ans = dp[cur_state];
if (dir == 0 || dir == 1) {
int sign = (dir == 0 ? -1 : 1);
int nxt = -1;
int 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) );
}
int sign = (dir == 2 ? -1 : 1);
int nxt = -1;
int 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() {
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;
int 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 |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 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 |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 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 |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
800 KB |
Output is correct |
16 |
Correct |
4 ms |
680 KB |
Output is correct |
17 |
Correct |
3 ms |
760 KB |
Output is correct |
18 |
Correct |
3 ms |
764 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
8 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1144 KB |
Output is correct |
22 |
Correct |
10 ms |
1500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 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 |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
800 KB |
Output is correct |
16 |
Correct |
4 ms |
680 KB |
Output is correct |
17 |
Correct |
3 ms |
760 KB |
Output is correct |
18 |
Correct |
3 ms |
764 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
8 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1144 KB |
Output is correct |
22 |
Correct |
10 ms |
1500 KB |
Output is correct |
23 |
Correct |
29 ms |
9080 KB |
Output is correct |
24 |
Correct |
29 ms |
9128 KB |
Output is correct |
25 |
Correct |
29 ms |
9124 KB |
Output is correct |
26 |
Correct |
30 ms |
9080 KB |
Output is correct |
27 |
Correct |
36 ms |
9180 KB |
Output is correct |
28 |
Correct |
68 ms |
15204 KB |
Output is correct |
29 |
Correct |
34 ms |
9932 KB |
Output is correct |
30 |
Correct |
181 ms |
19788 KB |
Output is correct |
31 |
Correct |
234 ms |
22648 KB |
Output is correct |
32 |
Correct |
34 ms |
9592 KB |
Output is correct |
33 |
Correct |
65 ms |
12408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
1272 KB |
Output is correct |
3 |
Correct |
7 ms |
1016 KB |
Output is correct |
4 |
Correct |
7 ms |
1016 KB |
Output is correct |
5 |
Correct |
7 ms |
1016 KB |
Output is correct |
6 |
Correct |
5 ms |
1192 KB |
Output is correct |
7 |
Correct |
5 ms |
1016 KB |
Output is correct |
8 |
Correct |
12 ms |
1596 KB |
Output is correct |
9 |
Correct |
12 ms |
1528 KB |
Output is correct |
10 |
Correct |
12 ms |
1528 KB |
Output is correct |
11 |
Correct |
13 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
248 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 |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
3 ms |
800 KB |
Output is correct |
16 |
Correct |
4 ms |
680 KB |
Output is correct |
17 |
Correct |
3 ms |
760 KB |
Output is correct |
18 |
Correct |
3 ms |
764 KB |
Output is correct |
19 |
Correct |
6 ms |
1016 KB |
Output is correct |
20 |
Correct |
8 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1144 KB |
Output is correct |
22 |
Correct |
10 ms |
1500 KB |
Output is correct |
23 |
Correct |
29 ms |
9080 KB |
Output is correct |
24 |
Correct |
29 ms |
9128 KB |
Output is correct |
25 |
Correct |
29 ms |
9124 KB |
Output is correct |
26 |
Correct |
30 ms |
9080 KB |
Output is correct |
27 |
Correct |
36 ms |
9180 KB |
Output is correct |
28 |
Correct |
68 ms |
15204 KB |
Output is correct |
29 |
Correct |
34 ms |
9932 KB |
Output is correct |
30 |
Correct |
181 ms |
19788 KB |
Output is correct |
31 |
Correct |
234 ms |
22648 KB |
Output is correct |
32 |
Correct |
34 ms |
9592 KB |
Output is correct |
33 |
Correct |
65 ms |
12408 KB |
Output is correct |
34 |
Correct |
7 ms |
1016 KB |
Output is correct |
35 |
Correct |
7 ms |
1272 KB |
Output is correct |
36 |
Correct |
7 ms |
1016 KB |
Output is correct |
37 |
Correct |
7 ms |
1016 KB |
Output is correct |
38 |
Correct |
7 ms |
1016 KB |
Output is correct |
39 |
Correct |
5 ms |
1192 KB |
Output is correct |
40 |
Correct |
5 ms |
1016 KB |
Output is correct |
41 |
Correct |
12 ms |
1596 KB |
Output is correct |
42 |
Correct |
12 ms |
1528 KB |
Output is correct |
43 |
Correct |
12 ms |
1528 KB |
Output is correct |
44 |
Correct |
13 ms |
1784 KB |
Output is correct |
45 |
Correct |
37 ms |
9720 KB |
Output is correct |
46 |
Correct |
37 ms |
9724 KB |
Output is correct |
47 |
Correct |
42 ms |
9720 KB |
Output is correct |
48 |
Correct |
41 ms |
9776 KB |
Output is correct |
49 |
Correct |
38 ms |
9720 KB |
Output is correct |
50 |
Correct |
76 ms |
15864 KB |
Output is correct |
51 |
Correct |
80 ms |
16560 KB |
Output is correct |
52 |
Correct |
283 ms |
28156 KB |
Output is correct |
53 |
Correct |
282 ms |
27524 KB |
Output is correct |
54 |
Correct |
274 ms |
26360 KB |
Output is correct |
55 |
Incorrect |
451 ms |
35448 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |