#include<bits/stdc++.h>
using namespace std;
#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define _ << " " <<
template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}
const int N=5e4+5;
const int LG=15;
int h,w,q,a[N],b[N];
unordered_map<int,int> dp[2][N];
struct SparseTable{
int len,mx[LG+1][N];
void init(int _len, int _a[]){
len=_len;
foru(i,1,len) mx[0][i]=_a[i];
for(int i=1; (1<<i)<=len; ++i){
for(int j=1; j+(1<<i)-1<=len; ++j){
mx[i][j]=max(mx[i-1][j],mx[i-1][j+(1<<i-1)]);
}
}
}
int get(int l, int r){
int lg=__lg(r-l+1);
return max(mx[lg][l],mx[lg][r-(1<<lg)+1]);
}
int getRight(int x, int val){
int L=x+1,R=len,res=len+1;
while(L<=R){
int M=(L+R)>>1;
if(get(L,M)>val){
res=M;
R=M-1;
}else{
L=M+1;
}
}
return res;
}
int getLeft(int x, int val){
int L=1,R=x-1,res=0;
while(L<=R){
int M=(L+R)>>1;
if(get(M,x-1)>val){
res=M;
L=M+1;
}else{
R=M-1;
}
}
return res;
}
} hh,ww;
int calc(int s, int t, int d){
if(s==0 || t==0 || s>h || t>w) return -1;
if(dp[d][s].find(t)!=dp[d][s].end()) return dp[d][s][t];
int &res=dp[d][s][t];
if(d==0){
int lt=ww.getLeft(t,a[s]), rt=ww.getRight(t,a[s]);
res=max(calc(s,lt,d^1)+abs(lt-t), calc(s,rt,d^1)+abs(rt-t));
} else{
int ls=hh.getLeft(s,b[t]), rs=hh.getRight(s,b[t]);
// if(s==3&&t==1)cout<<ls _ rs<<'\n';
res=max(calc(ls,t,d^1)+abs(ls-s), calc(rs,t,d^1)+abs(rs-s));
}
// printf("State: (%d,%d,%d), Answer: %d\n",s,t,d,res);
return res;
}
void solve(){
cin>>h>>w>>q;
foru(i,1,h) cin>>a[i];
foru(i,1,w) cin>>b[i];
hh.init(h,a);
ww.init(w,b);
foru(i,1,q){
int s,t; cin>>s>>t;
cout<<max(calc(s,t,0),calc(s,t,1))<<'\n';
}
}
int32_t main(){
#define task "test"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
rep(i,tc){
solve();
}
}
Compilation message (stderr)
abduction2.cpp: In function 'int32_t main()':
abduction2.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |