#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
struct pi{int a,b,c;};
bool operator<(pi a,pi b){
if(a.a==b.a) return a.b==b.b?a.c<b.c:a.b<b.b;
return a.a<b.a;
}
map<pi,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){
if(dp.find({a,b,d})!=dp.end()) return dp[{a,b,d}];
int c=(d>=2?A[a]:B[b]);
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) return dp[{a,b,d}]=h-1-a;
return dp[{a,b,d}]=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) return dp[{a,b,d}]=a;
return dp[{a,b,d}]=max(dfs(x,b,2),dfs(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) return dp[{a,b,d}]=w-1-b;
return dp[{a,b,d}]=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) return dp[{a,b,d}]=b;
return dp[{a,b,d}]=max(dfs(a,x,0),dfs(a,x,1))+b-(w-1-x);
}
}
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 |
Runtime error |
418 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
418 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
418 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
396 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
418 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |