#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define revv(V) reverse(allv(V))
#define sz(V) ((int)(V).size())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXH = 50005;
const int MAXW = 50005;
unordered_map<ll, ll> MP;
int AU[17][MAXH], AD[17][MAXH];
int BL[17][MAXW], BR[17][MAXW];
int A[MAXH], B[MAXW];
int H, W, Q;
ll f(int y, int x, int dr) {
ll key = (((ll(y) << 17) | x) << 3) | dr;
auto it = MP.find(key);
if(MP.end() != it) return it->second;
if(!dr) {
int Y = y;
for(int i = 17; i--;) if(AU[i][Y] < B[x])
Y = min(H, Y + (1<<i));
Y++;
if(A[Y] < B[x]) return MP[key] = H-y;
ll ret = max(f(Y, x, 1), f(Y, x, 3)) + (Y-y);
return MP[key] = ret;
} else if(2 == dr) {
int Y = y;
for(int i = 17; i--;) if(AD[i][Y] < B[x])
Y = max(1, Y - (1<<i));
Y--;
if(A[Y] < B[x]) return MP[key] = y-1;
ll ret = max(f(Y, x, 1), f(Y, x, 3)) + (y-Y);
return MP[key] = ret;
} else if(1 == dr) {
int X = x;
for(int i = 17; i--;) if(BR[i][X] < A[y])
X = min(W, X + (1<<i));
X++;
if(B[X] < A[y]) return MP[key] = W-x;
ll ret = max(f(y, X, 0), f(y, X, 2)) + (X-x);
return MP[key] = ret;
} else {
int X = x;
for(int i = 17; i--;) if(BL[i][X] < A[y])
X = max(1, X - (1<<i));
X--;
if(B[X] < A[y]) return MP[key] = x-1;
ll ret = max(f(y, X, 0), f(y, X, 2)) + (x-X);
return MP[key] = ret;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> H >> W >> Q;
for(int i = 1; i <= H; i++) cin >> A[i];
for(int i = 1; i <= W; i++) cin >> B[i];
for(int i = 1; i <= H; i++) {
AU[0][i] = A[i+1];
AD[0][i] = A[i-1];
}
for(int i = 1; i <= W; i++) {
BL[0][i] = B[i-1];
BR[0][i] = B[i+1];
}
for(int j = 0; j < 16; j++) {
for(int i = 1; i <= H; i++) {
AU[j+1][i] = AU[j][i];
if(i + (1<<j) <= H) upmax(AU[j+1][i], AU[j][i + (1<<j)]);
AD[j+1][i] = AD[j][i];
if(0 < i - (1<<j)) upmax(AD[j+1][i], AD[j][i - (1<<j)]);
}
for(int i = 1; i <= W; i++) {
BL[j+1][i] = BL[j][i];
if(0 < i - (1<<j)) upmax(BL[j+1][i], BL[j][i - (1<<j)]);
BR[j+1][i] = BR[j][i];
if(i + (1<<j) <= W) upmax(BR[j+1][i], BR[j][i + (1<<j)]);
}
}
for(int y, x; Q--;) {
cin >> y >> x;
ll ret = 0;
for(int dr = 0; dr < 4; dr++)
upmax(ret, f(y, x, dr));
printf("%lld\n", ret);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
652 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
652 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
2 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
2 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1280 KB |
Output is correct |
18 |
Correct |
2 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1536 KB |
Output is correct |
20 |
Correct |
4 ms |
1664 KB |
Output is correct |
21 |
Correct |
3 ms |
1536 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
652 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
2 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
2 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1280 KB |
Output is correct |
18 |
Correct |
2 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1536 KB |
Output is correct |
20 |
Correct |
4 ms |
1664 KB |
Output is correct |
21 |
Correct |
3 ms |
1536 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
24 ms |
14968 KB |
Output is correct |
24 |
Correct |
24 ms |
14968 KB |
Output is correct |
25 |
Correct |
23 ms |
14976 KB |
Output is correct |
26 |
Correct |
26 ms |
14968 KB |
Output is correct |
27 |
Correct |
23 ms |
15004 KB |
Output is correct |
28 |
Correct |
42 ms |
20484 KB |
Output is correct |
29 |
Correct |
25 ms |
15744 KB |
Output is correct |
30 |
Correct |
83 ms |
22940 KB |
Output is correct |
31 |
Correct |
93 ms |
24476 KB |
Output is correct |
32 |
Correct |
25 ms |
15488 KB |
Output is correct |
33 |
Correct |
36 ms |
17284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1408 KB |
Output is correct |
2 |
Correct |
3 ms |
1536 KB |
Output is correct |
3 |
Correct |
4 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
1536 KB |
Output is correct |
5 |
Correct |
3 ms |
1408 KB |
Output is correct |
6 |
Correct |
3 ms |
1536 KB |
Output is correct |
7 |
Correct |
3 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
1792 KB |
Output is correct |
10 |
Correct |
5 ms |
1792 KB |
Output is correct |
11 |
Correct |
6 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
652 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
2 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
2 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1280 KB |
Output is correct |
18 |
Correct |
2 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1536 KB |
Output is correct |
20 |
Correct |
4 ms |
1664 KB |
Output is correct |
21 |
Correct |
3 ms |
1536 KB |
Output is correct |
22 |
Correct |
4 ms |
1792 KB |
Output is correct |
23 |
Correct |
24 ms |
14968 KB |
Output is correct |
24 |
Correct |
24 ms |
14968 KB |
Output is correct |
25 |
Correct |
23 ms |
14976 KB |
Output is correct |
26 |
Correct |
26 ms |
14968 KB |
Output is correct |
27 |
Correct |
23 ms |
15004 KB |
Output is correct |
28 |
Correct |
42 ms |
20484 KB |
Output is correct |
29 |
Correct |
25 ms |
15744 KB |
Output is correct |
30 |
Correct |
83 ms |
22940 KB |
Output is correct |
31 |
Correct |
93 ms |
24476 KB |
Output is correct |
32 |
Correct |
25 ms |
15488 KB |
Output is correct |
33 |
Correct |
36 ms |
17284 KB |
Output is correct |
34 |
Correct |
4 ms |
1408 KB |
Output is correct |
35 |
Correct |
3 ms |
1536 KB |
Output is correct |
36 |
Correct |
4 ms |
1408 KB |
Output is correct |
37 |
Correct |
3 ms |
1536 KB |
Output is correct |
38 |
Correct |
3 ms |
1408 KB |
Output is correct |
39 |
Correct |
3 ms |
1536 KB |
Output is correct |
40 |
Correct |
3 ms |
1536 KB |
Output is correct |
41 |
Correct |
5 ms |
1792 KB |
Output is correct |
42 |
Correct |
5 ms |
1792 KB |
Output is correct |
43 |
Correct |
5 ms |
1792 KB |
Output is correct |
44 |
Correct |
6 ms |
2048 KB |
Output is correct |
45 |
Correct |
27 ms |
15480 KB |
Output is correct |
46 |
Correct |
28 ms |
15484 KB |
Output is correct |
47 |
Correct |
27 ms |
15480 KB |
Output is correct |
48 |
Correct |
28 ms |
15352 KB |
Output is correct |
49 |
Correct |
28 ms |
15352 KB |
Output is correct |
50 |
Correct |
55 ms |
19996 KB |
Output is correct |
51 |
Correct |
51 ms |
20508 KB |
Output is correct |
52 |
Correct |
162 ms |
30012 KB |
Output is correct |
53 |
Correct |
164 ms |
29756 KB |
Output is correct |
54 |
Correct |
151 ms |
29884 KB |
Output is correct |
55 |
Correct |
193 ms |
32996 KB |
Output is correct |
56 |
Correct |
2185 ms |
174096 KB |
Output is correct |
57 |
Correct |
528 ms |
55572 KB |
Output is correct |
58 |
Correct |
494 ms |
53924 KB |
Output is correct |
59 |
Correct |
479 ms |
53880 KB |
Output is correct |