#include <bits/stdc++.h>
using namespace std;
const int MAX = 50005;
const int L = 18;
int h, w, q, a[MAX], b[MAX], logs[MAX];
int maxa[MAX][L], maxb[MAX][L];
unordered_map<int, bool> was[4];
unordered_map<int, long long> dp[4];
int MaxA(int L, int R) {
int k = logs[R - L + 1];
return max(maxa[L][k], maxa[R - (1 << k) + 1][k]);
}
int MaxB(int L, int R) {
int k = logs[R - L + 1];
return max(maxb[L][k], maxb[R - (1 << k) + 1][k]);
}
pair<int, int> Go(int x, int y, int dir) {
if (dir == 0) {
// up
if (x == 0) {
return make_pair(x, y);
}
x--;
{
if (MaxA(0, x) < b[y]) {
return make_pair(0, y);
}
int low = 0, high = x, pos = x;
while (low <= high) {
int mid = low + high >> 1;
if (MaxA(mid, x) >= b[y]) {
pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return make_pair(pos, y);
}
/* while (x > 0 && a[x] < b[y]) {
x -= 1;
}
return make_pair(x, y); */
}
if (dir == 1) {
// down
if (x == h - 1) {
return make_pair(x, y);
}
x += 1;
{
if (MaxA(x, h - 1) < b[y]) {
return make_pair(h - 1, y);
}
int low = x, high = h - 1, pos = h - 1;
while (low <= high) {
int mid = low + high >> 1;
if (MaxA(x, mid) >= b[y]) {
pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return make_pair(pos, y);
}
/* while (x + 1 < h && a[x] < b[y]) {
x += 1;
}
return make_pair(x, y); */
}
if (dir == 2) {
// left
if (y == 0) {
return make_pair(x, y);
}
y -= 1;
{
if (MaxB(0, y) < a[x]) {
return make_pair(x, 0);
}
int low = 0, high = y, pos = y;
while (low <= high) {
int mid = low + high >> 1;
if (MaxB(mid, y) >= a[x]) {
pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return make_pair(x, pos);
}
/* while (y > 0 && a[x] > b[y]) {
y -= 1;
}
return make_pair(x, y); */
}
if (dir == 3) {
// right
if (y == w - 1) {
return make_pair(x, y);
}
y += 1;
{
if (MaxB(y, w - 1) < a[x]) {
return make_pair(x, w - 1);
}
int low = y, high = w - 1, pos = y;
while (low <= high) {
int mid = low + high >> 1;
if (MaxB(y, mid) >= a[x]) {
pos = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return make_pair(x, pos);
}
/* while (y + 1 < w && a[x] > b[y]) {
y += 1;
}
return make_pair(x, y); */
}
assert(false);
return make_pair(-1, -1);
}
long long Solve(int x, int y, int dir) {
if (dir <= 1) {
if (a[x] < b[y]) {
return 0LL;
}
} else {
if (a[x] > b[y]) {
return 0LL;
}
}
int curr = x * w + y;
if (was[dir][curr]) {
return dp[dir][curr];
}
was[dir][curr] = true;
if (dir == 0 || dir == 1) {
{
pair<int, int> nxt = Go(x, y, 2);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 2) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
{
pair<int, int> nxt = Go(x, y, 3);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 3) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
}
if (dir == 2 || dir == 3) {
{
pair<int, int> nxt = Go(x, y, 0);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 0) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
{
pair<int, int> nxt = Go(x, y, 1);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 1) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
}
return dp[dir][curr];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//int h, w, q;
cin >> h >> w >> q;
//vector<int> a(h);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
//vector<int> b(w);
for (int i = 0; i < w; i++) {
cin >> b[i];
}
//const int L = 20;
//vector<vector<int>> maxa(h, vector<int>(L));
//vector<vector<int>> maxb(w, vector<int>(L));
for (int i = 0; i < h; i++) {
maxa[i][0] = a[i];
}
for (int i = 0; i < w; i++) {
maxb[i][0] = b[i];
}
for (int j = 1; j < L; j++) {
for (int i = 0; i + (1 << j) <= h; i++) {
maxa[i][j] = max(maxa[i][j - 1], maxa[i + (1 << (j - 1))][j - 1]);
}
}
for (int j = 1; j < L; j++) {
for (int i = 0; i + (1 << j) <= w; i++) {
maxb[i][j] = max(maxb[i][j - 1], maxb[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 2; i <= max(h, w); i++) {
logs[i] = logs[i >> 1] + 1;
}
while (q--) {
int x, y;
cin >> x >> y;
--x; --y;
long long res = 1;
for (int dir = 0; dir < 4; dir++) {
pair<int, int> cell = Go(x, y, dir);
int nx = cell.first;
int ny = cell.second;
if (nx != x || ny != y) {
res = max(res, Solve(nx, ny, dir) + abs(x - nx) + abs(y - ny));
}
}
cout << res << '\n';
for (int dir = 0; dir < 4; dir++) {
was[dir].clear();
dp[dir].clear();
}
}
return 0;
}
Compilation message
abduction2.cpp: In function 'std::pair<int, int> Go(int, int, int)':
abduction2.cpp:37:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = low + high >> 1;
| ~~~~^~~~~~
abduction2.cpp:64:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = low + high >> 1;
| ~~~~^~~~~~
abduction2.cpp:91:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mid = low + high >> 1;
| ~~~~^~~~~~
abduction2.cpp:118:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
118 | int mid = low + high >> 1;
| ~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
3240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
3240 KB |
Output is correct |
23 |
Correct |
11 ms |
8028 KB |
Output is correct |
24 |
Correct |
11 ms |
8028 KB |
Output is correct |
25 |
Correct |
10 ms |
8028 KB |
Output is correct |
26 |
Correct |
11 ms |
8028 KB |
Output is correct |
27 |
Correct |
11 ms |
7928 KB |
Output is correct |
28 |
Correct |
24 ms |
14328 KB |
Output is correct |
29 |
Correct |
12 ms |
8792 KB |
Output is correct |
30 |
Correct |
48 ms |
15752 KB |
Output is correct |
31 |
Correct |
62 ms |
19140 KB |
Output is correct |
32 |
Correct |
12 ms |
8284 KB |
Output is correct |
33 |
Correct |
20 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
11 ms |
2908 KB |
Output is correct |
7 |
Correct |
11 ms |
2908 KB |
Output is correct |
8 |
Correct |
49 ms |
3124 KB |
Output is correct |
9 |
Correct |
49 ms |
3164 KB |
Output is correct |
10 |
Correct |
54 ms |
3152 KB |
Output is correct |
11 |
Correct |
74 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
3240 KB |
Output is correct |
23 |
Correct |
11 ms |
8028 KB |
Output is correct |
24 |
Correct |
11 ms |
8028 KB |
Output is correct |
25 |
Correct |
10 ms |
8028 KB |
Output is correct |
26 |
Correct |
11 ms |
8028 KB |
Output is correct |
27 |
Correct |
11 ms |
7928 KB |
Output is correct |
28 |
Correct |
24 ms |
14328 KB |
Output is correct |
29 |
Correct |
12 ms |
8792 KB |
Output is correct |
30 |
Correct |
48 ms |
15752 KB |
Output is correct |
31 |
Correct |
62 ms |
19140 KB |
Output is correct |
32 |
Correct |
12 ms |
8284 KB |
Output is correct |
33 |
Correct |
20 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2648 KB |
Output is correct |
39 |
Correct |
11 ms |
2908 KB |
Output is correct |
40 |
Correct |
11 ms |
2908 KB |
Output is correct |
41 |
Correct |
49 ms |
3124 KB |
Output is correct |
42 |
Correct |
49 ms |
3164 KB |
Output is correct |
43 |
Correct |
54 ms |
3152 KB |
Output is correct |
44 |
Correct |
74 ms |
3416 KB |
Output is correct |
45 |
Correct |
13 ms |
8024 KB |
Output is correct |
46 |
Correct |
12 ms |
8024 KB |
Output is correct |
47 |
Correct |
13 ms |
8272 KB |
Output is correct |
48 |
Correct |
13 ms |
8024 KB |
Output is correct |
49 |
Correct |
16 ms |
8028 KB |
Output is correct |
50 |
Correct |
445 ms |
17036 KB |
Output is correct |
51 |
Correct |
387 ms |
17516 KB |
Output is correct |
52 |
Correct |
2757 ms |
23212 KB |
Output is correct |
53 |
Correct |
2933 ms |
22880 KB |
Output is correct |
54 |
Correct |
2491 ms |
21920 KB |
Output is correct |
55 |
Correct |
4237 ms |
31344 KB |
Output is correct |
56 |
Correct |
685 ms |
10836 KB |
Output is correct |
57 |
Correct |
230 ms |
10516 KB |
Output is correct |
58 |
Correct |
203 ms |
10580 KB |
Output is correct |
59 |
Correct |
263 ms |
10832 KB |
Output is correct |