#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>H)continue;
liftmaxW[i][bit]=max(liftmaxW[i][bit-1],liftmaxW[i+(1<<(bit-1))][bit-1]);
}
}
// vector visited(5,vector(H+1,vector(W+1,-1)));
for(int i=1;i<=Q;i++) {
int s,t;
cin>>s>>t;
int bestans = 0;
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) {
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)+(1<<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)+(1<<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)+(1<<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)+(1<<bit),ans);
break;
}
}
}
}
return 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';
}
}
Compilation message
abduction2.cpp: In lambda function:
abduction2.cpp:44:17: warning: variable 'pedge' set but not used [-Wunused-but-set-variable]
44 | int pedge = 0;
| ^~~~~
# |
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 |
1 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 |
0 ms |
344 KB |
Output is correct |
10 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 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 |
0 ms |
344 KB |
Output is correct |
10 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 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 |
0 ms |
344 KB |
Output is correct |
10 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
1116 KB |
Output is correct |
2 |
Correct |
31 ms |
1116 KB |
Output is correct |
3 |
Correct |
42 ms |
1112 KB |
Output is correct |
4 |
Correct |
53 ms |
1116 KB |
Output is correct |
5 |
Correct |
50 ms |
1136 KB |
Output is correct |
6 |
Correct |
38 ms |
1372 KB |
Output is correct |
7 |
Correct |
35 ms |
1368 KB |
Output is correct |
8 |
Execution timed out |
5047 ms |
1272 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 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 |
0 ms |
344 KB |
Output is correct |
10 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |