This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |