#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) return -1e13;
long long &ret = D[{x, y, d}];
if (ret) return ret;
if (d == 0) {
if (max(pfa[x - 1], sfa[x + 1]) < B[y]) return ret = max(x - 1, H - x);
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 = 0;
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 {
if (max(pfb[y - 1], sfb[y + 1]) < A[x]) return ret = max(y - 1, W - y);
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 = 0;
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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
248 KB |
Output is correct |
6 |
Correct |
5 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
552 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
248 KB |
Output is correct |
6 |
Correct |
5 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
552 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
5 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
7 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
888 KB |
Output is correct |
22 |
Correct |
9 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
248 KB |
Output is correct |
6 |
Correct |
5 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
552 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
5 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
7 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
888 KB |
Output is correct |
22 |
Correct |
9 ms |
1016 KB |
Output is correct |
23 |
Correct |
26 ms |
2424 KB |
Output is correct |
24 |
Correct |
25 ms |
2424 KB |
Output is correct |
25 |
Correct |
24 ms |
2424 KB |
Output is correct |
26 |
Correct |
22 ms |
2296 KB |
Output is correct |
27 |
Correct |
22 ms |
2296 KB |
Output is correct |
28 |
Correct |
38 ms |
9464 KB |
Output is correct |
29 |
Correct |
24 ms |
3320 KB |
Output is correct |
30 |
Correct |
82 ms |
9592 KB |
Output is correct |
31 |
Correct |
119 ms |
11744 KB |
Output is correct |
32 |
Correct |
25 ms |
2680 KB |
Output is correct |
33 |
Correct |
37 ms |
4728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
760 KB |
Output is correct |
2 |
Correct |
7 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
636 KB |
Output is correct |
4 |
Correct |
8 ms |
632 KB |
Output is correct |
5 |
Correct |
8 ms |
632 KB |
Output is correct |
6 |
Correct |
6 ms |
888 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
11 ms |
1016 KB |
Output is correct |
9 |
Correct |
10 ms |
1016 KB |
Output is correct |
10 |
Correct |
9 ms |
1016 KB |
Output is correct |
11 |
Correct |
11 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
248 KB |
Output is correct |
6 |
Correct |
5 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
552 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
5 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
7 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
888 KB |
Output is correct |
22 |
Correct |
9 ms |
1016 KB |
Output is correct |
23 |
Correct |
26 ms |
2424 KB |
Output is correct |
24 |
Correct |
25 ms |
2424 KB |
Output is correct |
25 |
Correct |
24 ms |
2424 KB |
Output is correct |
26 |
Correct |
22 ms |
2296 KB |
Output is correct |
27 |
Correct |
22 ms |
2296 KB |
Output is correct |
28 |
Correct |
38 ms |
9464 KB |
Output is correct |
29 |
Correct |
24 ms |
3320 KB |
Output is correct |
30 |
Correct |
82 ms |
9592 KB |
Output is correct |
31 |
Correct |
119 ms |
11744 KB |
Output is correct |
32 |
Correct |
25 ms |
2680 KB |
Output is correct |
33 |
Correct |
37 ms |
4728 KB |
Output is correct |
34 |
Correct |
7 ms |
760 KB |
Output is correct |
35 |
Correct |
7 ms |
632 KB |
Output is correct |
36 |
Correct |
7 ms |
636 KB |
Output is correct |
37 |
Correct |
8 ms |
632 KB |
Output is correct |
38 |
Correct |
8 ms |
632 KB |
Output is correct |
39 |
Correct |
6 ms |
888 KB |
Output is correct |
40 |
Correct |
6 ms |
760 KB |
Output is correct |
41 |
Correct |
11 ms |
1016 KB |
Output is correct |
42 |
Correct |
10 ms |
1016 KB |
Output is correct |
43 |
Correct |
9 ms |
1016 KB |
Output is correct |
44 |
Correct |
11 ms |
1144 KB |
Output is correct |
45 |
Correct |
29 ms |
2680 KB |
Output is correct |
46 |
Correct |
27 ms |
2680 KB |
Output is correct |
47 |
Correct |
27 ms |
2680 KB |
Output is correct |
48 |
Incorrect |
27 ms |
2680 KB |
Output isn't correct |
49 |
Halted |
0 ms |
0 KB |
- |