#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=5e4+5;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<40);
const ll lgn=18;
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll ad[lgn+1][N],au[lgn+1][N],bl[lgn+1][N],br[lgn+1][N];
ll a[N],b[N],n,m,q;
map<ll,map<ll,map<ll,ll> > > dp;
ll f(ll y,ll x,ll dr){
if(dp[y][x].find(dr)!=dp[y][x].end()){return dp[y][x][dr];}
if(dr==0){
ll Y=y;
for(int i=lgn;i>=0;i--){
if(Y>(1LL<<i)&&au[i][Y]<b[x])Y-=(1LL<<i);
}
Y--;
if(Y==0)return y-Y-1;
if(a[Y]<b[x])return y-Y;
return dp[y][x][dr]=y-Y+max(f(Y,x,1),f(Y,x,3));
}else if(dr==1){
ll X=x;
for(int i=lgn;i>=0;i--){
if(X>(1LL<<i)&&bl[i][X]<a[y])X-=(1LL<<i);
}
X--;
if(X==0)return x-X-1;
if(b[X]<a[y])return x-X;
return dp[y][x][dr]=x-X+max(f(y,X,0),f(y,X,2));
}else if(dr==2){
ll Y=y;
for(int i=lgn;i>=0;i--){
if(Y+(1LL<<i)<=n&&ad[i][Y]<b[x])Y+=(1LL<<i);
}
Y++;
if(Y==n+1)return Y-y-1;
if(a[Y]<b[x])return Y-y;
return dp[y][x][dr]=Y-y+max(f(Y,x,1),f(Y,x,3));
}else if(dr==3){
ll X=x;
for(int i=lgn;i>=0;i--){
if(X+(1LL<<i)<=m&&br[i][X]<a[y])X+=(1LL<<i);
}
X++;
if(X==m+1)return X-x-1;
if(b[X]<a[y])return X-x;
return dp[y][x][dr]=X-x+max(f(y,X,0),f(y,X,2));
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m>>q;
REP1(i,n)cin>>a[i];
REP1(i,m)cin>>b[i];
REP1(i,n)au[0][i]=a[i-1],ad[0][i]=a[i+1];
REP1(i,m)br[0][i]=b[i+1],bl[0][i]=b[i-1];
for(int i=0;i<lgn;i++){
REP1(j,n){
au[i+1][j]=max(au[i][j],au[i][max(2LL,j-(1LL<<i))]);
ad[i+1][j]=max(ad[i][j],ad[i][min(n-1,j+(1LL<<i))]);
}
REP1(j,m){
br[i+1][j]=max(br[i][j],br[i][min(m-1,j+(1LL<<i))]);
bl[i+1][j]=max(bl[i][j],bl[i][max(2LL,j-(1LL<<i))]);
}
}
ll tmpa,tmpb;
while(q--){
cin>>tmpa>>tmpb;
cout<<max(max(f(tmpa,tmpb,0),f(tmpa,tmpb,1)),max(f(tmpa,tmpb,2),f(tmpa,tmpb,3)))<<"\n";
}
return 0;
}
Compilation message
abduction2.cpp: In function 'll f(ll, ll, ll)':
abduction2.cpp:68:1: warning: control reaches end of non-void function [-Wreturn-type]
68 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
3 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1900 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
2028 KB |
Output is correct |
19 |
Correct |
5 ms |
2540 KB |
Output is correct |
20 |
Correct |
7 ms |
2924 KB |
Output is correct |
21 |
Correct |
6 ms |
2668 KB |
Output is correct |
22 |
Correct |
10 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
3 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1900 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
2028 KB |
Output is correct |
19 |
Correct |
5 ms |
2540 KB |
Output is correct |
20 |
Correct |
7 ms |
2924 KB |
Output is correct |
21 |
Correct |
6 ms |
2668 KB |
Output is correct |
22 |
Correct |
10 ms |
3436 KB |
Output is correct |
23 |
Correct |
32 ms |
31872 KB |
Output is correct |
24 |
Correct |
32 ms |
31852 KB |
Output is correct |
25 |
Correct |
33 ms |
31852 KB |
Output is correct |
26 |
Correct |
35 ms |
31852 KB |
Output is correct |
27 |
Correct |
31 ms |
31852 KB |
Output is correct |
28 |
Correct |
64 ms |
41580 KB |
Output is correct |
29 |
Correct |
34 ms |
33260 KB |
Output is correct |
30 |
Correct |
171 ms |
51820 KB |
Output is correct |
31 |
Correct |
214 ms |
57580 KB |
Output is correct |
32 |
Correct |
38 ms |
32748 KB |
Output is correct |
33 |
Correct |
68 ms |
37996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2540 KB |
Output is correct |
2 |
Correct |
7 ms |
2540 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
6 ms |
2540 KB |
Output is correct |
5 |
Correct |
7 ms |
2540 KB |
Output is correct |
6 |
Correct |
4 ms |
2540 KB |
Output is correct |
7 |
Correct |
4 ms |
2412 KB |
Output is correct |
8 |
Correct |
12 ms |
3564 KB |
Output is correct |
9 |
Correct |
11 ms |
3564 KB |
Output is correct |
10 |
Correct |
11 ms |
3436 KB |
Output is correct |
11 |
Correct |
13 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
748 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
3 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1900 KB |
Output is correct |
17 |
Correct |
2 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
2028 KB |
Output is correct |
19 |
Correct |
5 ms |
2540 KB |
Output is correct |
20 |
Correct |
7 ms |
2924 KB |
Output is correct |
21 |
Correct |
6 ms |
2668 KB |
Output is correct |
22 |
Correct |
10 ms |
3436 KB |
Output is correct |
23 |
Correct |
32 ms |
31872 KB |
Output is correct |
24 |
Correct |
32 ms |
31852 KB |
Output is correct |
25 |
Correct |
33 ms |
31852 KB |
Output is correct |
26 |
Correct |
35 ms |
31852 KB |
Output is correct |
27 |
Correct |
31 ms |
31852 KB |
Output is correct |
28 |
Correct |
64 ms |
41580 KB |
Output is correct |
29 |
Correct |
34 ms |
33260 KB |
Output is correct |
30 |
Correct |
171 ms |
51820 KB |
Output is correct |
31 |
Correct |
214 ms |
57580 KB |
Output is correct |
32 |
Correct |
38 ms |
32748 KB |
Output is correct |
33 |
Correct |
68 ms |
37996 KB |
Output is correct |
34 |
Correct |
6 ms |
2540 KB |
Output is correct |
35 |
Correct |
7 ms |
2540 KB |
Output is correct |
36 |
Correct |
6 ms |
2560 KB |
Output is correct |
37 |
Correct |
6 ms |
2540 KB |
Output is correct |
38 |
Correct |
7 ms |
2540 KB |
Output is correct |
39 |
Correct |
4 ms |
2540 KB |
Output is correct |
40 |
Correct |
4 ms |
2412 KB |
Output is correct |
41 |
Correct |
12 ms |
3564 KB |
Output is correct |
42 |
Correct |
11 ms |
3564 KB |
Output is correct |
43 |
Correct |
11 ms |
3436 KB |
Output is correct |
44 |
Correct |
13 ms |
3820 KB |
Output is correct |
45 |
Correct |
40 ms |
32876 KB |
Output is correct |
46 |
Correct |
40 ms |
32876 KB |
Output is correct |
47 |
Correct |
42 ms |
32876 KB |
Output is correct |
48 |
Correct |
43 ms |
32876 KB |
Output is correct |
49 |
Correct |
40 ms |
32876 KB |
Output is correct |
50 |
Correct |
75 ms |
42732 KB |
Output is correct |
51 |
Correct |
79 ms |
43884 KB |
Output is correct |
52 |
Correct |
279 ms |
68588 KB |
Output is correct |
53 |
Correct |
262 ms |
66796 KB |
Output is correct |
54 |
Correct |
262 ms |
64748 KB |
Output is correct |
55 |
Correct |
383 ms |
82284 KB |
Output is correct |
56 |
Correct |
3562 ms |
474072 KB |
Output is correct |
57 |
Correct |
849 ms |
148096 KB |
Output is correct |
58 |
Correct |
803 ms |
142316 KB |
Output is correct |
59 |
Correct |
798 ms |
141804 KB |
Output is correct |