#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using namespace std;
struct node
{
int r, c, i;
};
const int mx = 50'000;
vi A(1+mx), B(1+mx);
const ll inf = 1'000'000'000'000'000LL;
int congestion(int i)
{
if(i > 0)
return A[i];
else
return B[-i];
}
struct inedge
{
int i; //index
int c; //congestion
ll d; //dp
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int H, W, Q;
cin >> H >> W >> Q;
for(int i = 1; i <= H; i++)
cin >> A[i];
for(int j = 1; j <= W; j++)
cin >> B[j];
vi roads;
for(int i = 1; i <= H; i++)
roads.push_back(i);
for(int j = 1; j <= W; j++)
roads.push_back(-j);
sort(roads.begin(), roads.end(), [] (int p, int q)
{
return congestion(p) < congestion(q);
});
A[0] = B[0] = 0;
for(int q = 1; q <= Q; q++)
{
int S, T;
cin >> S >> T;
ll res = 0;
// vll horiz_left(1+H, -inf), horiz_center(1+H, -inf), horiz_right(1+H, -inf); //dp
// vll vert_up(1+W, -inf), vert_center(1+W, -inf), vert_down(1+W, -inf);
ll ui = S-1, di = S+1, li = T-1, ri = T+1;
vector<inedge> horiz_in[1+H], vert_in[1+W];
horiz_in[S].push_back({T, 0, 0});
vert_in[T].push_back({S, 0, 0});
for(int f : roads)
{
if(f > 0) //road is horizontal
{
ll hl = -inf, hc = -inf, hr = -inf;
int z = f;
while(li >= 1 && B[li] < A[z])
li--;
while(ri <= W && B[ri] < A[z])
ri++;
for(inedge x : horiz_in[z])
{
if(x.c >= A[z]) continue;
if(x.i <= li || ri <= x.i) continue;
if(A[z] < B[T])
hc = max(hc, x.d + abs(T - x.i));
if(li != 0 && (x.i <= T || A[z] > B[T]))
{
hl = max(hl, x.d + abs(li - x.i));
}
else if(li == 0 && (x.i <= T || A[z] > B[T]))
res = max(res, x.d + abs(x.i - 1));
if(ri != W+1 && (x.i >= T || A[z] > B[T]))
{
hr = max(hr, x.d + abs(ri - x.i));
}
else if(ri == W+1 && (x.i >= T || A[z] > B[T]))
res = max(res, x.d + abs(x.i - W));
}
if(A[z] < B[T])
{
vert_in[T].push_back({z, A[z], hc});
}
if(li != 0)
vert_in[li].push_back({z, A[z], hl});
if(ri != W+1)
vert_in[ri].push_back({z, A[z], hr});
}
else
{
ll vu = -inf, vc = -inf, vd = -inf;
int z = -f;
while(ui >= 1 && A[ui] < B[z])
ui--;
while(di <= H && A[di] < B[z])
di++;
for(inedge x : vert_in[z])
{
if(x.c >= B[z]) continue;
if(x.i <= ui || di <= x.i) continue;
if(B[z] < A[S])
vc = max(vc, x.d + abs(S - x.i));
if(ui != 0 && (x.i <= S || B[z] > A[S]))
{
vu = max(vu, x.d + abs(ui - x.i));
}
else if(ui == 0 && (x.i <= S || B[z] > A[S]))
res = max(res, x.d + abs(1 - x.i));
if(di != H+1 && (x.i >= S || B[z] > A[S]))
{
vd = max(vd, x.d + abs(di - x.i));
}
else if(di == H+1 && (x.i >= S || B[z] > A[S]))
res = max(res, x.d + abs(H - x.i));
}
if(B[z] < A[S])
{
horiz_in[S].push_back({z, B[z], vc});
}
if(ui != 0)
horiz_in[ui].push_back({z, B[z], vu});
if(di != H+1)
horiz_in[di].push_back({z, B[z], vd});
}
}
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
996 KB |
Output is correct |
15 |
Correct |
2 ms |
992 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1000 KB |
Output is correct |
19 |
Correct |
2 ms |
992 KB |
Output is correct |
20 |
Correct |
2 ms |
996 KB |
Output is correct |
21 |
Correct |
2 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
996 KB |
Output is correct |
15 |
Correct |
2 ms |
992 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1000 KB |
Output is correct |
19 |
Correct |
2 ms |
992 KB |
Output is correct |
20 |
Correct |
2 ms |
996 KB |
Output is correct |
21 |
Correct |
2 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
980 KB |
Output is correct |
23 |
Correct |
37 ms |
8480 KB |
Output is correct |
24 |
Correct |
38 ms |
9636 KB |
Output is correct |
25 |
Correct |
47 ms |
9368 KB |
Output is correct |
26 |
Correct |
40 ms |
9552 KB |
Output is correct |
27 |
Correct |
38 ms |
8652 KB |
Output is correct |
28 |
Correct |
37 ms |
7616 KB |
Output is correct |
29 |
Correct |
40 ms |
8356 KB |
Output is correct |
30 |
Correct |
36 ms |
9536 KB |
Output is correct |
31 |
Correct |
35 ms |
9500 KB |
Output is correct |
32 |
Correct |
34 ms |
9196 KB |
Output is correct |
33 |
Correct |
33 ms |
9412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
1128 KB |
Output is correct |
2 |
Correct |
22 ms |
1128 KB |
Output is correct |
3 |
Correct |
22 ms |
1136 KB |
Output is correct |
4 |
Correct |
20 ms |
1140 KB |
Output is correct |
5 |
Correct |
28 ms |
1116 KB |
Output is correct |
6 |
Correct |
29 ms |
1164 KB |
Output is correct |
7 |
Correct |
28 ms |
1216 KB |
Output is correct |
8 |
Correct |
41 ms |
1360 KB |
Output is correct |
9 |
Correct |
39 ms |
1152 KB |
Output is correct |
10 |
Correct |
40 ms |
1236 KB |
Output is correct |
11 |
Correct |
38 ms |
1276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
996 KB |
Output is correct |
15 |
Correct |
2 ms |
992 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
2 ms |
1000 KB |
Output is correct |
19 |
Correct |
2 ms |
992 KB |
Output is correct |
20 |
Correct |
2 ms |
996 KB |
Output is correct |
21 |
Correct |
2 ms |
996 KB |
Output is correct |
22 |
Correct |
2 ms |
980 KB |
Output is correct |
23 |
Correct |
37 ms |
8480 KB |
Output is correct |
24 |
Correct |
38 ms |
9636 KB |
Output is correct |
25 |
Correct |
47 ms |
9368 KB |
Output is correct |
26 |
Correct |
40 ms |
9552 KB |
Output is correct |
27 |
Correct |
38 ms |
8652 KB |
Output is correct |
28 |
Correct |
37 ms |
7616 KB |
Output is correct |
29 |
Correct |
40 ms |
8356 KB |
Output is correct |
30 |
Correct |
36 ms |
9536 KB |
Output is correct |
31 |
Correct |
35 ms |
9500 KB |
Output is correct |
32 |
Correct |
34 ms |
9196 KB |
Output is correct |
33 |
Correct |
33 ms |
9412 KB |
Output is correct |
34 |
Correct |
23 ms |
1128 KB |
Output is correct |
35 |
Correct |
22 ms |
1128 KB |
Output is correct |
36 |
Correct |
22 ms |
1136 KB |
Output is correct |
37 |
Correct |
20 ms |
1140 KB |
Output is correct |
38 |
Correct |
28 ms |
1116 KB |
Output is correct |
39 |
Correct |
29 ms |
1164 KB |
Output is correct |
40 |
Correct |
28 ms |
1216 KB |
Output is correct |
41 |
Correct |
41 ms |
1360 KB |
Output is correct |
42 |
Correct |
39 ms |
1152 KB |
Output is correct |
43 |
Correct |
40 ms |
1236 KB |
Output is correct |
44 |
Correct |
38 ms |
1276 KB |
Output is correct |
45 |
Correct |
614 ms |
11740 KB |
Output is correct |
46 |
Correct |
596 ms |
11992 KB |
Output is correct |
47 |
Correct |
698 ms |
11296 KB |
Output is correct |
48 |
Correct |
670 ms |
11116 KB |
Output is correct |
49 |
Correct |
667 ms |
11704 KB |
Output is correct |
50 |
Correct |
745 ms |
13596 KB |
Output is correct |
51 |
Correct |
737 ms |
12752 KB |
Output is correct |
52 |
Correct |
1094 ms |
13184 KB |
Output is correct |
53 |
Correct |
1046 ms |
14232 KB |
Output is correct |
54 |
Correct |
1115 ms |
14832 KB |
Output is correct |
55 |
Correct |
1074 ms |
13408 KB |
Output is correct |
56 |
Correct |
628 ms |
9192 KB |
Output is correct |
57 |
Correct |
651 ms |
13144 KB |
Output is correct |
58 |
Correct |
654 ms |
13224 KB |
Output is correct |
59 |
Correct |
700 ms |
13020 KB |
Output is correct |