#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define pb push_back
ll n, m, q;
vector<ll> arr1;
vector<ll> arr2;
vector<pair<ll, ll>> qry;
ll solve(ll x, ll y, ll dir) {
// cout << x << ' ' << y << endl;
if (dir == 1) {//right
ll tmp = 1, ans = 0;
for (ll i = x + 1; i < m; ++i) {
if (arr2[i] > arr1[y])
break;
if (i == m - 1)
return tmp;
tmp++;
}
if (y < n - 1)
ans = max(ans, solve(x + tmp, y, 3) + tmp);
if (y > 0)
ans = max(ans, solve(x + tmp, y, 4) + tmp);
return ans;
}
else if (dir == 2) {//left
ll tmp = 1, ans = 0;
for (ll i = x - 1; i >= 0; --i) {
if (arr2[i] > arr1[y])
break;
if (i == 0)
return tmp;
tmp++;
}
if (y < n - 1)
ans = max(ans, solve(x - tmp, y, 3) + tmp);
if (y > 0)
ans = max(ans, solve(x - tmp, y, 4) + tmp);
return ans;
}
else if (dir == 3) {//down
ll tmp = 1, ans = 0;
for (ll i = y + 1; i < n; ++i) {
if (arr1[i] > arr2[x])
break;
if (i == n - 1)
return tmp;
tmp++;
}
if (x < m - 1)
ans = max(ans, solve(x, y + tmp, 1) + tmp);
if (x > 0)
ans = max(ans, solve(x, y + tmp, 2) + tmp);
return ans;
}
else {//up
ll tmp = 1, ans = 0;
for (ll i = y - 1; i >= 0; --i) {
if (arr1[i] > arr2[x])
break;
if (i == 0)
return tmp;
tmp++;
}
if (x < m - 1)
ans = max(ans, solve(x, y - tmp, 1) + tmp);
if (x > 0)
ans = max(ans, solve(x, y - tmp, 2) + tmp);
return ans;
}
}
signed main() {
//the boost
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
//main code
cin >> n >> m >> q;
arr1.resize(n);
arr2.resize(m);
qry.resize(q);
for (ll i = 0; i < n; ++i) {
cin >> arr1[i];
}
for (ll i = 0; i < m; ++i) {
cin >> arr2[i];
}
for (ll i = 0; i < q; ++i) {
cin >> qry[i].fi >> qry[i].se;
}
for (ll i = 0; i < q; ++i) {
ll ans = 0;
ll x = qry[i].se - 1, y = qry[i].fi - 1;
if (x < m - 1)
ans = max(ans, solve(x, y, 1));//right
if (x > 0)
ans = max(ans, solve(x, y, 2));//left
if (y < n - 1)
ans = max(ans, solve(x, y, 3));//down
if (y > 0)
ans = max(ans, solve(x, y, 4));//up
cout << ans << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |