#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
typedef pair<int,int> pii;
const int MAXN = 5e4+5;
vector<int>a,b;
int h,w,q;
map<int,int>dp[2][MAXN];
int lg[MAXN];
struct spt{
vector<int>a;
vector<vector<int>>mx;
int n;
spt(){}
spt(vector<int>_a){
a = _a;
n = a.size();
mx.resize(20);
for(int i=0;i<20;i++)mx[i].assign(n,-1e9);
for(int i=0;i<n;i++)mx[0][i] = a[i];
for(int i=1;i<20;i++){
for(int j=0;j+(1<<i)-1<n;j++){
mx[i][j] = max(mx[i-1][j],mx[i-1][j+(1<<(i-1))]);
}
}
}
int query(int l,int r){
int v = lg[r-l+1];
int res = max(mx[v][l],mx[v][r-(1<<v)+1]);
return res;
}
void print(){
for(int i=0;i<20;i++){
for(int j=0;j+(1<<i)-1<n;j++)cout<<mx[i][j]<<" ";
cout<<'\n';
}
}
}mw,mh;
int solve(int i,int j,int k){
if(dp[k][i].find(j) != dp[k][i].end())return dp[k][i][j];
if(!k){
dp[0][i][j] = -1e9;
int l = j+1,r = w-1;
while(r>=l){
int mid = (l+r)/2;
if(mw.query(j+1,mid) > a[i])r=mid-1;
else l = mid+1;
}
if(l > j && l<w)dp[0][i][j] = max(dp[0][i][j],solve(i,l,1) + l - j);
for(int jj=j-1;jj>=0;jj--){
if(b[jj] > a[i]){
dp[0][i][j] = max(dp[0][i][j],solve(i,jj,1)+j-jj);
break;
}
}
if(dp[0][i][j] == -1e9)dp[0][i][j] = max(w-1-j,j);
}else{
int l = i+1,r = h-1;
while(r>=l){
int mid = (l+r)/2;
if(mh.query(i+1,mid) > b[j])r=mid-1;
else l = mid+1;
}
if(l>i && l<h)dp[1][i][j] = max(dp[1][i][j],solve(l,j,0) + l - i);
for(int ii=i-1;ii>=0;ii--){
if(a[ii] > b[j]){
dp[1][i][j] = max(dp[1][i][j],solve(ii,j,0) + i-ii);
break;
}
}
if(dp[1][i][j] == -1e9)dp[1][i][j] = max(h-1-i,i);
}
return dp[k][i][j];
}
int main()
{
cin>>h>>w>>q;
a.resize(h);
b.resize(w);
for(int i=0;i<h;i++)cin>>a[i];
for(int i=0;i<w;i++)cin>>b[i];
lg[1] = 0;
for(int i=2;i<=50000;i++)lg[i] = lg[i/2]+1;
mh = spt(a);
mw = spt(b);
//mh.print();
//mw.print();
while(q--){
int i,j;
cin>>i>>j;
i--;
j--;
int ans = max(solve(i,j,0),solve(i,j,1));
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5588 KB |
Output is correct |
2 |
Correct |
6 ms |
5716 KB |
Output is correct |
3 |
Incorrect |
7 ms |
5708 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |