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 ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e4;
int N, M, Q;
int A[MAXN+10], B[MAXN+10];
int LA[MAXN+10][20], RA[MAXN+10][20];
int LB[MAXN+10][20], RB[MAXN+10][20];
map<pair<pii, int>, int> memo;
int solve(int y, int x, int d)
{
if(memo.find({{y, x}, d})!=memo.end()) return memo[{{y, x}, d}];
int i, j;
int ret=0;
int l, r, k;
if(d==0)
{
l=y-1; r=y+1; k=B[x];
for(i=16; i>=0; i--) if(LA[l][i]<=k) l=max(0, l-(1<<i));
for(i=16; i>=0; i--) if(RA[r][i]<=k) r=min(N+1, r+(1<<i));
if(l==0) ret=max(ret, y-1);
else ret=max(ret, y-l+solve(l, x, 1));
if(r==N+1) ret=max(ret, N-y);
else ret=max(ret, r-y+solve(r, x, 1));
}
else
{
l=x-1; r=x+1; k=A[y];
for(i=16; i>=0; i--) if(LB[l][i]<=k) l=max(0, l-(1<<i));
for(i=16; i>=0; i--) if(RB[r][i]<=k) r=min(M+1, r+(1<<i));
if(l==0) ret=max(ret, x-1);
else ret=max(ret, x-l+solve(y, l, 0));
if(r==M+1) ret=max(ret, M-x);
else ret=max(ret, r-x+solve(y, r, 0));
}
memo[{{y, x}, d}]=ret;
return ret;
}
int main()
{
int i, j;
scanf("%d%d%d", &N, &M, &Q);
for(i=1; i<=N; i++) scanf("%d", &A[i]);
for(i=1; i<=M; i++) scanf("%d", &B[i]);
for(i=1; i<=N; i++) LA[i][0]=RA[i][0]=A[i];
for(i=1; i<=16; i++) for(j=1; j<=N; j++) LA[j][i]=max(LA[j][i-1], LA[max(0, j-(1<<(i-1)))][i-1]);
for(i=1; i<=16; i++) for(j=1; j<=N; j++) RA[j][i]=max(RA[j][i-1], RA[min(N+1, j+(1<<(i-1)))][i-1]);
for(i=1; i<=M; i++) LB[i][0]=RB[i][0]=B[i];
for(i=1; i<=16; i++) for(j=1; j<=M; j++) LB[j][i]=max(LB[j][i-1], LB[max(0, j-(1<<(i-1)))][i-1]);
for(i=1; i<=16; i++) for(j=1; j<=M; j++) RB[j][i]=max(RB[j][i-1], RB[min(M+1, j+(1<<(i-1)))][i-1]);
while(Q--)
{
int y, x;
scanf("%d%d", &y, &x);
printf("%d\n", max(solve(y, x, 0), solve(y, x, 1)));
}
}
Compilation message (stderr)
abduction2.cpp: In function 'int solve(int, int, int)':
abduction2.cpp:21:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
abduction2.cpp: In function 'int main()':
abduction2.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &N, &M, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:58:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
abduction2.cpp:59:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=M; i++) scanf("%d", &B[i]);
~~~~~^~~~~~~~~~~~~
abduction2.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &y, &x);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |