#include <stdio.h>
#include <map>
static inline int min(int a, int b) {
return a < b ? a : b;
}
static inline int max(int a, int 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("%d\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 |
0 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 |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2656 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2656 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 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 |
4 ms |
5052 KB |
Output is correct |
21 |
Correct |
3 ms |
5052 KB |
Output is correct |
22 |
Correct |
3 ms |
5328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2656 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 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 |
4 ms |
5052 KB |
Output is correct |
21 |
Correct |
3 ms |
5052 KB |
Output is correct |
22 |
Correct |
3 ms |
5328 KB |
Output is correct |
23 |
Correct |
17 ms |
7412 KB |
Output is correct |
24 |
Correct |
10 ms |
7516 KB |
Output is correct |
25 |
Correct |
10 ms |
7516 KB |
Output is correct |
26 |
Correct |
11 ms |
7516 KB |
Output is correct |
27 |
Correct |
9 ms |
7584 KB |
Output is correct |
28 |
Correct |
21 ms |
13556 KB |
Output is correct |
29 |
Correct |
11 ms |
8280 KB |
Output is correct |
30 |
Correct |
45 ms |
14160 KB |
Output is correct |
31 |
Correct |
66 ms |
16152 KB |
Output is correct |
32 |
Correct |
12 ms |
7772 KB |
Output is correct |
33 |
Correct |
30 ms |
9496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4820 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 |
2 ms |
5056 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
4 ms |
5208 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 |
0 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 |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2656 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 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 |
4 ms |
5052 KB |
Output is correct |
21 |
Correct |
3 ms |
5052 KB |
Output is correct |
22 |
Correct |
3 ms |
5328 KB |
Output is correct |
23 |
Correct |
17 ms |
7412 KB |
Output is correct |
24 |
Correct |
10 ms |
7516 KB |
Output is correct |
25 |
Correct |
10 ms |
7516 KB |
Output is correct |
26 |
Correct |
11 ms |
7516 KB |
Output is correct |
27 |
Correct |
9 ms |
7584 KB |
Output is correct |
28 |
Correct |
21 ms |
13556 KB |
Output is correct |
29 |
Correct |
11 ms |
8280 KB |
Output is correct |
30 |
Correct |
45 ms |
14160 KB |
Output is correct |
31 |
Correct |
66 ms |
16152 KB |
Output is correct |
32 |
Correct |
12 ms |
7772 KB |
Output is correct |
33 |
Correct |
30 ms |
9496 KB |
Output is correct |
34 |
Correct |
3 ms |
4956 KB |
Output is correct |
35 |
Correct |
3 ms |
4820 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 |
2 ms |
5056 KB |
Output is correct |
40 |
Correct |
2 ms |
4956 KB |
Output is correct |
41 |
Correct |
4 ms |
5208 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 |
22 ms |
7772 KB |
Output is correct |
46 |
Correct |
12 ms |
7768 KB |
Output is correct |
47 |
Correct |
11 ms |
7772 KB |
Output is correct |
48 |
Correct |
22 ms |
7772 KB |
Output is correct |
49 |
Correct |
11 ms |
7772 KB |
Output is correct |
50 |
Correct |
20 ms |
12568 KB |
Output is correct |
51 |
Correct |
22 ms |
13392 KB |
Output is correct |
52 |
Correct |
61 ms |
18064 KB |
Output is correct |
53 |
Correct |
54 ms |
17748 KB |
Output is correct |
54 |
Correct |
55 ms |
17240 KB |
Output is correct |
55 |
Incorrect |
94 ms |
22804 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |