#include <bits/stdc++.h>
using namespace std;
int H, W, Q;
int A[50005], B[50005];
int pfa[50005], sfa[50005], pfb[50005], sfb[50005];
map<tuple<int, int, int>, long long> D;
const int SIZE = 1 << 16;
struct ST {
int A[2 * SIZE];
vector<int> a, b;
void update(int x, int v) {
for (x += SIZE; x; x >>= 1) A[x] = max(A[x], v);
}
int query(int o, int s, int e, int v) {
a.clear(), b.clear();
for (s += SIZE, e += SIZE; s <= e; s >>=1, e >>= 1) {
if ( s & 1) a.push_back(s), ++s;
if (~e & 1) b.push_back(e), --e;
}
a.insert(a.end(), b.rbegin(), b.rend());
if (!o) reverse(a.begin(), a.end());
for (auto i : a) if (A[i] > v) {
while (i < SIZE) {
i <<= 1;
if (o == 1) { if (A[i] < v) i |= 1; }
else {if (A[i | 1] > v) i |= 1;}
}
return i - SIZE;
}
return 0;
}
} a, b;
long long f(int x, int y, int d) {
if (!x || !y || x > H || y > W) return -1;
long long &ret = D[{x, y, d}];
if (ret) return ret;
if (d == 0) {
int p, q;
if (sfa[x + 1] < B[y]) p = 0;
else p = a.query(1, x + 1, H, B[y]);
if (pfa[x - 1] < B[y]) q = H + 1;
else q = a.query(0, 1, x - 1, B[y]);
return ret = max(abs(x - p) + f(p, y, 1), abs(x - q) + f(q, y, 1));
}
else {
int p, q;
if (sfb[y + 1] < A[x]) p = 0;
else p = b.query(1, y + 1, W, A[x]);
if (pfb[y - 1] < A[x]) q = W + 1;
else q = b.query(0, 1, y - 1, A[x]);
return ret = max(abs(y - p) + f(x, p, 0), abs(y - q) + f(x, q, 0));
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> H >> W >> Q;
for (int i = 1; i <= H; ++i) cin >> A[i], a.update(i, A[i]);
for (int i = 1; i <= W; ++i) cin >> B[i], b.update(i, B[i]);
for (int i = 1; i <= H; ++i) pfa[i] = max(pfa[i - 1], A[i]);
for (int i = 1; i <= W; ++i) pfb[i] = max(pfb[i - 1], B[i]);
for (int i = H; i; --i) sfa[i] = max(sfa[i + 1], A[i]);
for (int i = W; i; --i) sfb[i] = max(sfb[i + 1], B[i]);
for (int i = 0; i < Q; ++i) {
int x, y; cin >> x >> y;
printf("%lld\n", max(f(x, y, 0), f(x, y, 1)));
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |