This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e5+7;
int a[N], b[N];
struct State {
int x, y, t;
int val() {
if (t&1)
return a[x];
else
return b[y];
};
bool operator <(State s) {
return val() < s.val();
}
};
int n, m, q;
map <int, int> dp[4][N];
int get(int x, int y, int t) {
if (x < 0 || x >= n || y < 0 || y >= m)
return -1;
if (dp[t][x].find(y) != dp[t][x].end())
return dp[t][x][y];
int &d = dp[t][x][y];
d = 0;
if (t&1) {
if (a[x] < b[y]) {
d = max(d, get(x,y,0));
d = max(d, get(x,y,2));
}
else if (t == 1) {
if (y + 1 < m)
d = get(x,y+1,1)+1;
}
else {
if (y)
d = get(x,y-1,3)+1;
}
}
else {
if (b[y] < a[x]) {
d = max(d, get(x,y,1));
d = max(d, get(x,y,3));
}
else if (t == 0) {
if (x)
d = get(x-1,y,0)+1;
}
else {
if (x + 1 < n)
d = get(x+1,y,2)+1;
}
}
return d;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> m >> q;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < m; ++i)
cin >> b[i];
while (q--) {
int x, y;
cin >> x >> y;
--x; --y;
int ans = 0;
ans = max(ans, get(x-1,y,0)+1);
ans = max(ans, get(x+1,y,2)+1);
ans = max(ans, get(x,y-1,3)+1);
ans = max(ans, get(x,y+1,1)+1);
cout << ans << endl;
}
}
# | 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... |