#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e4;
int N, M, Q;
int A[MAXN+10], B[MAXN+10];
int LA[MAXN+10][20], RA[MAXN+10][20];
int LB[MAXN+10][20], RB[MAXN+10][20];
map<pair<pii, int>, ll> memo;
ll solve(int y, int x, int d)
{
if(memo.find({{y, x}, d})!=memo.end()) return memo[{{y, x}, d}];
int i, j;
ll ret=0;
int l, r, k;
if(d==0)
{
l=y-1; r=y+1; k=B[x];
for(i=16; i>=0; i--) if(LA[l][i]<=k) l=max(0, l-(1<<i));
for(i=16; i>=0; i--) if(RA[r][i]<=k) r=min(N+1, r+(1<<i));
if(l==0) ret=max(ret, (ll)y-1);
else ret=max(ret, y-l+solve(l, x, 1));
if(r==N+1) ret=max(ret, (ll)N-y);
else ret=max(ret, r-y+solve(r, x, 1));
}
else
{
l=x-1; r=x+1; k=A[y];
for(i=16; i>=0; i--) if(LB[l][i]<=k) l=max(0, l-(1<<i));
for(i=16; i>=0; i--) if(RB[r][i]<=k) r=min(M+1, r+(1<<i));
if(l==0) ret=max(ret, (ll)x-1);
else ret=max(ret, x-l+solve(y, l, 0));
if(r==M+1) ret=max(ret, (ll)M-x);
else ret=max(ret, r-x+solve(y, r, 0));
}
memo[{{y, x}, d}]=ret;
return ret;
}
int main()
{
int i, j;
scanf("%d%d%d", &N, &M, &Q);
for(i=1; i<=N; i++) scanf("%d", &A[i]);
for(i=1; i<=M; i++) scanf("%d", &B[i]);
for(i=1; i<=N; i++) LA[i][0]=RA[i][0]=A[i];
for(i=1; i<=16; i++) for(j=1; j<=N; j++) LA[j][i]=max(LA[j][i-1], LA[max(0, j-(1<<(i-1)))][i-1]);
for(i=1; i<=16; i++) for(j=1; j<=N; j++) RA[j][i]=max(RA[j][i-1], RA[min(N+1, j+(1<<(i-1)))][i-1]);
for(i=1; i<=M; i++) LB[i][0]=RB[i][0]=B[i];
for(i=1; i<=16; i++) for(j=1; j<=M; j++) LB[j][i]=max(LB[j][i-1], LB[max(0, j-(1<<(i-1)))][i-1]);
for(i=1; i<=16; i++) for(j=1; j<=M; j++) RB[j][i]=max(RB[j][i-1], RB[min(M+1, j+(1<<(i-1)))][i-1]);
while(Q--)
{
int y, x;
scanf("%d%d", &y, &x);
printf("%lld\n", max(solve(y, x, 0), solve(y, x, 1)));
}
}
Compilation message
abduction2.cpp: In function 'll solve(int, int, int)':
abduction2.cpp:21:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
abduction2.cpp: In function 'int main()':
abduction2.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &N, &M, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:58:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
abduction2.cpp:59:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=M; i++) scanf("%d", &B[i]);
~~~~~^~~~~~~~~~~~~
abduction2.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &y, &x);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
1016 KB |
Output is correct |
13 |
Correct |
6 ms |
1016 KB |
Output is correct |
14 |
Correct |
6 ms |
1016 KB |
Output is correct |
15 |
Correct |
6 ms |
1016 KB |
Output is correct |
16 |
Correct |
6 ms |
1016 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
10 ms |
1016 KB |
Output is correct |
19 |
Correct |
8 ms |
1272 KB |
Output is correct |
20 |
Correct |
9 ms |
1272 KB |
Output is correct |
21 |
Correct |
8 ms |
1276 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
1016 KB |
Output is correct |
13 |
Correct |
6 ms |
1016 KB |
Output is correct |
14 |
Correct |
6 ms |
1016 KB |
Output is correct |
15 |
Correct |
6 ms |
1016 KB |
Output is correct |
16 |
Correct |
6 ms |
1016 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
10 ms |
1016 KB |
Output is correct |
19 |
Correct |
8 ms |
1272 KB |
Output is correct |
20 |
Correct |
9 ms |
1272 KB |
Output is correct |
21 |
Correct |
8 ms |
1276 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
49 ms |
16376 KB |
Output is correct |
24 |
Correct |
51 ms |
16440 KB |
Output is correct |
25 |
Correct |
46 ms |
16376 KB |
Output is correct |
26 |
Correct |
48 ms |
16376 KB |
Output is correct |
27 |
Correct |
48 ms |
16376 KB |
Output is correct |
28 |
Correct |
55 ms |
21496 KB |
Output is correct |
29 |
Correct |
46 ms |
17144 KB |
Output is correct |
30 |
Correct |
100 ms |
22628 KB |
Output is correct |
31 |
Correct |
117 ms |
24440 KB |
Output is correct |
32 |
Correct |
60 ms |
16504 KB |
Output is correct |
33 |
Correct |
61 ms |
18296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1144 KB |
Output is correct |
2 |
Correct |
8 ms |
1144 KB |
Output is correct |
3 |
Correct |
7 ms |
1144 KB |
Output is correct |
4 |
Correct |
8 ms |
1144 KB |
Output is correct |
5 |
Correct |
8 ms |
1144 KB |
Output is correct |
6 |
Correct |
8 ms |
1272 KB |
Output is correct |
7 |
Correct |
8 ms |
1320 KB |
Output is correct |
8 |
Correct |
10 ms |
1400 KB |
Output is correct |
9 |
Correct |
11 ms |
1400 KB |
Output is correct |
10 |
Correct |
10 ms |
1400 KB |
Output is correct |
11 |
Correct |
10 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
1016 KB |
Output is correct |
13 |
Correct |
6 ms |
1016 KB |
Output is correct |
14 |
Correct |
6 ms |
1016 KB |
Output is correct |
15 |
Correct |
6 ms |
1016 KB |
Output is correct |
16 |
Correct |
6 ms |
1016 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
10 ms |
1016 KB |
Output is correct |
19 |
Correct |
8 ms |
1272 KB |
Output is correct |
20 |
Correct |
9 ms |
1272 KB |
Output is correct |
21 |
Correct |
8 ms |
1276 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
49 ms |
16376 KB |
Output is correct |
24 |
Correct |
51 ms |
16440 KB |
Output is correct |
25 |
Correct |
46 ms |
16376 KB |
Output is correct |
26 |
Correct |
48 ms |
16376 KB |
Output is correct |
27 |
Correct |
48 ms |
16376 KB |
Output is correct |
28 |
Correct |
55 ms |
21496 KB |
Output is correct |
29 |
Correct |
46 ms |
17144 KB |
Output is correct |
30 |
Correct |
100 ms |
22628 KB |
Output is correct |
31 |
Correct |
117 ms |
24440 KB |
Output is correct |
32 |
Correct |
60 ms |
16504 KB |
Output is correct |
33 |
Correct |
61 ms |
18296 KB |
Output is correct |
34 |
Correct |
9 ms |
1144 KB |
Output is correct |
35 |
Correct |
8 ms |
1144 KB |
Output is correct |
36 |
Correct |
7 ms |
1144 KB |
Output is correct |
37 |
Correct |
8 ms |
1144 KB |
Output is correct |
38 |
Correct |
8 ms |
1144 KB |
Output is correct |
39 |
Correct |
8 ms |
1272 KB |
Output is correct |
40 |
Correct |
8 ms |
1320 KB |
Output is correct |
41 |
Correct |
10 ms |
1400 KB |
Output is correct |
42 |
Correct |
11 ms |
1400 KB |
Output is correct |
43 |
Correct |
10 ms |
1400 KB |
Output is correct |
44 |
Correct |
10 ms |
1528 KB |
Output is correct |
45 |
Correct |
53 ms |
16632 KB |
Output is correct |
46 |
Correct |
51 ms |
16632 KB |
Output is correct |
47 |
Correct |
48 ms |
16632 KB |
Output is correct |
48 |
Correct |
52 ms |
16632 KB |
Output is correct |
49 |
Correct |
51 ms |
16632 KB |
Output is correct |
50 |
Correct |
62 ms |
20984 KB |
Output is correct |
51 |
Correct |
63 ms |
21496 KB |
Output is correct |
52 |
Correct |
169 ms |
26616 KB |
Output is correct |
53 |
Correct |
139 ms |
25976 KB |
Output is correct |
54 |
Correct |
140 ms |
25624 KB |
Output is correct |
55 |
Correct |
209 ms |
30968 KB |
Output is correct |
56 |
Correct |
1468 ms |
142408 KB |
Output is correct |
57 |
Correct |
384 ms |
49784 KB |
Output is correct |
58 |
Correct |
369 ms |
48120 KB |
Output is correct |
59 |
Correct |
368 ms |
47992 KB |
Output is correct |