#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int pw[17];
map<tuple<int, int, int>, ll> dp;
int n, m, h[50001][17], v[50001][17];
ll dfs(int x, int y, int dir) {
if (dp.count({x, y, dir})) return dp[{x, y, dir}];
int &ans = dp[{x, y, dir}];
if (dir & 1) { // Up or down (x changes)
int sign = dir - 2;
int nxt_x = x;
for (int i = 16; ~i; i--) {
int tmp_x = nxt_x + (1 << i) * sign - sign;
if (tmp_x < 1 || tmp_x > n) continue;
if ((sign == 1 ? h[nxt_x][i] : h[tmp_x][i]) > v[y][0]) continue;
nxt_x = tmp_x + sign;
}
if (nxt_x < 1 || nxt_x > n) return ans = abs(nxt_x - sign - x);
return ans = abs(nxt_x - x) + max(dfs(nxt_x, y, 0), dfs(nxt_x, y, 2));
} else { // Left or right (y changes)
int sign = dir - 1;
int nxt_y = y;
for (int i = 16; ~i; i--) {
int tmp_y = nxt_y + (1 << i) * sign - sign;
if (tmp_y < 1 || tmp_y > m) continue;
if ((sign == 1 ? v[nxt_y][i] : v[tmp_y][i]) > h[x][0]) continue;
nxt_y = tmp_y + sign;
}
if (nxt_y < 1 || nxt_y > m) return ans = abs(nxt_y - sign - y);
return ans = abs(nxt_y - y) + max(dfs(x, nxt_y, 1), dfs(x, nxt_y, 3));
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pw[0] = 1;
for (int i = 1; i < 17; i++) pw[i] = pw[i - 1] << 1;
int q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) cin >> h[i][0];
for (int i = 1; i <= m; i++) cin >> v[i][0];
for (int j = 1; j < 17; j++) {
for (int i = 1; i <= n - pw[j] + 1; i++) {
h[i][j] = max(h[i][j - 1], h[i + pw[j - 1]][j - 1]);
}
for (int i = 1; i <= m - pw[j] + 1; i++) {
v[i][j] = max(v[i][j - 1], v[i + pw[j - 1]][j - 1]);
}
}
while (q--) {
int x, y;
cin >> x >> y;
ll ans = 0;
if (x < n) ans = max(ans, dfs(x + 1, y, 3) + 1);
if (y < m) ans = max(ans, dfs(x, y + 1, 2) + 1);
if (x > 1) ans = max(ans, dfs(x - 1, y, 1) + 1);
if (y > 1) ans = max(ans, dfs(x, y - 1, 0) + 1);
cout << ans << '\n';
}
return 0;
}
Compilation message
abduction2.cpp: In function 'll dfs(int, int, int)':
abduction2.cpp:11:30: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
int &ans = dp[{x, y, dir}];
^