#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<pair<int,int>,int> piii;
map<piii,ll> dp;
int A[50010],B[50010];
int a1[50010][17],a2[50010][17],b1[50010][17],b2[50010][17];
int h,w,q;
ll dfs(int a,int b,int d){
//printf("--%d %d %d\n",a,b,d);
if(dp.find({{a,b},d})!=dp.end()) return dp[{{a,b},d}];
int c=(d>=2?A[a]:B[b]); ll re=0;
// printf("%d\n",c);
if(d==0){
int x=a+1;
for(int i=16;i>=0;i--){
if(x>=h) break;
if(a1[x][i]<c) x+=(1<<i);
}
if(x>=h) re=h-1-a;
else re=max(dfs(x,b,2),dfs(x,b,3))+x-a;
}
else if(d==1){
int x=h-a;
for(int i=16;i>=0;i--){
if(x>=h) break;
if(a2[x][i]<c) x+=(1<<i);
}
if(x>=h) re=a;
else re=max(dfs(h-1-x,b,2),dfs(h-1-x,b,3))+a-(h-1-x);
}
else if(d==2){
int x=b+1;
for(int i=16;i>=0;i--){
if(x>=w) break;
if(b1[x][i]<c) x+=(1<<i);
}
if(x>=w) re=w-1-b;
else re=max(dfs(a,x,0),dfs(a,x,1))+x-b;
}
else{
int x=w-b;
for(int i=16;i>=0;i--){
if(x>=w) break;
if(b2[x][i]<c) x+=(1<<i);
}
if(x>=w) re=b;
else re=max(dfs(a,w-1-x,0),dfs(a,w-1-x,1))+b-(w-1-x);
}
// printf("%d %d %d %lld\n",a,b,d,re);
return dp[{{a,b},d}]=re;
}
int main()
{
scanf("%d %d %d",&h,&w,&q);
for(int i=0;i<h;i++){scanf("%d",&A[i]); a1[i][0]=a2[h-1-i][0]=A[i];}
for(int i=0;i<w;i++){scanf("%d",&B[i]); b1[i][0]=b2[w-1-i][0]=B[i];}
for(int i=1;i<17;i++){
for(int j=0;j<h;j++){
a1[j][i]=max(a1[j][i-1],a1[min(j+(1<<(i-1)),h)][i-1]);
a2[j][i]=max(a2[j][i-1],a2[min(j+(1<<(i-1)),h)][i-1]);
}
for(int j=0;j<w;j++){
b1[j][i]=max(b1[j][i-1],b1[min(j+(1<<(i-1)),w)][i-1]);
b2[j][i]=max(b2[j][i-1],b2[min(j+(1<<(i-1)),w)][i-1]);
}
}
for(int i=0;i<q;i++){
int a,b; scanf("%d %d",&a,&b);
ll re=0;
for(int j=0;j<4;j++) re=max(re,dfs(a-1,b-1,j));
printf("%lld\n",re);
}
return 0;
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d %d %d",&h,&w,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:58:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | for(int i=0;i<h;i++){scanf("%d",&A[i]); a1[i][0]=a2[h-1-i][0]=A[i];}
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:59:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | for(int i=0;i<w;i++){scanf("%d",&B[i]); b1[i][0]=b2[w-1-i][0]=B[i];}
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:71:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | int a,b; scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
2 ms |
844 KB |
Output is correct |
13 |
Correct |
3 ms |
844 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
3 ms |
824 KB |
Output is correct |
16 |
Correct |
2 ms |
824 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
5 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
9 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
2 ms |
844 KB |
Output is correct |
13 |
Correct |
3 ms |
844 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
3 ms |
824 KB |
Output is correct |
16 |
Correct |
2 ms |
824 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
5 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
9 ms |
1740 KB |
Output is correct |
23 |
Correct |
40 ms |
14952 KB |
Output is correct |
24 |
Correct |
42 ms |
14864 KB |
Output is correct |
25 |
Correct |
40 ms |
14924 KB |
Output is correct |
26 |
Correct |
42 ms |
14956 KB |
Output is correct |
27 |
Correct |
41 ms |
14904 KB |
Output is correct |
28 |
Correct |
67 ms |
22616 KB |
Output is correct |
29 |
Correct |
42 ms |
15960 KB |
Output is correct |
30 |
Correct |
151 ms |
26228 KB |
Output is correct |
31 |
Correct |
179 ms |
29500 KB |
Output is correct |
32 |
Correct |
45 ms |
15336 KB |
Output is correct |
33 |
Correct |
80 ms |
18412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1228 KB |
Output is correct |
2 |
Correct |
5 ms |
1228 KB |
Output is correct |
3 |
Correct |
7 ms |
1228 KB |
Output is correct |
4 |
Correct |
5 ms |
1228 KB |
Output is correct |
5 |
Correct |
5 ms |
1208 KB |
Output is correct |
6 |
Correct |
4 ms |
1228 KB |
Output is correct |
7 |
Correct |
4 ms |
1228 KB |
Output is correct |
8 |
Correct |
10 ms |
1716 KB |
Output is correct |
9 |
Correct |
9 ms |
1788 KB |
Output is correct |
10 |
Correct |
10 ms |
1624 KB |
Output is correct |
11 |
Correct |
12 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
2 ms |
844 KB |
Output is correct |
13 |
Correct |
3 ms |
844 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
3 ms |
824 KB |
Output is correct |
16 |
Correct |
2 ms |
824 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
5 ms |
1228 KB |
Output is correct |
20 |
Correct |
6 ms |
1484 KB |
Output is correct |
21 |
Correct |
6 ms |
1356 KB |
Output is correct |
22 |
Correct |
9 ms |
1740 KB |
Output is correct |
23 |
Correct |
40 ms |
14952 KB |
Output is correct |
24 |
Correct |
42 ms |
14864 KB |
Output is correct |
25 |
Correct |
40 ms |
14924 KB |
Output is correct |
26 |
Correct |
42 ms |
14956 KB |
Output is correct |
27 |
Correct |
41 ms |
14904 KB |
Output is correct |
28 |
Correct |
67 ms |
22616 KB |
Output is correct |
29 |
Correct |
42 ms |
15960 KB |
Output is correct |
30 |
Correct |
151 ms |
26228 KB |
Output is correct |
31 |
Correct |
179 ms |
29500 KB |
Output is correct |
32 |
Correct |
45 ms |
15336 KB |
Output is correct |
33 |
Correct |
80 ms |
18412 KB |
Output is correct |
34 |
Correct |
6 ms |
1228 KB |
Output is correct |
35 |
Correct |
5 ms |
1228 KB |
Output is correct |
36 |
Correct |
7 ms |
1228 KB |
Output is correct |
37 |
Correct |
5 ms |
1228 KB |
Output is correct |
38 |
Correct |
5 ms |
1208 KB |
Output is correct |
39 |
Correct |
4 ms |
1228 KB |
Output is correct |
40 |
Correct |
4 ms |
1228 KB |
Output is correct |
41 |
Correct |
10 ms |
1716 KB |
Output is correct |
42 |
Correct |
9 ms |
1788 KB |
Output is correct |
43 |
Correct |
10 ms |
1624 KB |
Output is correct |
44 |
Correct |
12 ms |
1884 KB |
Output is correct |
45 |
Correct |
51 ms |
15452 KB |
Output is correct |
46 |
Correct |
48 ms |
15436 KB |
Output is correct |
47 |
Correct |
49 ms |
15428 KB |
Output is correct |
48 |
Correct |
47 ms |
15436 KB |
Output is correct |
49 |
Correct |
47 ms |
15516 KB |
Output is correct |
50 |
Correct |
80 ms |
22596 KB |
Output is correct |
51 |
Correct |
81 ms |
23400 KB |
Output is correct |
52 |
Correct |
243 ms |
34580 KB |
Output is correct |
53 |
Correct |
236 ms |
33580 KB |
Output is correct |
54 |
Correct |
225 ms |
32584 KB |
Output is correct |
55 |
Correct |
432 ms |
42244 KB |
Output is correct |
56 |
Correct |
3531 ms |
265124 KB |
Output is correct |
57 |
Correct |
871 ms |
79516 KB |
Output is correct |
58 |
Correct |
795 ms |
76444 KB |
Output is correct |
59 |
Correct |
862 ms |
75832 KB |
Output is correct |