#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];
void update(int x, int v) {
for (x += SIZE; x; x >>= 1) A[x] = max(A[x], v);
}
int query(int s, int e) {
int ret = 0;
for (s += SIZE, e += SIZE; s <= e; s >>= 1, e >>= 1) {
if ( s & 1) ret = max(ret, A[s++]);
if (~e & 1) ret = max(ret, A[e--]);
}
return ret;
}
} a, b;
long long f(int x, int y, int d) {
if (!x || !y) return -2e9;
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;
{
int l = x + 1, r = H;
if (sfa[x + 1] < B[y]) p = 0;
else {
while (l < r) {
int m = l + r >> 1;
if (a.query(x + 1, m) > B[y]) r = m;
else l = m + 1;
}
p = l;
}
}
{
int l = 1, r = x - 1;
if (pfa[x - 1] < B[y]) q = 0;
else {
while (l < r) {
int m = l + r + 1 >> 1;
if (a.query(m, x - 1) > B[y]) l = m;
else r = m - 1;
}
q = r;
}
}
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;
{
int l = y + 1, r = W;
if (sfb[y + 1] < A[x]) p = 0;
else {
while (l < r) {
int m = l + r >> 1;
if (b.query(y + 1, m) > A[x]) r = m;
else l = m + 1;
}
p = l;
}
}
{
int l = 1, r = y - 1;
if (pfb[y - 1] < A[x]) q = 0;
else {
while (l < r) {
int m = l + r + 1 >> 1;
if (b.query(m, y - 1) > A[x]) l = m;
else r = m - 1;
}
q = r;
}
}
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;
}
Compilation message
abduction2.cpp: In function 'long long int f(int, int, int)':
abduction2.cpp:37:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
abduction2.cpp:49:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r + 1 >> 1;
~~~~~~^~~
abduction2.cpp:66:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
abduction2.cpp:78:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r + 1 >> 1;
~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
636 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
760 KB |
Output is correct |
22 |
Correct |
11 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
636 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
760 KB |
Output is correct |
22 |
Correct |
11 ms |
1016 KB |
Output is correct |
23 |
Correct |
21 ms |
2420 KB |
Output is correct |
24 |
Correct |
21 ms |
2552 KB |
Output is correct |
25 |
Correct |
20 ms |
2424 KB |
Output is correct |
26 |
Correct |
21 ms |
2552 KB |
Output is correct |
27 |
Correct |
21 ms |
2428 KB |
Output is correct |
28 |
Correct |
58 ms |
7032 KB |
Output is correct |
29 |
Correct |
24 ms |
3192 KB |
Output is correct |
30 |
Correct |
198 ms |
8440 KB |
Output is correct |
31 |
Correct |
254 ms |
10232 KB |
Output is correct |
32 |
Correct |
28 ms |
2808 KB |
Output is correct |
33 |
Correct |
68 ms |
4344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
760 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
760 KB |
Output is correct |
4 |
Correct |
7 ms |
760 KB |
Output is correct |
5 |
Correct |
7 ms |
760 KB |
Output is correct |
6 |
Correct |
5 ms |
760 KB |
Output is correct |
7 |
Correct |
5 ms |
760 KB |
Output is correct |
8 |
Correct |
12 ms |
1052 KB |
Output is correct |
9 |
Correct |
12 ms |
1044 KB |
Output is correct |
10 |
Correct |
11 ms |
1016 KB |
Output is correct |
11 |
Correct |
14 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
636 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
760 KB |
Output is correct |
20 |
Correct |
8 ms |
888 KB |
Output is correct |
21 |
Correct |
7 ms |
760 KB |
Output is correct |
22 |
Correct |
11 ms |
1016 KB |
Output is correct |
23 |
Correct |
21 ms |
2420 KB |
Output is correct |
24 |
Correct |
21 ms |
2552 KB |
Output is correct |
25 |
Correct |
20 ms |
2424 KB |
Output is correct |
26 |
Correct |
21 ms |
2552 KB |
Output is correct |
27 |
Correct |
21 ms |
2428 KB |
Output is correct |
28 |
Correct |
58 ms |
7032 KB |
Output is correct |
29 |
Correct |
24 ms |
3192 KB |
Output is correct |
30 |
Correct |
198 ms |
8440 KB |
Output is correct |
31 |
Correct |
254 ms |
10232 KB |
Output is correct |
32 |
Correct |
28 ms |
2808 KB |
Output is correct |
33 |
Correct |
68 ms |
4344 KB |
Output is correct |
34 |
Correct |
6 ms |
760 KB |
Output is correct |
35 |
Correct |
6 ms |
632 KB |
Output is correct |
36 |
Correct |
6 ms |
760 KB |
Output is correct |
37 |
Correct |
7 ms |
760 KB |
Output is correct |
38 |
Correct |
7 ms |
760 KB |
Output is correct |
39 |
Correct |
5 ms |
760 KB |
Output is correct |
40 |
Correct |
5 ms |
760 KB |
Output is correct |
41 |
Correct |
12 ms |
1052 KB |
Output is correct |
42 |
Correct |
12 ms |
1044 KB |
Output is correct |
43 |
Correct |
11 ms |
1016 KB |
Output is correct |
44 |
Correct |
14 ms |
1016 KB |
Output is correct |
45 |
Correct |
31 ms |
2692 KB |
Output is correct |
46 |
Correct |
31 ms |
2808 KB |
Output is correct |
47 |
Correct |
32 ms |
2808 KB |
Output is correct |
48 |
Incorrect |
31 ms |
2808 KB |
Output isn't correct |
49 |
Halted |
0 ms |
0 KB |
- |