#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e4 + 10, LOG = 16;
int n, m, q;
int a[N], b[N];
int sta[N][LOG], stb[N][LOG];
int L[N], R[N], U[N], D[N];
map<pair<int, int>, int> dp;
int geta(int l, int r) {
int len = r - l + 1;
int bit = 31 - __builtin_clz(len);
return max(sta[l][bit], sta[r - (1 << bit) + 1][bit]);
}
int getb(int l, int r) {
int len = r - l + 1;
int bit = 31 - __builtin_clz(len);
return max(stb[l][bit], stb[r - (1 << bit) + 1][bit]);
}
int findL(int x, int y) {
int l = 0, r = y - 1;
int ans = l;
while(l <= r) {
int mid = (l + r) / 2;
// if(x == 2 && y == 2) cout << "mid " << mid << " " << getb(mid, y - 1) << "\n";
if(getb(mid, y - 1) > a[x]) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
return ans;
}
int findR(int x, int y) {
int l = y + 1, r = m + 1;
int ans = r;
while(l <= r) {
int mid = (l + r) / 2;
if(getb(y + 1, mid) > a[x]) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
return ans;
}
int findU(int x, int y) {
int l = 0, r = x - 1;
int ans = l;
while(l <= r) {
int mid = (l + r) / 2;
if(geta(mid, x - 1) > b[y]) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
return ans;
}
int findD(int x, int y) {
int l = x + 1, r = n + 1;
int ans = r;
while(l <= r) {
int mid = (l + r) / 2;
if(geta(x + 1, mid) > b[y]) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
return ans;
}
int add(int x, int y) {
if(x == 0 || x == n + 1 || y == 0 || y == m + 1) return 0;
if(dp[{x, y}] != 0) return dp[{x, y}];
// cout << "add " << x << " " << y << "\n";
if(a[x] < b[y]) {
int U = findU(x, y);
int D = findD(x, y);
dp[{x, y}] = max(dp[{x, y}], add(U, y) + x - U);
dp[{x, y}] = max(dp[{x, y}], add(D, y) + D - x);
} else {
int L = findL(x, y);
int R = findR(x, y);
dp[{x, y}] = max(dp[{x, y}], add(x, L) + y - L);
dp[{x, y}] = max(dp[{x, y}], add(x, R) + R - y);
}
if(dp[{x, y}] == 0) dp[{x, y}] = 1;
// cout << "end " << x << " " << y << " " << dp[{x, y}] << "\n";
return dp[{x, y}];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
sta[i][0] = a[i];
}
for(int i = 1; i <= m; i++) {
cin >> b[i];
stb[i][0] = b[i];
}
for(int j = 1; j < LOG; j++) {
for(int i = 0; i + (1 << (j - 1)) <= n; i++) {
sta[i][j] = max(sta[i][j - 1], sta[i + (1 << (j - 1))][j - 1]);
}
}
for(int j = 1; j < LOG; j++) {
for(int i = 0; i + (1 << (j - 1)) <= m; i++) {
stb[i][j] = max(stb[i][j - 1], stb[i + (1 << (j - 1))][j - 1]);
}
}
while(q--) {
int x, y;
cin >> x >> y;
int L = findL(x, y);
int R = findR(x, y);
int U = findU(x, y);
int D = findD(x, y);
// cout << L << " " << R << " " << U << " " << D << "\n";
int ans = 1;
if(L == 0) ans = max(ans, y);
else {
if(dp[{x, L}] == 0) dp[{x, L}] = add(x, L);
ans = max(ans, dp[{x, L}] + y - L);
}
if(R == m + 1) ans = max(ans, m - y + 1);
else {
if(dp[{x, R}] == 0) dp[{x, R}] = add(x, R);
ans = max(ans, dp[{x, R}] + R - y);
}
if(U == 0) ans = max(ans, x);
else {
if(dp[{U, y}] == 0) dp[{U, y}] = add(U, y);
ans = max(ans, dp[{U, y}] + x - U);
}
if(L == 0) ans = max(ans, n - x + 1);
else {
if(dp[{D, y}] == 0) dp[{D, y}] = add(D, y);
ans = max(ans, dp[{D, y}] + D - x);
}
// dp[{x, y}] = ans;
cout << ans - 1 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6604 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
5 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6604 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
5 ms |
7004 KB |
Output is correct |
23 |
Correct |
12 ms |
14940 KB |
Output is correct |
24 |
Correct |
12 ms |
15172 KB |
Output is correct |
25 |
Correct |
13 ms |
14940 KB |
Output is correct |
26 |
Correct |
12 ms |
15164 KB |
Output is correct |
27 |
Correct |
12 ms |
14940 KB |
Output is correct |
28 |
Correct |
42 ms |
21804 KB |
Output is correct |
29 |
Correct |
15 ms |
15964 KB |
Output is correct |
30 |
Correct |
94 ms |
22120 KB |
Output is correct |
31 |
Correct |
118 ms |
24148 KB |
Output is correct |
32 |
Correct |
15 ms |
15448 KB |
Output is correct |
33 |
Correct |
33 ms |
17232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7008 KB |
Output is correct |
2 |
Correct |
3 ms |
6748 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
4 ms |
6748 KB |
Output is correct |
5 |
Correct |
4 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
7 ms |
7004 KB |
Output is correct |
9 |
Correct |
6 ms |
7004 KB |
Output is correct |
10 |
Correct |
6 ms |
7004 KB |
Output is correct |
11 |
Correct |
7 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6604 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
5 ms |
7004 KB |
Output is correct |
23 |
Correct |
12 ms |
14940 KB |
Output is correct |
24 |
Correct |
12 ms |
15172 KB |
Output is correct |
25 |
Correct |
13 ms |
14940 KB |
Output is correct |
26 |
Correct |
12 ms |
15164 KB |
Output is correct |
27 |
Correct |
12 ms |
14940 KB |
Output is correct |
28 |
Correct |
42 ms |
21804 KB |
Output is correct |
29 |
Correct |
15 ms |
15964 KB |
Output is correct |
30 |
Correct |
94 ms |
22120 KB |
Output is correct |
31 |
Correct |
118 ms |
24148 KB |
Output is correct |
32 |
Correct |
15 ms |
15448 KB |
Output is correct |
33 |
Correct |
33 ms |
17232 KB |
Output is correct |
34 |
Correct |
4 ms |
7008 KB |
Output is correct |
35 |
Correct |
3 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
4 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6744 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
7 ms |
7004 KB |
Output is correct |
42 |
Correct |
6 ms |
7004 KB |
Output is correct |
43 |
Correct |
6 ms |
7004 KB |
Output is correct |
44 |
Correct |
7 ms |
7000 KB |
Output is correct |
45 |
Correct |
19 ms |
15192 KB |
Output is correct |
46 |
Correct |
16 ms |
15196 KB |
Output is correct |
47 |
Correct |
16 ms |
15452 KB |
Output is correct |
48 |
Correct |
21 ms |
15424 KB |
Output is correct |
49 |
Correct |
20 ms |
15448 KB |
Output is correct |
50 |
Correct |
53 ms |
20500 KB |
Output is correct |
51 |
Correct |
47 ms |
21400 KB |
Output is correct |
52 |
Correct |
145 ms |
25760 KB |
Output is correct |
53 |
Correct |
135 ms |
25604 KB |
Output is correct |
54 |
Correct |
151 ms |
25172 KB |
Output is correct |
55 |
Correct |
222 ms |
30548 KB |
Output is correct |
56 |
Correct |
2033 ms |
141320 KB |
Output is correct |
57 |
Correct |
487 ms |
48724 KB |
Output is correct |
58 |
Correct |
506 ms |
46932 KB |
Output is correct |
59 |
Correct |
464 ms |
46924 KB |
Output is correct |