#include <bits/stdc++.h>
using i64 = long long;
constexpr int Log = 16;
constexpr int N = 50000;
int am[Log][N], bm[Log][N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m, q;
std::cin >> n >> m >> q;
std::vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < m; i++) {
std::cin >> b[i];
}
for (int i = 0; i < n; i++)
am[0][i] = a[i];
for (int j = 0; (1 << (j + 1)) <= n; j++)
for (int i = 0; i + (1 << (j + 1)) <= n; i++)
am[j + 1][i] = std::max(am[j][i], am[j][i + (1 << j)]);
for (int i = 0; i < m; i++)
bm[0][i] = b[i];
for (int j = 0; (1 << (j + 1)) <= m; j++)
for (int i = 0; i + (1 << (j + 1)) <= m; i++)
bm[j + 1][i] = std::max(bm[j][i], bm[j][i + (1 << j)]);
std::unordered_map<i64, i64> dp;
std::function<int(int, int, int)> calc = [&](int x, int y, int t) {
i64 state = x * 1LL * m + y + t * (1LL << 60);
if (dp.count(state))
return dp[state];
int res = 0;
if (t == 0) {
int j = y + 1;
for (int k = Log - 1; k >= 0; k--)
if (j + (1 << k) <= m && bm[k][j] <= a[x])
j += 1 << k;
if (j == m)
res = std::max(res, m - 1 - y);
else
res = std::max(res, j - y + calc(x, j, !t));
j = y;
for (int k = Log - 1; k >= 0; k--)
if (j - (1 << k) >= 0 && bm[k][j - (1 << k)] <= a[x])
j -= 1 << k;
j--;
if (j == -1)
res = std::max(res, y);
else
res = std::max(res, y - j + calc(x, j, !t));
} else {
int i = x + 1;
for (int k = Log - 1; k >= 0; k--)
if (i + (1 << k) <= n && am[k][i] <= b[y])
i += 1 << k;
if (i == n)
res = std::max(res, n - 1 - x);
else
res = std::max(res, i - x + calc(i, y, !t));
i = x;
for (int k = Log - 1; k >= 0; k--)
if (i - (1 << k) >= 0 && am[k][i - (1 << k)] <= b[y])
i -= 1 << k;
i--;
if (i == -1)
res = std::max(res, x);
else
res = std::max(res, x - i + calc(i, y, !t));
}
return dp[state] = res;
};
while (q--) {
int s, t;
std::cin >> s >> t;
s--;
t--;
std::cout << std::max(calc(s, t, 0), calc(s, t, 1)) << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
596 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 |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
1072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
596 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 |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
1072 KB |
Output is correct |
23 |
Correct |
14 ms |
7460 KB |
Output is correct |
24 |
Correct |
13 ms |
7512 KB |
Output is correct |
25 |
Correct |
13 ms |
7380 KB |
Output is correct |
26 |
Correct |
13 ms |
7384 KB |
Output is correct |
27 |
Correct |
14 ms |
7372 KB |
Output is correct |
28 |
Correct |
25 ms |
13352 KB |
Output is correct |
29 |
Correct |
16 ms |
8268 KB |
Output is correct |
30 |
Correct |
52 ms |
12748 KB |
Output is correct |
31 |
Correct |
57 ms |
14932 KB |
Output is correct |
32 |
Correct |
14 ms |
7628 KB |
Output is correct |
33 |
Correct |
20 ms |
9416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
852 KB |
Output is correct |
9 |
Correct |
3 ms |
988 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
4 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
596 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 |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
852 KB |
Output is correct |
22 |
Correct |
3 ms |
1072 KB |
Output is correct |
23 |
Correct |
14 ms |
7460 KB |
Output is correct |
24 |
Correct |
13 ms |
7512 KB |
Output is correct |
25 |
Correct |
13 ms |
7380 KB |
Output is correct |
26 |
Correct |
13 ms |
7384 KB |
Output is correct |
27 |
Correct |
14 ms |
7372 KB |
Output is correct |
28 |
Correct |
25 ms |
13352 KB |
Output is correct |
29 |
Correct |
16 ms |
8268 KB |
Output is correct |
30 |
Correct |
52 ms |
12748 KB |
Output is correct |
31 |
Correct |
57 ms |
14932 KB |
Output is correct |
32 |
Correct |
14 ms |
7628 KB |
Output is correct |
33 |
Correct |
20 ms |
9416 KB |
Output is correct |
34 |
Correct |
2 ms |
724 KB |
Output is correct |
35 |
Correct |
2 ms |
608 KB |
Output is correct |
36 |
Correct |
2 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
724 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
3 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
988 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
4 ms |
980 KB |
Output is correct |
45 |
Correct |
20 ms |
7628 KB |
Output is correct |
46 |
Correct |
17 ms |
7624 KB |
Output is correct |
47 |
Correct |
15 ms |
7628 KB |
Output is correct |
48 |
Correct |
17 ms |
7652 KB |
Output is correct |
49 |
Correct |
16 ms |
7568 KB |
Output is correct |
50 |
Correct |
31 ms |
12444 KB |
Output is correct |
51 |
Correct |
30 ms |
12968 KB |
Output is correct |
52 |
Correct |
73 ms |
15036 KB |
Output is correct |
53 |
Correct |
73 ms |
14712 KB |
Output is correct |
54 |
Correct |
72 ms |
14696 KB |
Output is correct |
55 |
Incorrect |
99 ms |
20108 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |