#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
int dp[2001][2001][4];
int n,m,q;
int arr1[2001],arr2[10001];
int solve(int x,int y,int dir){
if(x<0||x>=n||y<0||y>=m)return -1;
if(dp[x][y][dir]!=-1)return dp[x][y][dir];
if(dir==0){
if(arr2[y]>arr1[x]){
int c1 = 0;
c1 = max(c1,solve(x-1,y,2)+1);
c1 = max(c1,solve(x+1,y,3)+1);
return dp[x][y][dir] = c1;
}else{
return dp[x][y][dir] = solve(x,y-1,dir)+1;
}
}if(dir==1){
if(arr2[y]>arr1[x]){
int c1 = 0;
c1 = max(c1,solve(x-1,y,2)+1);
c1 = max(c1,solve(x+1,y,3)+1);
return dp[x][y][dir] = c1;
}else{
return dp[x][y][dir] = solve(x,y+1,dir)+1;
}
}if(dir==2){
if(arr1[x]>arr2[y]){
int c1 = 0;
c1 = max(c1,solve(x,y-1,0)+1);
c1 = max(c1,solve(x,y+1,1)+1);
return dp[x][y][dir] = c1;
}else{
return dp[x][y][dir] = solve(x-1,y,dir)+1;
}
}if(dir==3){
if(arr1[x]>arr2[y]){
int c1 = 0;
c1 = max(c1,solve(x,y-1,0)+1);
c1 = max(c1,solve(x,y+1,1)+1);
return dp[x][y][dir] = c1;
}else{
return dp[x][y][dir] = solve(x+1,y,dir)+1;
}
}
}
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];
memset(dp,-1,sizeof dp);
while(q--){
int a,b;cin>>a>>b;
a--;b--;
int 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 'int solve(int, int, int)':
abduction2.cpp:52:1: warning: control reaches end of non-void function [-Wreturn-type]
52 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62916 KB |
Output is correct |
2 |
Correct |
23 ms |
62868 KB |
Output is correct |
3 |
Correct |
23 ms |
62912 KB |
Output is correct |
4 |
Correct |
25 ms |
62928 KB |
Output is correct |
5 |
Correct |
27 ms |
62900 KB |
Output is correct |
6 |
Correct |
26 ms |
62928 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
23 ms |
62984 KB |
Output is correct |
10 |
Correct |
24 ms |
62868 KB |
Output is correct |
11 |
Correct |
24 ms |
62924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62916 KB |
Output is correct |
2 |
Correct |
23 ms |
62868 KB |
Output is correct |
3 |
Correct |
23 ms |
62912 KB |
Output is correct |
4 |
Correct |
25 ms |
62928 KB |
Output is correct |
5 |
Correct |
27 ms |
62900 KB |
Output is correct |
6 |
Correct |
26 ms |
62928 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
23 ms |
62984 KB |
Output is correct |
10 |
Correct |
24 ms |
62868 KB |
Output is correct |
11 |
Correct |
24 ms |
62924 KB |
Output is correct |
12 |
Correct |
26 ms |
63156 KB |
Output is correct |
13 |
Correct |
25 ms |
63224 KB |
Output is correct |
14 |
Correct |
26 ms |
63224 KB |
Output is correct |
15 |
Correct |
26 ms |
63196 KB |
Output is correct |
16 |
Correct |
27 ms |
63192 KB |
Output is correct |
17 |
Correct |
29 ms |
63240 KB |
Output is correct |
18 |
Correct |
30 ms |
63232 KB |
Output is correct |
19 |
Correct |
84 ms |
63896 KB |
Output is correct |
20 |
Correct |
89 ms |
63760 KB |
Output is correct |
21 |
Correct |
80 ms |
63700 KB |
Output is correct |
22 |
Correct |
131 ms |
63792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62916 KB |
Output is correct |
2 |
Correct |
23 ms |
62868 KB |
Output is correct |
3 |
Correct |
23 ms |
62912 KB |
Output is correct |
4 |
Correct |
25 ms |
62928 KB |
Output is correct |
5 |
Correct |
27 ms |
62900 KB |
Output is correct |
6 |
Correct |
26 ms |
62928 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
23 ms |
62984 KB |
Output is correct |
10 |
Correct |
24 ms |
62868 KB |
Output is correct |
11 |
Correct |
24 ms |
62924 KB |
Output is correct |
12 |
Correct |
26 ms |
63156 KB |
Output is correct |
13 |
Correct |
25 ms |
63224 KB |
Output is correct |
14 |
Correct |
26 ms |
63224 KB |
Output is correct |
15 |
Correct |
26 ms |
63196 KB |
Output is correct |
16 |
Correct |
27 ms |
63192 KB |
Output is correct |
17 |
Correct |
29 ms |
63240 KB |
Output is correct |
18 |
Correct |
30 ms |
63232 KB |
Output is correct |
19 |
Correct |
84 ms |
63896 KB |
Output is correct |
20 |
Correct |
89 ms |
63760 KB |
Output is correct |
21 |
Correct |
80 ms |
63700 KB |
Output is correct |
22 |
Correct |
131 ms |
63792 KB |
Output is correct |
23 |
Runtime error |
3048 ms |
128436 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
63180 KB |
Output is correct |
2 |
Correct |
27 ms |
63164 KB |
Output is correct |
3 |
Correct |
27 ms |
63216 KB |
Output is correct |
4 |
Correct |
28 ms |
63300 KB |
Output is correct |
5 |
Correct |
28 ms |
63272 KB |
Output is correct |
6 |
Correct |
52 ms |
63304 KB |
Output is correct |
7 |
Correct |
50 ms |
63372 KB |
Output is correct |
8 |
Correct |
101 ms |
63744 KB |
Output is correct |
9 |
Correct |
99 ms |
63864 KB |
Output is correct |
10 |
Correct |
103 ms |
63740 KB |
Output is correct |
11 |
Correct |
140 ms |
63700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62916 KB |
Output is correct |
2 |
Correct |
23 ms |
62868 KB |
Output is correct |
3 |
Correct |
23 ms |
62912 KB |
Output is correct |
4 |
Correct |
25 ms |
62928 KB |
Output is correct |
5 |
Correct |
27 ms |
62900 KB |
Output is correct |
6 |
Correct |
26 ms |
62928 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
23 ms |
62984 KB |
Output is correct |
10 |
Correct |
24 ms |
62868 KB |
Output is correct |
11 |
Correct |
24 ms |
62924 KB |
Output is correct |
12 |
Correct |
26 ms |
63156 KB |
Output is correct |
13 |
Correct |
25 ms |
63224 KB |
Output is correct |
14 |
Correct |
26 ms |
63224 KB |
Output is correct |
15 |
Correct |
26 ms |
63196 KB |
Output is correct |
16 |
Correct |
27 ms |
63192 KB |
Output is correct |
17 |
Correct |
29 ms |
63240 KB |
Output is correct |
18 |
Correct |
30 ms |
63232 KB |
Output is correct |
19 |
Correct |
84 ms |
63896 KB |
Output is correct |
20 |
Correct |
89 ms |
63760 KB |
Output is correct |
21 |
Correct |
80 ms |
63700 KB |
Output is correct |
22 |
Correct |
131 ms |
63792 KB |
Output is correct |
23 |
Runtime error |
3048 ms |
128436 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |