#pragma GCC optimize("Ofast","unroll-all-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int H,W,Q;
cin >> H >> W >> Q;
vector<int> A(H+1);
for(int i=1;i<=H;i++)cin>>A[i];
vector<int> B(W+1);
for(int i=1;i<=W;i++)cin>>B[i];
for(int i=1;i<=Q;i++) {
int s,t;cin>>s>>t;
int bestans = 0;
vector visited(5,vector(H+1,vector<int>(W+1,-1)));
function<int(int,int,int&,int&)> dfs = [&](int x,int y,int &parx,int &pary) {
if(x<1 or x>H or y<1 or y>W)return -1;
int pedge = 0;
if(x>parx)pedge=1;
else if(x<parx)pedge=2;
else if(y>pary)pedge=3;
else if(y<pary)pedge=4;
if(visited[pedge][x][y]!=-1)return visited[pedge][x][y];
int ans=0;
if(A[x]>B[y]) {
if(x!=parx or y-1!=pary)ans=max(dfs(x,y-1,x,y)+1,ans);
if(x!=parx or y+1!=pary)ans=max(dfs(x,y+1,x,y)+1,ans);
} else {
if(x+1!=parx or y!=pary)ans=max(dfs(x+1,y,x,y)+1,ans);
if(x-1!=parx or y!=pary)ans=max(dfs(x-1,y,x,y)+1,ans);
}
return visited[pedge][x][y]=ans;
};
bestans = max(bestans,dfs(s,t,s,t));
if(A[s]>B[t]) {
bestans=max(bestans,dfs(s-1,t,s,t)+1);
bestans=max(bestans,dfs(s+1,t,s,t)+1);
} else {
bestans=max(bestans,dfs(s,t-1,s,t)+1);
bestans=max(bestans,dfs(s,t+1,s,t)+1);
}
cout << bestans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
39 ms |
95056 KB |
Output is correct |
13 |
Correct |
39 ms |
95056 KB |
Output is correct |
14 |
Correct |
40 ms |
95056 KB |
Output is correct |
15 |
Correct |
52 ms |
95088 KB |
Output is correct |
16 |
Correct |
45 ms |
95068 KB |
Output is correct |
17 |
Correct |
42 ms |
95052 KB |
Output is correct |
18 |
Correct |
46 ms |
95060 KB |
Output is correct |
19 |
Correct |
93 ms |
95828 KB |
Output is correct |
20 |
Correct |
107 ms |
92944 KB |
Output is correct |
21 |
Correct |
97 ms |
94032 KB |
Output is correct |
22 |
Correct |
155 ms |
95700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
39 ms |
95056 KB |
Output is correct |
13 |
Correct |
39 ms |
95056 KB |
Output is correct |
14 |
Correct |
40 ms |
95056 KB |
Output is correct |
15 |
Correct |
52 ms |
95088 KB |
Output is correct |
16 |
Correct |
45 ms |
95068 KB |
Output is correct |
17 |
Correct |
42 ms |
95052 KB |
Output is correct |
18 |
Correct |
46 ms |
95060 KB |
Output is correct |
19 |
Correct |
93 ms |
95828 KB |
Output is correct |
20 |
Correct |
107 ms |
92944 KB |
Output is correct |
21 |
Correct |
97 ms |
94032 KB |
Output is correct |
22 |
Correct |
155 ms |
95700 KB |
Output is correct |
23 |
Runtime error |
216 ms |
524288 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3331 ms |
95412 KB |
Output is correct |
2 |
Correct |
3377 ms |
95352 KB |
Output is correct |
3 |
Correct |
3498 ms |
95432 KB |
Output is correct |
4 |
Correct |
3276 ms |
95376 KB |
Output is correct |
5 |
Correct |
3318 ms |
95484 KB |
Output is correct |
6 |
Execution timed out |
5020 ms |
95544 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
39 ms |
95056 KB |
Output is correct |
13 |
Correct |
39 ms |
95056 KB |
Output is correct |
14 |
Correct |
40 ms |
95056 KB |
Output is correct |
15 |
Correct |
52 ms |
95088 KB |
Output is correct |
16 |
Correct |
45 ms |
95068 KB |
Output is correct |
17 |
Correct |
42 ms |
95052 KB |
Output is correct |
18 |
Correct |
46 ms |
95060 KB |
Output is correct |
19 |
Correct |
93 ms |
95828 KB |
Output is correct |
20 |
Correct |
107 ms |
92944 KB |
Output is correct |
21 |
Correct |
97 ms |
94032 KB |
Output is correct |
22 |
Correct |
155 ms |
95700 KB |
Output is correct |
23 |
Runtime error |
216 ms |
524288 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |