#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=(1<<30);
struct Seg {
int ar[(1<<17)];
void upd(int i, int s, int e, int t, int v) {
if (s==e) { ar[i]=v; return ; }
int md=(s+e)/2;
if (t<=md) upd(i*2, s, md, t, v);
else upd(i*2+1, md+1, e, t, v);
ar[i]=max(ar[i*2], ar[i*2+1]);
}
int lo(int i, int s, int e, int t, int v) {
if (t<=s||ar[i]<v) return INF;
if (s==e) return s;
int md=(s+e)/2, ret;
ret=lo(i*2+1, md+1, e, t, v);
if (ret<t) return ret;
return lo(i*2, s, md, t, v);
}
int hi(int i, int s, int e, int t, int v) {
if (e<=t||ar[i]<v) return -1;
if (s==e) return s;
int md=(s+e)/2, ret;
ret=hi(i*2, s, md, t, v);
if (ret>t) return ret;
return hi(i*2+1, md+1, e, t, v);
}
}S1, S2;
ll sol1(int u, int v);
ll sol2(int u, int v);
int H, W, Q, A[50010], B[50010];
int main() {
scanf("%d %d %d", &H, &W, &Q);
for (int i=1; i<=H; i++) scanf("%d", &A[i]), S1.upd(1, 1, H, i, A[i]);
for (int i=1; i<=W; i++) scanf("%d", &B[i]), S2.upd(1, 1, W, i, B[i]);
while (Q--) {
int u, v; scanf("%d %d", &u, &v);
printf("%lld\n", max(sol1(u, v), sol2(u, v)));
}
return 0;
}
ll hsh(int x, int y) { return x*50001ll+y; }
map<ll, ll> M1, M2;
ll sol1(int u, int v) {
if (M1[hsh(u, v)]) return M1[hsh(u, v)];
ll a1, a2; int im;
im=S1.lo(1, 1, H, u, B[v]);
a1=(im<u?sol2(im, v)+u-im:u-1);
im=S1.hi(1, 1, H, u, B[v]);
a2=(im>u?sol2(im, v)+im-u:H-u);
return M1[hsh(u, v)]=max(a1, a2);
}
ll sol2(int u, int v) {
if (M2[hsh(u, v)]) return M2[hsh(u, v)];
ll a1, a2; int im;
im=S2.lo(1, 1, W, v, A[u]);
a1=(im<v?sol1(u, im)+v-im:v-1);
im=S2.hi(1, 1, W, v, A[u]);
a2=(im>v?sol1(u, im)+im-v:W-v);
return M2[hsh(u, v)]=max(a1, a2);
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &H, &W, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:40:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1; i<=H; i++) scanf("%d", &A[i]), S1.upd(1, 1, H, i, A[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:41:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1; i<=W; i++) scanf("%d", &B[i]), S2.upd(1, 1, W, i, B[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:43:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v; scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# |
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 |
380 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 |
5 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 |
380 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 |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
500 KB |
Output is correct |
19 |
Correct |
8 ms |
632 KB |
Output is correct |
20 |
Correct |
9 ms |
760 KB |
Output is correct |
21 |
Correct |
8 ms |
632 KB |
Output is correct |
22 |
Correct |
10 ms |
892 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 |
380 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 |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
500 KB |
Output is correct |
19 |
Correct |
8 ms |
632 KB |
Output is correct |
20 |
Correct |
9 ms |
760 KB |
Output is correct |
21 |
Correct |
8 ms |
632 KB |
Output is correct |
22 |
Correct |
10 ms |
892 KB |
Output is correct |
23 |
Correct |
36 ms |
2808 KB |
Output is correct |
24 |
Correct |
38 ms |
2680 KB |
Output is correct |
25 |
Correct |
38 ms |
2656 KB |
Output is correct |
26 |
Correct |
36 ms |
2680 KB |
Output is correct |
27 |
Correct |
41 ms |
2680 KB |
Output is correct |
28 |
Correct |
55 ms |
8828 KB |
Output is correct |
29 |
Correct |
37 ms |
3576 KB |
Output is correct |
30 |
Correct |
109 ms |
9464 KB |
Output is correct |
31 |
Correct |
132 ms |
11384 KB |
Output is correct |
32 |
Correct |
37 ms |
3064 KB |
Output is correct |
33 |
Correct |
54 ms |
4856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
8 ms |
632 KB |
Output is correct |
3 |
Correct |
8 ms |
632 KB |
Output is correct |
4 |
Correct |
8 ms |
632 KB |
Output is correct |
5 |
Correct |
8 ms |
632 KB |
Output is correct |
6 |
Correct |
8 ms |
760 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
11 ms |
888 KB |
Output is correct |
9 |
Correct |
11 ms |
888 KB |
Output is correct |
10 |
Correct |
10 ms |
888 KB |
Output is correct |
11 |
Correct |
12 ms |
1144 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 |
380 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 |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
6 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
500 KB |
Output is correct |
19 |
Correct |
8 ms |
632 KB |
Output is correct |
20 |
Correct |
9 ms |
760 KB |
Output is correct |
21 |
Correct |
8 ms |
632 KB |
Output is correct |
22 |
Correct |
10 ms |
892 KB |
Output is correct |
23 |
Correct |
36 ms |
2808 KB |
Output is correct |
24 |
Correct |
38 ms |
2680 KB |
Output is correct |
25 |
Correct |
38 ms |
2656 KB |
Output is correct |
26 |
Correct |
36 ms |
2680 KB |
Output is correct |
27 |
Correct |
41 ms |
2680 KB |
Output is correct |
28 |
Correct |
55 ms |
8828 KB |
Output is correct |
29 |
Correct |
37 ms |
3576 KB |
Output is correct |
30 |
Correct |
109 ms |
9464 KB |
Output is correct |
31 |
Correct |
132 ms |
11384 KB |
Output is correct |
32 |
Correct |
37 ms |
3064 KB |
Output is correct |
33 |
Correct |
54 ms |
4856 KB |
Output is correct |
34 |
Correct |
10 ms |
632 KB |
Output is correct |
35 |
Correct |
8 ms |
632 KB |
Output is correct |
36 |
Correct |
8 ms |
632 KB |
Output is correct |
37 |
Correct |
8 ms |
632 KB |
Output is correct |
38 |
Correct |
8 ms |
632 KB |
Output is correct |
39 |
Correct |
8 ms |
760 KB |
Output is correct |
40 |
Correct |
7 ms |
632 KB |
Output is correct |
41 |
Correct |
11 ms |
888 KB |
Output is correct |
42 |
Correct |
11 ms |
888 KB |
Output is correct |
43 |
Correct |
10 ms |
888 KB |
Output is correct |
44 |
Correct |
12 ms |
1144 KB |
Output is correct |
45 |
Correct |
41 ms |
2940 KB |
Output is correct |
46 |
Correct |
40 ms |
3064 KB |
Output is correct |
47 |
Correct |
40 ms |
3064 KB |
Output is correct |
48 |
Correct |
46 ms |
2936 KB |
Output is correct |
49 |
Correct |
41 ms |
2936 KB |
Output is correct |
50 |
Correct |
60 ms |
7928 KB |
Output is correct |
51 |
Correct |
63 ms |
8696 KB |
Output is correct |
52 |
Correct |
163 ms |
13432 KB |
Output is correct |
53 |
Correct |
163 ms |
12920 KB |
Output is correct |
54 |
Correct |
159 ms |
12708 KB |
Output is correct |
55 |
Correct |
225 ms |
17912 KB |
Output is correct |
56 |
Correct |
1919 ms |
127992 KB |
Output is correct |
57 |
Correct |
461 ms |
35320 KB |
Output is correct |
58 |
Correct |
425 ms |
33528 KB |
Output is correct |
59 |
Correct |
441 ms |
33368 KB |
Output is correct |