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;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,int>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
void solve(){
int n,m,q;
cin >> n >> m >> q;
int hori[n];
vector<pair<int,pii>>v; //val index row/column
for(int x=0;x<n;x++){
cin >> hori[x];
v.push_back({hori[x],{x,0}});
}
int vert[m];
for(int x=0;x<m;x++){
cin >> vert[x];
v.push_back({vert[x],{x,1}});
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
int ans[n][m];
memset(ans,-1,sizeof(ans));
bool done[n][m];
memset(done,0,sizeof(done));
for(int x=0;x<n+m;x++){
pair<int,pii>cur=v[x];
if(cur.second.second==0){
//hori
int index=cur.second.first;
int hold=-1;
for(int y=0;y<m;y++){
if(done[index][y]==1){
hold=ans[index][y];
}
else hold++;
ans[index][y]=max(ans[index][y],hold);
}
hold=-1;
for(int y=m-1;y>=0;y--){
if(done[index][y]==1){
hold=ans[index][y];
}
else hold++;
done[index][y]=true;
ans[index][y]=max(ans[index][y],hold);
}
}
else{
//vert
int index=cur.second.first;
int hold=-1;
for(int y=0;y<n;y++){
if(done[y][index]==1){
hold=ans[y][index];
}
else hold++;
ans[y][index]=max(ans[y][index],hold);
}
hold=-1;
for(int y=n-1;y>=0;y--){
if(done[y][index]==1){
hold=ans[y][index];
}
else hold++;
done[y][index]=true;
ans[y][index]=max(ans[y][index],hold);
}
}
}
int up[n][m];
int down[n][m];
int lft[n][m];
int rgt[n][m];
memset(up,-1,sizeof(up));
memset(down,-1,sizeof(down));
memset(lft,-1,sizeof(lft));
memset(rgt,-1,sizeof(rgt));
for(int y=0;y<m;y++){
int cur=-1;
for(int x=0;x<n;x++){
up[x][y]=cur;
if(hori[x]>vert[y]) cur=x;
}
cur=-1;
for(int x=n-1;x>=0;x--){
down[x][y]=cur;
if(hori[x]>vert[y]) cur=x;
}
}
for(int x=0;x<n;x++){
int cur=-1;
for(int y=0;y<m;y++){
lft[x][y]=cur;
if(vert[y]>hori[x]) cur=y;
}
cur=-1;
for(int y=m-1;y>=0;y--){
rgt[x][y]=cur;
if(vert[y]>hori[x]) cur=y;
}
}
//for(int x=0;x<n;x++){
//for(int y=0;y<m;y++){
//cout << ans[x][y] << " ";
//}
//cout << "\n";
//}
int temp,temp2;
for(int x=0;x<q;x++){
cin >> temp >> temp2;
temp--; temp2--;
//up
int take=0;
if(up[temp][temp2]==-1) take=max(take,temp);
else{
int index=up[temp][temp2];
take=max(take,ans[index][temp2]+abs(index-temp));
}
if(down[temp][temp2]==-1) take=max(take,n-1-temp);
else{
int index=down[temp][temp2];
take=max(take,ans[index][temp2]+abs(index-temp));
}
if(lft[temp][temp2]==-1) take=max(take,temp2);
else{
int index=lft[temp][temp2];
take=max(take,ans[temp][index]+abs(index-temp2));
}
if(rgt[temp][temp2]==-1) take=max(take,m-1-temp2);
else{
int index=rgt[temp][temp2];
take=max(take,ans[temp][index]+abs(index-temp2));
}
take=max(take,ans[temp][temp2]);
cout << take << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
//freopen("in.txt","r",stdin);
while(t--){
solve();
}
}
# | 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... |