#include <stdio.h>
#include <map>
static inline long long min(long long a, long long b) {
return a < b ? a : b;
}
static inline long long max(long long a, long long b) {
return a > b ? a : b;
}
#define MAXN 50000
#define LOGN 16
#define VERTICAL 0
#define ORIZONTAL 1
int n, m, a[LOGN][MAXN], b[LOGN][MAXN], lg2[MAXN + 1];
std::map<long long, long long> memo;
long long solve(int l, int c, int stare) {
int i, sus, jos, st, dr;
long long ans;
if(l < 0 || c < 0 || l >= n || c >= m) {
return -1;
}
if(memo.count((1LL * l * m + c) * 2 + stare)) {
return memo[(1LL * l * m + c) * 2 + stare];
}
ans = 0;
if(stare == VERTICAL) {
sus = l - 1;
jos = l + 1;
for(i = LOGN - 1; i >= 0; i--) {
if(sus - (1 << i) + 1 >= 0 && a[i][sus - (1 << i) + 1]
<= b[0][c]) {
sus -= 1 << i;
}
if(jos + (1 << i) - 1 < n && a[i][jos] <= b[0][c]) {
jos += 1 << i;
}
}
ans = max(ans, solve(sus, c, ORIZONTAL) + l - sus);
ans = max(ans, solve(jos, c, ORIZONTAL) + jos - l);
} else {
st = c - 1;
dr = c + 1;
for(i = LOGN - 1; i >= 0; i--) {
if(st - (1 << i) + 1 >= 0 && b[i][st - (1 << i) + 1]
<= a[0][l]) {
st -= 1 << i;
}
if(dr + (1 << i) - 1 < m && b[i][dr] <= a[0][l]) {
dr += 1 << i;
}
}
ans = max(ans, solve(l, st, VERTICAL) + c - st);
ans = max(ans, solve(l, dr, VERTICAL) + dr - c);
}
return memo[(1LL * l * m + c) * 2 + stare] = ans;
}
int main() {
int q, i, j;
scanf("%d%d%d", &n, &m, &q);
for(i = 0; i < n; i++) {
scanf("%d", &a[0][i]);
}
for(i = 0; i < m; i++) {
scanf("%d", &b[0][i]);
}
for(i = 2; i <= MAXN; i++) {
lg2[i] = 1 + lg2[i >> 1];
}
for(i = 1; i <= lg2[n]; i++) {
for(j = 0; j + (1 << i) <= n; j++) {
a[i][j] = max(a[i - 1][j], a[i - 1][j + (1 << (i - 1))]);
}
}
for(i = 1; i <= lg2[m]; i++) {
for(j = 0; j + (1 << i) <= m; j++) {
b[i][j] = max(b[i - 1][j], b[i - 1][j + (1 << (i - 1))]);
}
}
while(q--) {
scanf("%d%d", &i, &j);
i--;
j--;
printf("%lld\n", max(solve(i, j, VERTICAL),
solve(i, j, ORIZONTAL)));
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d%d", &n, &m, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &a[0][i]);
| ~~~~~^~~~~~~~~~~~~~~~
abduction2.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d", &b[0][i]);
| ~~~~~^~~~~~~~~~~~~~~~
abduction2.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d%d", &i, &j);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4784 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4784 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
10 ms |
6492 KB |
Output is correct |
24 |
Correct |
9 ms |
6492 KB |
Output is correct |
25 |
Correct |
9 ms |
6572 KB |
Output is correct |
26 |
Correct |
9 ms |
6572 KB |
Output is correct |
27 |
Correct |
10 ms |
6492 KB |
Output is correct |
28 |
Correct |
17 ms |
12800 KB |
Output is correct |
29 |
Correct |
10 ms |
7480 KB |
Output is correct |
30 |
Correct |
38 ms |
13392 KB |
Output is correct |
31 |
Correct |
48 ms |
15128 KB |
Output is correct |
32 |
Correct |
11 ms |
7000 KB |
Output is correct |
33 |
Correct |
17 ms |
8624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
1 ms |
4956 KB |
Output is correct |
8 |
Correct |
4 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
0 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4784 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
10 ms |
6492 KB |
Output is correct |
24 |
Correct |
9 ms |
6492 KB |
Output is correct |
25 |
Correct |
9 ms |
6572 KB |
Output is correct |
26 |
Correct |
9 ms |
6572 KB |
Output is correct |
27 |
Correct |
10 ms |
6492 KB |
Output is correct |
28 |
Correct |
17 ms |
12800 KB |
Output is correct |
29 |
Correct |
10 ms |
7480 KB |
Output is correct |
30 |
Correct |
38 ms |
13392 KB |
Output is correct |
31 |
Correct |
48 ms |
15128 KB |
Output is correct |
32 |
Correct |
11 ms |
7000 KB |
Output is correct |
33 |
Correct |
17 ms |
8624 KB |
Output is correct |
34 |
Correct |
2 ms |
4956 KB |
Output is correct |
35 |
Correct |
2 ms |
4940 KB |
Output is correct |
36 |
Correct |
2 ms |
4956 KB |
Output is correct |
37 |
Correct |
2 ms |
4956 KB |
Output is correct |
38 |
Correct |
2 ms |
4956 KB |
Output is correct |
39 |
Correct |
3 ms |
4956 KB |
Output is correct |
40 |
Correct |
1 ms |
4956 KB |
Output is correct |
41 |
Correct |
4 ms |
5212 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
11 ms |
6748 KB |
Output is correct |
46 |
Correct |
11 ms |
6908 KB |
Output is correct |
47 |
Correct |
12 ms |
6748 KB |
Output is correct |
48 |
Correct |
11 ms |
6888 KB |
Output is correct |
49 |
Correct |
18 ms |
6744 KB |
Output is correct |
50 |
Correct |
21 ms |
11604 KB |
Output is correct |
51 |
Correct |
22 ms |
12372 KB |
Output is correct |
52 |
Correct |
62 ms |
17100 KB |
Output is correct |
53 |
Correct |
72 ms |
16724 KB |
Output is correct |
54 |
Correct |
55 ms |
16468 KB |
Output is correct |
55 |
Correct |
96 ms |
21660 KB |
Output is correct |
56 |
Correct |
746 ms |
132692 KB |
Output is correct |
57 |
Correct |
190 ms |
39916 KB |
Output is correct |
58 |
Correct |
202 ms |
38220 KB |
Output is correct |
59 |
Correct |
198 ms |
38124 KB |
Output is correct |