#include <bits/stdc++.h>
#define NORTH 0
#define SOUTH 1
#define EAST 2
#define WEST 3
using namespace std;
const int N = 5e4 + 5;
struct Node {
int x, y;
int val;
int dir;
int dist;
bool operator<(const Node& n) const {
return val > n.val;
//return make_tuple(x, y, dir) < make_tuple(n.x, n.y, n.dir);
}
};
int h, w;
int a[N], b[N];
int solve(int x, int y, int dir) {
vector<int> nxt0(w + h + 1, 0);
vector<int> nxt1(w + h + 1, 0);
vector<int> nxt2(w + h + 1, 0);
vector<int> nxt3(w + h + 1, 0);
//unordered_map<pair<int, int>, int, Hasher2> dp;
vector<int> dp1sus(w + h + 1, 0);
vector<int> dp1jos(w + h + 1, 0);
vector<int> dp2st(w + h + 1, 0);
vector<int> dp2dr(w + h + 1, 0);
int hl, hr, wl, wr;
int ans = 0;
hl = hr = x;
wl = wr = y;
if (dir == 0) hl = max(hl - 1, 1);
else if (dir == 1) hr = min(hr + 1, h);
else if (dir == 2) wr = min(wr + 1, w);
else wl = max(wl - 1, 1);
priority_queue<Node> pq;
pq.push({x, y, 0, dir, 0});
while (!pq.empty()) {
auto n = pq.top();
//cout << n.x << " " << n.y << " " << n.dir << " " << n.dist << endl;
pq.pop();
ans = max(ans, n.dist);
//if (n.dir == NORTH && n.x == 1)
// continue;
//if (n.dir == SOUTH && n.x == h)
// continue;
// if (n.dir == EAST && n.y == w)
// continue;
//if (n.dir == WEST && n.y == 1)
// continue;
if (n.dir == NORTH) {
if (nxt0[n.y] != 0) {
} else {
while (hl > 1 && a[hl] <= b[n.y])
--hl;
if (a[hl] <= b[n.y]) {
ans = max(ans, n.dist + n.x - hl);
continue;
}
nxt0[n.y] = hl;
}
pair<int, int> urm = {nxt0[n.y], n.y};
if (dp1sus[n.y] >= n.dist + n.x - urm.first)
continue;
dp1sus[n.y] = n.dist + n.x - urm.first;
pq.push({urm.first, urm.second, a[urm.first], WEST, n.dist + n.x - urm.first});
pq.push({urm.first, urm.second, a[urm.first], EAST, n.dist + n.x - urm.first});
} else if (n.dir == SOUTH) {
if (nxt1[n.y] == 0) {
while (hr < h && a[hr] <= b[n.y])
++hr;
if (a[hr] <= b[n.y]) {
ans = max(ans, n.dist + hr - n.x);
continue;
}
nxt1[n.y] = hr;
}
pair<int, int> urm = {nxt1[n.y], n.y};
if (dp1jos[n.y] >= n.dist + urm.first - n.x)
continue;
dp1jos[n.y] = n.dist + urm.first - n.x;
pq.push({urm.first, urm.second, a[urm.first], WEST, n.dist + urm.first - n.x});
pq.push({urm.first, urm.second, a[urm.first], EAST, n.dist + urm.first - n.x});
} else if (n.dir == EAST) {
if (nxt2[n.x] == 0) {
while (wr < w && b[wr] <= a[n.x])
++wr;
if (b[wr] <= a[n.x]) {
ans = max(ans, n.dist + wr - n.y);
continue;
}
nxt2[n.x] = wr;
}
pair<int, int> urm = {n.x, nxt2[n.x]};
if (dp2dr[n.x] >= n.dist + urm.second - n.y)
continue;
dp2dr[n.x] = n.dist + urm.second - n.y;
pq.push({urm.first, urm.second, b[urm.second], NORTH, n.dist + urm.second - n.y});
pq.push({urm.first, urm.second, b[urm.second], SOUTH, n.dist + urm.second - n.y});
} else {
if (nxt3[n.x] == 0) {
while (wl > 1 && b[wl] <= a[n.x])
--wl;
if (b[wl] <= a[n.x]) {
ans = max(ans, n.dist + n.y - wl);
continue;
}
nxt3[n.x] = wl;
}
pair<int, int> urm = {n.x, nxt3[n.x]};
if (dp2st[n.x] >= n.dist + n.y - urm.second)
continue;
dp2st[n.x] = n.dist + n.y - urm.second;
pq.push({urm.first, urm.second, b[urm.second], NORTH, n.dist + n.y - urm.second});
pq.push({urm.first, urm.second, b[urm.second], SOUTH, n.dist + n.y - urm.second});
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int q;
cin >> h >> w >> q;
for (int i = 1; i <= h; ++i)
cin >> a[i];
for (int i = 1; i <= w; ++i)
cin >> b[i];
while (q--) {
int x, y;
cin >> x >> y;
int ans = 0;
for (int i = 0; i < 4; ++i)
ans = max(ans, solve(x, y, i));
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
6 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
6 ms |
468 KB |
Output is correct |
23 |
Correct |
14 ms |
3876 KB |
Output is correct |
24 |
Correct |
14 ms |
3832 KB |
Output is correct |
25 |
Correct |
14 ms |
3852 KB |
Output is correct |
26 |
Correct |
13 ms |
3860 KB |
Output is correct |
27 |
Correct |
13 ms |
3804 KB |
Output is correct |
28 |
Correct |
19 ms |
3828 KB |
Output is correct |
29 |
Correct |
13 ms |
3860 KB |
Output is correct |
30 |
Correct |
86 ms |
3808 KB |
Output is correct |
31 |
Correct |
95 ms |
3872 KB |
Output is correct |
32 |
Correct |
17 ms |
3824 KB |
Output is correct |
33 |
Correct |
37 ms |
3816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
5 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
16 ms |
472 KB |
Output is correct |
7 |
Correct |
15 ms |
476 KB |
Output is correct |
8 |
Correct |
170 ms |
468 KB |
Output is correct |
9 |
Correct |
178 ms |
468 KB |
Output is correct |
10 |
Correct |
188 ms |
468 KB |
Output is correct |
11 |
Correct |
301 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
6 ms |
468 KB |
Output is correct |
23 |
Correct |
14 ms |
3876 KB |
Output is correct |
24 |
Correct |
14 ms |
3832 KB |
Output is correct |
25 |
Correct |
14 ms |
3852 KB |
Output is correct |
26 |
Correct |
13 ms |
3860 KB |
Output is correct |
27 |
Correct |
13 ms |
3804 KB |
Output is correct |
28 |
Correct |
19 ms |
3828 KB |
Output is correct |
29 |
Correct |
13 ms |
3860 KB |
Output is correct |
30 |
Correct |
86 ms |
3808 KB |
Output is correct |
31 |
Correct |
95 ms |
3872 KB |
Output is correct |
32 |
Correct |
17 ms |
3824 KB |
Output is correct |
33 |
Correct |
37 ms |
3816 KB |
Output is correct |
34 |
Correct |
4 ms |
468 KB |
Output is correct |
35 |
Correct |
4 ms |
468 KB |
Output is correct |
36 |
Correct |
4 ms |
468 KB |
Output is correct |
37 |
Correct |
5 ms |
468 KB |
Output is correct |
38 |
Correct |
4 ms |
468 KB |
Output is correct |
39 |
Correct |
16 ms |
472 KB |
Output is correct |
40 |
Correct |
15 ms |
476 KB |
Output is correct |
41 |
Correct |
170 ms |
468 KB |
Output is correct |
42 |
Correct |
178 ms |
468 KB |
Output is correct |
43 |
Correct |
188 ms |
468 KB |
Output is correct |
44 |
Correct |
301 ms |
468 KB |
Output is correct |
45 |
Correct |
453 ms |
4280 KB |
Output is correct |
46 |
Correct |
405 ms |
3988 KB |
Output is correct |
47 |
Correct |
404 ms |
4172 KB |
Output is correct |
48 |
Correct |
383 ms |
4044 KB |
Output is correct |
49 |
Correct |
406 ms |
4020 KB |
Output is correct |
50 |
Correct |
700 ms |
4176 KB |
Output is correct |
51 |
Correct |
642 ms |
4016 KB |
Output is correct |
52 |
Correct |
4723 ms |
4248 KB |
Output is correct |
53 |
Correct |
4567 ms |
4260 KB |
Output is correct |
54 |
Correct |
4352 ms |
4056 KB |
Output is correct |
55 |
Execution timed out |
5057 ms |
4088 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |