#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include <bits/stdc++.h>
using namespace std;
//dir = 0 left
//dir = 1 right
//dir = 2 up
//dir = 3 down
map<int,long long>dp[50001][4];
int n,m,q;int logg[50001];
int arr1[50001],arr2[50001];
int spar1[50001][16];
int spar2[50001][16];
int query1(int l,int r){
int sz = r-l+1;
return max(spar1[l][logg[sz]],spar1[r-(1<<logg[sz])+1][logg[sz]]);
}
int query2(int l,int r){
int sz = r-l+1;
return max(spar2[l][logg[sz]],spar2[r-(1<<logg[sz])+1][logg[sz]]);
}
long long solve(int x,int y,int dir){
if(x<0||x>=n||y<0||y>=m)return -1;
//cout<<x<<" "<<y<<" "<<dir<<endl;
if(dp[x][dir].count(y)!=0)return dp[x][dir][y];
//cout<<x<<" "<<y<<" "<<dir<<endl;
if(dir==0){
int l = 0, r = y , ans = -1;
while(l<=r){
int mid = (l+r)/2;
if(query2(mid,y)>arr1[x]){
ans = mid;
l = mid+1;
}else r = mid-1;
}
//cout<<y<<" "<<ans<<" "<<dir<<endl;
if(ans!=-1){
long long c1 = 0;
c1 = max(c1,solve(x-1,ans,2)+1);
c1 = max(c1,solve(x+1,ans,3)+1);
return dp[x][dir][y] = c1+(y-ans);
}else{
return dp[x][dir][y] = y;
}
}if(dir==1){
int l = y, r = m-1 , ans = -1;
while(l<=r){
int mid = (l+r)/2;
if(query2(y,mid)>arr1[x]){
ans = mid;
r = mid-1;
}else l = mid+1;
}
//cout<<y<<" "<<ans<<" "<<dir<<endl;
if(ans!=-1){
long long c1 = 0;
c1 = max(c1,solve(x-1,ans,2)+1);
c1 = max(c1,solve(x+1,ans,3)+1);
return dp[x][dir][y] = c1+(ans-y);
}else{
return dp[x][dir][y] = (m-1)-y;
}
}if(dir==2){
int l = 0, r = x , ans = -1;
while(l<=r){
int mid = (l+r)/2;
if(query1(mid,x)>arr2[y]){
ans = mid;
l = mid+1;
}else r = mid-1;
}
//cout<<x<<" "<<ans<<" "<<dir<<endl;
if(ans!=-1){
long long c1 = 0;
c1 = max(c1,solve(ans,y-1,0)+1);
c1 = max(c1,solve(ans,y+1,1)+1);
return dp[x][dir][y] = c1+(x-ans);
}else{
return dp[x][dir][y] = x;
}
}if(dir==3){
int l = x, r = n-1 , ans = -1;
while(l<=r){
int mid = (l+r)/2;
if(query1(x,mid)>arr2[y]){
ans = mid;
r = mid-1;
}else l = mid+1;
}
//cout<<x<<" "<<ans<<" "<<dir<<endl;
if(ans!=-1){
long long c1 = 0;
c1 = max(c1,solve(ans,y-1,0)+1);
c1 = max(c1,solve(ans,y+1,1)+1);
return dp[x][dir][y] = c1+(ans-x);
}else{
return dp[x][dir][y] = (n-1)-x;
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m>>q;
for(int i = 0;i<n;i++)cin>>arr1[i];
for(int i = 0;i<m;i++)cin>>arr2[i];
for(int i = 0;i<n;i++)spar1[i][0] = arr1[i];
for(int i = 0;i<m;i++)spar2[i][0] = arr2[i];
for(int j = 1;j<16;j++){
for(int i = 0;i<n;i++){
if(i+(1<<j)>n)break;
spar1[i][j] = max(spar1[i][j-1],spar1[i+(1<<(j-1))][j-1]);
}
}
for(int j = 1;j<16;j++){
for(int i = 0;i<m;i++){
if(i+(1<<j)>m)break;
spar2[i][j] = max(spar2[i][j-1],spar2[i+(1<<(j-1))][j-1]);
}
}
logg[1] = 0;
for(int i = 2;i<=50000;i++)logg[i] = logg[i/2]+1;
while(q--){
int a,b;cin>>a>>b;
a--;b--;
long long ans = 0;
ans = max(ans,solve(a,b-1,0)+1);
ans = max(ans,solve(a,b+1,1)+1);
ans = max(ans,solve(a-1,b,2)+1);
ans = max(ans,solve(a+1,b,3)+1);
cout<<ans<<"\n";
}
}
Compilation message
abduction2.cpp: In function 'long long int solve(int, int, int)':
abduction2.cpp:100:1: warning: control reaches end of non-void function [-Wreturn-type]
100 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9852 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9932 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9868 KB |
Output is correct |
8 |
Correct |
4 ms |
9812 KB |
Output is correct |
9 |
Correct |
4 ms |
9856 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9852 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9932 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9868 KB |
Output is correct |
8 |
Correct |
4 ms |
9812 KB |
Output is correct |
9 |
Correct |
4 ms |
9856 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
10196 KB |
Output is correct |
13 |
Correct |
5 ms |
10196 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
10196 KB |
Output is correct |
16 |
Correct |
6 ms |
10104 KB |
Output is correct |
17 |
Correct |
5 ms |
10128 KB |
Output is correct |
18 |
Correct |
5 ms |
10196 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
20 |
Correct |
6 ms |
10708 KB |
Output is correct |
21 |
Correct |
6 ms |
10560 KB |
Output is correct |
22 |
Correct |
6 ms |
10964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9852 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9932 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9868 KB |
Output is correct |
8 |
Correct |
4 ms |
9812 KB |
Output is correct |
9 |
Correct |
4 ms |
9856 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
10196 KB |
Output is correct |
13 |
Correct |
5 ms |
10196 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
10196 KB |
Output is correct |
16 |
Correct |
6 ms |
10104 KB |
Output is correct |
17 |
Correct |
5 ms |
10128 KB |
Output is correct |
18 |
Correct |
5 ms |
10196 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
20 |
Correct |
6 ms |
10708 KB |
Output is correct |
21 |
Correct |
6 ms |
10560 KB |
Output is correct |
22 |
Correct |
6 ms |
10964 KB |
Output is correct |
23 |
Correct |
18 ms |
16468 KB |
Output is correct |
24 |
Correct |
16 ms |
16540 KB |
Output is correct |
25 |
Correct |
17 ms |
16472 KB |
Output is correct |
26 |
Correct |
18 ms |
16596 KB |
Output is correct |
27 |
Correct |
17 ms |
16500 KB |
Output is correct |
28 |
Correct |
27 ms |
23708 KB |
Output is correct |
29 |
Correct |
18 ms |
17492 KB |
Output is correct |
30 |
Correct |
46 ms |
27592 KB |
Output is correct |
31 |
Correct |
58 ms |
30696 KB |
Output is correct |
32 |
Correct |
18 ms |
17044 KB |
Output is correct |
33 |
Correct |
24 ms |
19856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
10484 KB |
Output is correct |
2 |
Correct |
7 ms |
10452 KB |
Output is correct |
3 |
Correct |
6 ms |
10452 KB |
Output is correct |
4 |
Correct |
8 ms |
10452 KB |
Output is correct |
5 |
Correct |
6 ms |
10452 KB |
Output is correct |
6 |
Correct |
6 ms |
10580 KB |
Output is correct |
7 |
Correct |
6 ms |
10512 KB |
Output is correct |
8 |
Correct |
9 ms |
10964 KB |
Output is correct |
9 |
Correct |
9 ms |
10984 KB |
Output is correct |
10 |
Correct |
9 ms |
10964 KB |
Output is correct |
11 |
Correct |
7 ms |
11220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9852 KB |
Output is correct |
2 |
Correct |
4 ms |
9812 KB |
Output is correct |
3 |
Correct |
4 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9932 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9868 KB |
Output is correct |
8 |
Correct |
4 ms |
9812 KB |
Output is correct |
9 |
Correct |
4 ms |
9856 KB |
Output is correct |
10 |
Correct |
4 ms |
9812 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
10196 KB |
Output is correct |
13 |
Correct |
5 ms |
10196 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
10196 KB |
Output is correct |
16 |
Correct |
6 ms |
10104 KB |
Output is correct |
17 |
Correct |
5 ms |
10128 KB |
Output is correct |
18 |
Correct |
5 ms |
10196 KB |
Output is correct |
19 |
Correct |
6 ms |
10452 KB |
Output is correct |
20 |
Correct |
6 ms |
10708 KB |
Output is correct |
21 |
Correct |
6 ms |
10560 KB |
Output is correct |
22 |
Correct |
6 ms |
10964 KB |
Output is correct |
23 |
Correct |
18 ms |
16468 KB |
Output is correct |
24 |
Correct |
16 ms |
16540 KB |
Output is correct |
25 |
Correct |
17 ms |
16472 KB |
Output is correct |
26 |
Correct |
18 ms |
16596 KB |
Output is correct |
27 |
Correct |
17 ms |
16500 KB |
Output is correct |
28 |
Correct |
27 ms |
23708 KB |
Output is correct |
29 |
Correct |
18 ms |
17492 KB |
Output is correct |
30 |
Correct |
46 ms |
27592 KB |
Output is correct |
31 |
Correct |
58 ms |
30696 KB |
Output is correct |
32 |
Correct |
18 ms |
17044 KB |
Output is correct |
33 |
Correct |
24 ms |
19856 KB |
Output is correct |
34 |
Correct |
6 ms |
10484 KB |
Output is correct |
35 |
Correct |
7 ms |
10452 KB |
Output is correct |
36 |
Correct |
6 ms |
10452 KB |
Output is correct |
37 |
Correct |
8 ms |
10452 KB |
Output is correct |
38 |
Correct |
6 ms |
10452 KB |
Output is correct |
39 |
Correct |
6 ms |
10580 KB |
Output is correct |
40 |
Correct |
6 ms |
10512 KB |
Output is correct |
41 |
Correct |
9 ms |
10964 KB |
Output is correct |
42 |
Correct |
9 ms |
10984 KB |
Output is correct |
43 |
Correct |
9 ms |
10964 KB |
Output is correct |
44 |
Correct |
7 ms |
11220 KB |
Output is correct |
45 |
Correct |
22 ms |
17024 KB |
Output is correct |
46 |
Correct |
19 ms |
17060 KB |
Output is correct |
47 |
Correct |
19 ms |
17148 KB |
Output is correct |
48 |
Correct |
19 ms |
17108 KB |
Output is correct |
49 |
Correct |
19 ms |
17104 KB |
Output is correct |
50 |
Correct |
31 ms |
23828 KB |
Output is correct |
51 |
Correct |
31 ms |
24592 KB |
Output is correct |
52 |
Correct |
76 ms |
35936 KB |
Output is correct |
53 |
Correct |
72 ms |
35136 KB |
Output is correct |
54 |
Correct |
68 ms |
34044 KB |
Output is correct |
55 |
Correct |
88 ms |
43572 KB |
Output is correct |
56 |
Correct |
1198 ms |
267528 KB |
Output is correct |
57 |
Correct |
239 ms |
81992 KB |
Output is correct |
58 |
Correct |
293 ms |
78792 KB |
Output is correct |
59 |
Correct |
231 ms |
78332 KB |
Output is correct |