#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 5e4 + 5, LG = 16;
int n, m, q;
int a[N], b[N], spt[2][LG][N], lg[N];
map<int, long long> mp[2][N];
void bld(int spt[LG][N], int *A, int n) {
for (int i = 1; i <= n; ++i) {
spt[0][i] = A[i];
}
for (int j = 1; j <= lg[n]; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
spt[j][i] = max(spt[j - 1][i], spt[j - 1][i + (1 << j - 1)]);
}
}
}
int qry(int spt[LG][N], int l, int r) {
int k = lg[r - l + 1];
return max(spt[k][l], spt[k][r - (1 << k) + 1]);
}
int prv(int spt[LG][N], int i, int x) {
int l = 1, r = i - 1, res = 0;
while (l <= r) {
int md = (l + r) / 2;
if (qry(spt, md, i - 1) > x) {
res = md;
l = md + 1;
} else {
r = md - 1;
}
}
return res;
}
int nxt(int spt[LG][N], int i, int n, int x) {
int l = i + 1, r = n, res = n + 1;
while (l <= r) {
int md = (l + r) / 2;
if (qry(spt, i + 1, md) > x) {
res = md;
r = md - 1;
} else {
l = md + 1;
}
}
return res;
}
long long solve(int i, int j, int dr) {
if (mp[dr][i].count(j)) {
return mp[dr][i][j];
}
long long res = 0;
if (dr == 0) {
int lt = prv(spt[1], j, a[i]), rt = nxt(spt[1], j, m, a[i]);
res = max({res, lt == 0 ? j - 1 : solve(i, lt, 1) + j - lt, rt == m + 1 ? m - j : solve(i, rt, 1) + rt - j});
} else {
int lt = prv(spt[0], i, b[j]), rt = nxt(spt[0], i, n, b[j]);
res = max({res, lt == 0 ? i - 1 : solve(lt, j, 0) + i - lt, rt == n + 1 ? n - i : solve(rt, j, 0) + rt - i});
}
return mp[dr][i][j] = res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m >> q;
for (int i = 2; i <= max(n, m); ++i) {
lg[i] = lg[i / 2] + 1;
}
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
cin >> b[i];
}
bld(spt[0], a, n);
bld(spt[1], b, m);
while (q--) {
int s, t; cin >> s >> t;
cout << max(solve(s, t, 0), solve(s, t, 1)) << "\n";
}
return 0;
}
Compilation message
abduction2.cpp: In function 'void bld(int (*)[50005], int*, int)':
abduction2.cpp:23:67: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
23 | spt[j][i] = max(spt[j - 1][i], spt[j - 1][i + (1 << j - 1)]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10888 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10888 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10896 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
10896 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
2 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
11156 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10888 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10896 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
10896 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
2 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
11156 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
8 ms |
12544 KB |
Output is correct |
24 |
Correct |
8 ms |
12612 KB |
Output is correct |
25 |
Correct |
9 ms |
12636 KB |
Output is correct |
26 |
Correct |
8 ms |
12636 KB |
Output is correct |
27 |
Correct |
8 ms |
12672 KB |
Output is correct |
28 |
Correct |
15 ms |
17756 KB |
Output is correct |
29 |
Correct |
10 ms |
13312 KB |
Output is correct |
30 |
Correct |
26 ms |
18772 KB |
Output is correct |
31 |
Correct |
27 ms |
20688 KB |
Output is correct |
32 |
Correct |
8 ms |
13008 KB |
Output is correct |
33 |
Correct |
11 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
2 ms |
11012 KB |
Output is correct |
4 |
Correct |
2 ms |
11100 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11100 KB |
Output is correct |
7 |
Correct |
2 ms |
11100 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
3 ms |
11356 KB |
Output is correct |
11 |
Correct |
3 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
1 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10888 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10896 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
10896 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
11100 KB |
Output is correct |
20 |
Correct |
2 ms |
11100 KB |
Output is correct |
21 |
Correct |
2 ms |
11156 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
8 ms |
12544 KB |
Output is correct |
24 |
Correct |
8 ms |
12612 KB |
Output is correct |
25 |
Correct |
9 ms |
12636 KB |
Output is correct |
26 |
Correct |
8 ms |
12636 KB |
Output is correct |
27 |
Correct |
8 ms |
12672 KB |
Output is correct |
28 |
Correct |
15 ms |
17756 KB |
Output is correct |
29 |
Correct |
10 ms |
13312 KB |
Output is correct |
30 |
Correct |
26 ms |
18772 KB |
Output is correct |
31 |
Correct |
27 ms |
20688 KB |
Output is correct |
32 |
Correct |
8 ms |
13008 KB |
Output is correct |
33 |
Correct |
11 ms |
14548 KB |
Output is correct |
34 |
Correct |
3 ms |
11100 KB |
Output is correct |
35 |
Correct |
2 ms |
11100 KB |
Output is correct |
36 |
Correct |
2 ms |
11012 KB |
Output is correct |
37 |
Correct |
2 ms |
11100 KB |
Output is correct |
38 |
Correct |
3 ms |
11100 KB |
Output is correct |
39 |
Correct |
2 ms |
11100 KB |
Output is correct |
40 |
Correct |
2 ms |
11100 KB |
Output is correct |
41 |
Correct |
3 ms |
11356 KB |
Output is correct |
42 |
Correct |
2 ms |
11356 KB |
Output is correct |
43 |
Correct |
3 ms |
11356 KB |
Output is correct |
44 |
Correct |
3 ms |
11356 KB |
Output is correct |
45 |
Correct |
15 ms |
12892 KB |
Output is correct |
46 |
Correct |
14 ms |
12916 KB |
Output is correct |
47 |
Correct |
9 ms |
12892 KB |
Output is correct |
48 |
Correct |
9 ms |
12972 KB |
Output is correct |
49 |
Correct |
9 ms |
12892 KB |
Output is correct |
50 |
Correct |
14 ms |
17244 KB |
Output is correct |
51 |
Correct |
17 ms |
17756 KB |
Output is correct |
52 |
Correct |
40 ms |
22916 KB |
Output is correct |
53 |
Correct |
49 ms |
22608 KB |
Output is correct |
54 |
Correct |
32 ms |
22100 KB |
Output is correct |
55 |
Correct |
41 ms |
27132 KB |
Output is correct |
56 |
Correct |
422 ms |
137556 KB |
Output is correct |
57 |
Correct |
108 ms |
44920 KB |
Output is correct |
58 |
Correct |
104 ms |
43384 KB |
Output is correct |
59 |
Correct |
97 ms |
43092 KB |
Output is correct |