#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
const int N=50050;
const int L=16;
int a[N],b[N],lgs[N];
struct SparseTable{
int rmq[N][L],n;
SparseTable(){}
void Build(int*a,int n){
this->n=n;
for(int i=1;i<=n;i++)rmq[i][0]=a[i];
for(int j=1;j<L;j++){
for(int i=1;i<=n-(1<<j)+1;i++){
rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<j-1)][j-1]);
}
}
}
int Get(int l,int r){
int k=lgs[r-l+1];
return max(rmq[l][k],rmq[r-(1<<k)+1][k]);
}
pii GetRng(int x,int y){
int top=x-1,bot=1,mid,L=0,R=n+1;
while(top>=bot){
mid=top+bot>>1;
if(Get(mid,x-1)>y)L=mid,bot=mid+1;
else top=mid-1;
}
top=n,bot=x+1;
while(top>=bot){
mid=top+bot>>1;
if(Get(x+1,mid)>y)R=mid,top=mid-1;
else bot=mid+1;
}
return {L,R};
}
}A,B;
int n,m,q;
map<pii,ll> dp[2];
ll DP(int x,int y,int t){
if(x<1||x>n||y<1||y>m)return -1;
if(dp[t].count({x,y}))return dp[t][{x,y}];
ll ans;
if(t==0){
pii rng=A.GetRng(x,b[y]);
//printf("1: %i %i -> %i %i\n",x,y,rng.first,rng.second);
ans=max(DP(rng.first,y,1)+x-rng.first,DP(rng.second,y,1)+rng.second-x);
}else{
pii rng=B.GetRng(y,a[x]);
//printf("2: %i %i -> %i %i\n",x,y,rng.first,rng.second);
ans=max(DP(x,rng.first,0)+y-rng.first,DP(x,rng.second,0)+rng.second-y);
}
return dp[t][{x,y}]=ans;
}
int main(){
for(int i=2;i<N;i++)lgs[i]=lgs[i>>1]+1;
scanf("%i %i %i",&n,&m,&q);
for(int i=1;i<=n;i++)scanf("%i",&a[i]);A.Build(a,n);
for(int i=1;i<=m;i++)scanf("%i",&b[i]);B.Build(b,m);
while(q--){
int x,y;
scanf("%i %i",&x,&y);
printf("%lld\n",max(DP(x,y,0),DP(x,y,1)));
}
return 0;
}
Compilation message
abduction2.cpp: In member function 'void SparseTable::Build(int*, int)':
abduction2.cpp:16:42: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
16 | rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<j-1)][j-1]);
| ~^~
abduction2.cpp: In member function 'std::pair<int, int> SparseTable::GetRng(int, int)':
abduction2.cpp:27:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | mid=top+bot>>1;
| ~~~^~~~
abduction2.cpp:33:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | mid=top+bot>>1;
| ~~~^~~~
abduction2.cpp: In function 'int main()':
abduction2.cpp:60:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
60 | for(int i=1;i<=n;i++)scanf("%i",&a[i]);A.Build(a,n);
| ^~~
abduction2.cpp:60:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
60 | for(int i=1;i<=n;i++)scanf("%i",&a[i]);A.Build(a,n);
| ^
abduction2.cpp:61:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
61 | for(int i=1;i<=m;i++)scanf("%i",&b[i]);B.Build(b,m);
| ^~~
abduction2.cpp:61:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
61 | for(int i=1;i<=m;i++)scanf("%i",&b[i]);B.Build(b,m);
| ^
abduction2.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
59 | scanf("%i %i %i",&n,&m,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:60:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | for(int i=1;i<=n;i++)scanf("%i",&a[i]);A.Build(a,n);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:61:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
61 | for(int i=1;i<=m;i++)scanf("%i",&b[i]);B.Build(b,m);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("%i %i",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
488 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
488 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
896 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
2 ms |
984 KB |
Output is correct |
17 |
Correct |
2 ms |
896 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
4 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1152 KB |
Output is correct |
22 |
Correct |
5 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
488 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
896 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
2 ms |
984 KB |
Output is correct |
17 |
Correct |
2 ms |
896 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
4 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1152 KB |
Output is correct |
22 |
Correct |
5 ms |
1408 KB |
Output is correct |
23 |
Correct |
27 ms |
8192 KB |
Output is correct |
24 |
Correct |
28 ms |
8212 KB |
Output is correct |
25 |
Correct |
27 ms |
8184 KB |
Output is correct |
26 |
Correct |
27 ms |
8184 KB |
Output is correct |
27 |
Correct |
27 ms |
8184 KB |
Output is correct |
28 |
Correct |
44 ms |
13816 KB |
Output is correct |
29 |
Correct |
28 ms |
9088 KB |
Output is correct |
30 |
Correct |
93 ms |
14712 KB |
Output is correct |
31 |
Correct |
115 ms |
16504 KB |
Output is correct |
32 |
Correct |
30 ms |
8448 KB |
Output is correct |
33 |
Correct |
44 ms |
10260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
4 ms |
1024 KB |
Output is correct |
4 |
Correct |
4 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
6 ms |
1340 KB |
Output is correct |
9 |
Correct |
6 ms |
1268 KB |
Output is correct |
10 |
Correct |
5 ms |
1280 KB |
Output is correct |
11 |
Correct |
7 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
488 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
896 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
2 ms |
984 KB |
Output is correct |
17 |
Correct |
2 ms |
896 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
4 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1152 KB |
Output is correct |
22 |
Correct |
5 ms |
1408 KB |
Output is correct |
23 |
Correct |
27 ms |
8192 KB |
Output is correct |
24 |
Correct |
28 ms |
8212 KB |
Output is correct |
25 |
Correct |
27 ms |
8184 KB |
Output is correct |
26 |
Correct |
27 ms |
8184 KB |
Output is correct |
27 |
Correct |
27 ms |
8184 KB |
Output is correct |
28 |
Correct |
44 ms |
13816 KB |
Output is correct |
29 |
Correct |
28 ms |
9088 KB |
Output is correct |
30 |
Correct |
93 ms |
14712 KB |
Output is correct |
31 |
Correct |
115 ms |
16504 KB |
Output is correct |
32 |
Correct |
30 ms |
8448 KB |
Output is correct |
33 |
Correct |
44 ms |
10260 KB |
Output is correct |
34 |
Correct |
4 ms |
1024 KB |
Output is correct |
35 |
Correct |
3 ms |
1024 KB |
Output is correct |
36 |
Correct |
4 ms |
1024 KB |
Output is correct |
37 |
Correct |
4 ms |
1024 KB |
Output is correct |
38 |
Correct |
3 ms |
1024 KB |
Output is correct |
39 |
Correct |
3 ms |
1152 KB |
Output is correct |
40 |
Correct |
3 ms |
1024 KB |
Output is correct |
41 |
Correct |
6 ms |
1340 KB |
Output is correct |
42 |
Correct |
6 ms |
1268 KB |
Output is correct |
43 |
Correct |
5 ms |
1280 KB |
Output is correct |
44 |
Correct |
7 ms |
1408 KB |
Output is correct |
45 |
Correct |
31 ms |
8440 KB |
Output is correct |
46 |
Correct |
32 ms |
8480 KB |
Output is correct |
47 |
Correct |
32 ms |
8448 KB |
Output is correct |
48 |
Correct |
32 ms |
8440 KB |
Output is correct |
49 |
Correct |
32 ms |
8440 KB |
Output is correct |
50 |
Correct |
53 ms |
13048 KB |
Output is correct |
51 |
Correct |
54 ms |
13688 KB |
Output is correct |
52 |
Correct |
158 ms |
18680 KB |
Output is correct |
53 |
Correct |
157 ms |
18168 KB |
Output is correct |
54 |
Correct |
149 ms |
17656 KB |
Output is correct |
55 |
Correct |
235 ms |
23032 KB |
Output is correct |
56 |
Correct |
2265 ms |
133624 KB |
Output is correct |
57 |
Correct |
511 ms |
40736 KB |
Output is correct |
58 |
Correct |
495 ms |
38904 KB |
Output is correct |
59 |
Correct |
487 ms |
38776 KB |
Output is correct |