#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);
vector liftmaxH(H+1,vector(16,0ll));
for(int i=1;i<=H;i++) {
cin>>A[i];
liftmaxH[i][0]=A[i];
}
for(int bit=1;bit<16;bit++) {
for(int i=1;i<=H;i++) {
if(i+(1<<bit)-1>H)continue;
liftmaxH[i][bit]=max(liftmaxH[i][bit-1],liftmaxH[i+(1<<(bit-1))][bit-1]);
}
}
vector<int> B(W+1);
vector liftmaxW(W+1,vector(16,0ll));
for(int i=1;i<=W;i++){
cin>>B[i];
liftmaxW[i][0]=B[i];
}
for(int bit=1;bit<16;bit++) {
for(int i=1;i<=W;i++) {
if(i+(1<<bit)-1>W)continue;
liftmaxW[i][bit]=max(liftmaxW[i][bit-1],liftmaxW[i+(1<<(bit-1))][bit-1]);
}
}
vector visited(5,vector(H+1,map<int,long long>()));
for(int i=1;i<=Q;i++) {
int s,t;
cin>>s>>t;
long long bestans = 0;
function<long long(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 -1ll;
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].count(y))return visited[pedge][x][y];
long long ans=0;
if(A[x]>B[y]) {
if(x!=parx or y-1!=pary) {
for(int bit=15;bit>=0;bit--) {
if(y-(1<<bit)<=0)continue;
if(liftmaxW[y-(1<<bit)+1][bit]<A[x]) {
ans = max(dfs(x,y-(1<<bit),x,y-(1<<bit)+1)+(1ll<<bit),ans);
break;
}
}
}
if(x!=parx or y+1!=pary) {
for(int bit=15;bit>=0;bit--) {
if(y+(1<<bit)>W)continue;
if(liftmaxW[y][bit]<A[x]) {
ans = max(dfs(x,y+(1<<bit),x,y+(1<<bit)-1)+(1ll<<bit),ans);
break;
}
}
}
} else {
if(x+1!=parx or y!=pary) {
for(int bit=15;bit>=0;bit--) {
if(x+(1<<bit)>H)continue;
if(liftmaxH[x][bit]<B[y]) {
ans = max(dfs(x+(1<<bit),y,x+(1<<bit)-1,y)+(1ll<<bit),ans);
break;
}
}
}
if(x-1!=parx or y!=pary) {
for(int bit=15;bit>=0;bit--) {
if(x-(1<<bit)<=0)continue;
if(liftmaxH[x-(1<<bit)+1][bit]<B[y]) {
ans = max(dfs(x-(1<<bit),y,x-(1<<bit)+1,y)+(1ll<<bit),ans);
break;
}
}
}
}
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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1624 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
2648 KB |
Output is correct |
20 |
Correct |
4 ms |
2908 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
6 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1624 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
2648 KB |
Output is correct |
20 |
Correct |
4 ms |
2908 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
6 ms |
3924 KB |
Output is correct |
23 |
Correct |
27 ms |
31324 KB |
Output is correct |
24 |
Correct |
26 ms |
31312 KB |
Output is correct |
25 |
Correct |
28 ms |
31312 KB |
Output is correct |
26 |
Correct |
26 ms |
31320 KB |
Output is correct |
27 |
Correct |
25 ms |
31324 KB |
Output is correct |
28 |
Correct |
87 ms |
53804 KB |
Output is correct |
29 |
Correct |
33 ms |
32564 KB |
Output is correct |
30 |
Correct |
178 ms |
70444 KB |
Output is correct |
31 |
Correct |
209 ms |
81748 KB |
Output is correct |
32 |
Correct |
34 ms |
31276 KB |
Output is correct |
33 |
Correct |
59 ms |
42284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2140 KB |
Output is correct |
2 |
Correct |
4 ms |
2140 KB |
Output is correct |
3 |
Correct |
3 ms |
2140 KB |
Output is correct |
4 |
Correct |
4 ms |
2140 KB |
Output is correct |
5 |
Correct |
4 ms |
2140 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
2652 KB |
Output is correct |
8 |
Correct |
8 ms |
3928 KB |
Output is correct |
9 |
Correct |
7 ms |
3932 KB |
Output is correct |
10 |
Correct |
9 ms |
3676 KB |
Output is correct |
11 |
Correct |
8 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1624 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
2648 KB |
Output is correct |
20 |
Correct |
4 ms |
2908 KB |
Output is correct |
21 |
Correct |
4 ms |
2652 KB |
Output is correct |
22 |
Correct |
6 ms |
3924 KB |
Output is correct |
23 |
Correct |
27 ms |
31324 KB |
Output is correct |
24 |
Correct |
26 ms |
31312 KB |
Output is correct |
25 |
Correct |
28 ms |
31312 KB |
Output is correct |
26 |
Correct |
26 ms |
31320 KB |
Output is correct |
27 |
Correct |
25 ms |
31324 KB |
Output is correct |
28 |
Correct |
87 ms |
53804 KB |
Output is correct |
29 |
Correct |
33 ms |
32564 KB |
Output is correct |
30 |
Correct |
178 ms |
70444 KB |
Output is correct |
31 |
Correct |
209 ms |
81748 KB |
Output is correct |
32 |
Correct |
34 ms |
31276 KB |
Output is correct |
33 |
Correct |
59 ms |
42284 KB |
Output is correct |
34 |
Correct |
3 ms |
2140 KB |
Output is correct |
35 |
Correct |
4 ms |
2140 KB |
Output is correct |
36 |
Correct |
3 ms |
2140 KB |
Output is correct |
37 |
Correct |
4 ms |
2140 KB |
Output is correct |
38 |
Correct |
4 ms |
2140 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
4 ms |
2652 KB |
Output is correct |
41 |
Correct |
8 ms |
3928 KB |
Output is correct |
42 |
Correct |
7 ms |
3932 KB |
Output is correct |
43 |
Correct |
9 ms |
3676 KB |
Output is correct |
44 |
Correct |
8 ms |
4444 KB |
Output is correct |
45 |
Correct |
34 ms |
31324 KB |
Output is correct |
46 |
Correct |
43 ms |
31324 KB |
Output is correct |
47 |
Correct |
34 ms |
31312 KB |
Output is correct |
48 |
Correct |
35 ms |
31312 KB |
Output is correct |
49 |
Correct |
31 ms |
31320 KB |
Output is correct |
50 |
Correct |
108 ms |
57900 KB |
Output is correct |
51 |
Correct |
103 ms |
60000 KB |
Output is correct |
52 |
Correct |
312 ms |
102156 KB |
Output is correct |
53 |
Correct |
299 ms |
98948 KB |
Output is correct |
54 |
Correct |
279 ms |
95788 KB |
Output is correct |
55 |
Correct |
379 ms |
128296 KB |
Output is correct |
56 |
Runtime error |
1576 ms |
524288 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |