#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4+10;
const int maxl = 17;
int n, m;
int a[2][maxn];
map<int, ll> dp[maxn][2];
struct SparseTable
{
int tab[2][2][maxn][maxl];
void build(void)
{
for (int i = 1; i <= n; i++)
{
if (i > 1) tab[0][0][i][0] = a[0][i-1];
if (i < n) tab[0][1][i][0] = a[0][i+1];
}
for (int i = 1; i <= m; i++)
{
if (i > 1) tab[1][0][i][0] = a[1][i-1];
if (i < m) tab[1][1][i][0] = a[1][i+1];
}
for (int j = 1; j < maxl; j++)
{
for (int i = 1; i <= n; i++)
{
if (i-(1<<j) > 0)
tab[0][0][i][j] = max(tab[0][0][i][j-1], tab[0][0][i-(1<<(j-1))][j-1]);
if (i+(1<<j) <= n)
tab[0][1][i][j] = max(tab[0][1][i][j-1], tab[0][1][i+(1<<(j-1))][j-1]);
}
for (int i = 1; i <= m; i++)
{
if (i-(1<<j) > 0)
tab[1][0][i][j] = max(tab[1][0][i][j-1], tab[1][0][i-(1<<(j-1))][j-1]);
if (i+(1<<j) <= m)
tab[1][1][i][j] = max(tab[1][1][i][j-1], tab[1][1][i+(1<<(j-1))][j-1]);
}
}
}
int get_l(int pos, int x, int q)
{
for (int i = maxl-1; i >= 0; i--)
if (pos-(1<<i) > 0 && tab[q][0][pos][i] < x)
pos -= (1<<i);
return pos-1;
}
int get_r(int pos, int x, int q)
{
for (int i = maxl-1; i >= 0; i--)
if (pos+(1<<i) <= (!q ? n : m) && tab[q][1][pos][i] < x)
pos += (1<<i);
return pos+1;
}
} ST;
ll solve(int i, int j, int q)
{
if (dp[i][q].find(j) != dp[i][q].end()) return dp[i][q][j];
ll ans;
if (!q)
{
int l = ST.get_l(j, a[0][i], 1), r = ST.get_r(j, a[0][i], 1);
if (l == 0 && r == m+1) ans = max(j-1, m-j);
else if (l == 0) ans = (1ll*(r-j) + solve(i, r, 1));
else if (r == m+1) ans = (1ll*(j-l) + solve(i, l, 1));
else ans = max(1ll*(r-j) + solve(i, r, 1), 1ll*(j-l) + solve(i, l, 1));
}
else
{
int l = ST.get_l(i, a[1][j], 0), r = ST.get_r(i, a[1][j], 0);
if (l == 0 && r == n+1) ans = max(i-1, n-i);
else if (l == 0) ans = (1ll*(r-i) + solve(r, j, 0));
else if (r == n+1) ans = (1ll*(i-l) + solve(l, j, 0));
else ans = max(1ll*(r-i) + solve(r, j, 0), 1ll*(i-l) + solve(l, j, 0));
}
return dp[i][q][j] = ans;
}
int main(void)
{
int q;
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[0][i]);
for (int i = 1; i <= m; i++)
scanf("%d", &a[1][i]);
ST.build();
while (q--)
{
int i, j;
scanf("%d %d", &i, &j);
printf("%lld\n", max(solve(i, j, 0), solve(i, j, 1)));
}
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | scanf("%d %d %d", &n, &m, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
110 | scanf("%d", &a[0][i]);
| ~~~~~^~~~~~~~~~~~~~~~
abduction2.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
113 | scanf("%d", &a[1][i]);
| ~~~~~^~~~~~~~~~~~~~~~
abduction2.cpp:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
120 | scanf("%d %d", &i, &j);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
5632 KB |
Output is correct |
13 |
Correct |
5 ms |
5632 KB |
Output is correct |
14 |
Correct |
4 ms |
5632 KB |
Output is correct |
15 |
Correct |
5 ms |
5632 KB |
Output is correct |
16 |
Correct |
4 ms |
5632 KB |
Output is correct |
17 |
Correct |
4 ms |
5632 KB |
Output is correct |
18 |
Correct |
5 ms |
5632 KB |
Output is correct |
19 |
Correct |
5 ms |
5760 KB |
Output is correct |
20 |
Correct |
5 ms |
5888 KB |
Output is correct |
21 |
Correct |
5 ms |
5888 KB |
Output is correct |
22 |
Correct |
6 ms |
6016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
5632 KB |
Output is correct |
13 |
Correct |
5 ms |
5632 KB |
Output is correct |
14 |
Correct |
4 ms |
5632 KB |
Output is correct |
15 |
Correct |
5 ms |
5632 KB |
Output is correct |
16 |
Correct |
4 ms |
5632 KB |
Output is correct |
17 |
Correct |
4 ms |
5632 KB |
Output is correct |
18 |
Correct |
5 ms |
5632 KB |
Output is correct |
19 |
Correct |
5 ms |
5760 KB |
Output is correct |
20 |
Correct |
5 ms |
5888 KB |
Output is correct |
21 |
Correct |
5 ms |
5888 KB |
Output is correct |
22 |
Correct |
6 ms |
6016 KB |
Output is correct |
23 |
Correct |
47 ms |
18716 KB |
Output is correct |
24 |
Correct |
48 ms |
18680 KB |
Output is correct |
25 |
Correct |
48 ms |
18816 KB |
Output is correct |
26 |
Correct |
47 ms |
18680 KB |
Output is correct |
27 |
Correct |
47 ms |
18680 KB |
Output is correct |
28 |
Correct |
56 ms |
23800 KB |
Output is correct |
29 |
Correct |
47 ms |
19452 KB |
Output is correct |
30 |
Correct |
73 ms |
24952 KB |
Output is correct |
31 |
Correct |
81 ms |
26744 KB |
Output is correct |
32 |
Correct |
47 ms |
18936 KB |
Output is correct |
33 |
Correct |
53 ms |
20732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5760 KB |
Output is correct |
2 |
Correct |
6 ms |
5760 KB |
Output is correct |
3 |
Correct |
5 ms |
5760 KB |
Output is correct |
4 |
Correct |
6 ms |
5760 KB |
Output is correct |
5 |
Correct |
6 ms |
5760 KB |
Output is correct |
6 |
Correct |
5 ms |
5888 KB |
Output is correct |
7 |
Correct |
6 ms |
5760 KB |
Output is correct |
8 |
Correct |
7 ms |
6144 KB |
Output is correct |
9 |
Correct |
7 ms |
6016 KB |
Output is correct |
10 |
Correct |
6 ms |
6016 KB |
Output is correct |
11 |
Correct |
6 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
3 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
5632 KB |
Output is correct |
13 |
Correct |
5 ms |
5632 KB |
Output is correct |
14 |
Correct |
4 ms |
5632 KB |
Output is correct |
15 |
Correct |
5 ms |
5632 KB |
Output is correct |
16 |
Correct |
4 ms |
5632 KB |
Output is correct |
17 |
Correct |
4 ms |
5632 KB |
Output is correct |
18 |
Correct |
5 ms |
5632 KB |
Output is correct |
19 |
Correct |
5 ms |
5760 KB |
Output is correct |
20 |
Correct |
5 ms |
5888 KB |
Output is correct |
21 |
Correct |
5 ms |
5888 KB |
Output is correct |
22 |
Correct |
6 ms |
6016 KB |
Output is correct |
23 |
Correct |
47 ms |
18716 KB |
Output is correct |
24 |
Correct |
48 ms |
18680 KB |
Output is correct |
25 |
Correct |
48 ms |
18816 KB |
Output is correct |
26 |
Correct |
47 ms |
18680 KB |
Output is correct |
27 |
Correct |
47 ms |
18680 KB |
Output is correct |
28 |
Correct |
56 ms |
23800 KB |
Output is correct |
29 |
Correct |
47 ms |
19452 KB |
Output is correct |
30 |
Correct |
73 ms |
24952 KB |
Output is correct |
31 |
Correct |
81 ms |
26744 KB |
Output is correct |
32 |
Correct |
47 ms |
18936 KB |
Output is correct |
33 |
Correct |
53 ms |
20732 KB |
Output is correct |
34 |
Correct |
5 ms |
5760 KB |
Output is correct |
35 |
Correct |
6 ms |
5760 KB |
Output is correct |
36 |
Correct |
5 ms |
5760 KB |
Output is correct |
37 |
Correct |
6 ms |
5760 KB |
Output is correct |
38 |
Correct |
6 ms |
5760 KB |
Output is correct |
39 |
Correct |
5 ms |
5888 KB |
Output is correct |
40 |
Correct |
6 ms |
5760 KB |
Output is correct |
41 |
Correct |
7 ms |
6144 KB |
Output is correct |
42 |
Correct |
7 ms |
6016 KB |
Output is correct |
43 |
Correct |
6 ms |
6016 KB |
Output is correct |
44 |
Correct |
6 ms |
6144 KB |
Output is correct |
45 |
Correct |
50 ms |
18936 KB |
Output is correct |
46 |
Correct |
50 ms |
19064 KB |
Output is correct |
47 |
Correct |
49 ms |
18936 KB |
Output is correct |
48 |
Incorrect |
52 ms |
18940 KB |
Output isn't correct |
49 |
Halted |
0 ms |
0 KB |
- |