#include<bits/stdc++.h>
using namespace std;
enum {UP, LEFT, DOWN, RIGHT};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int MAXN = 65536;
const int lgMAXN = 16;
int H, W, Q;
int A[MAXN], B[MAXN];
//Am[i][j]: max of [i, i+2^j)
int Am[MAXN][lgMAXN], Bm[MAXN][lgMAXN];
int lg(int a)
{
return 31 - __builtin_clz(a);
}
void spinit()
{
for(int i=0; i<H; ++i) Am[i][0] = A[i];
for(int i=0; i<W; ++i) Bm[i][0] = B[i];
for(int j=1; j<lgMAXN; ++j)
{
for(int i=0; i<=H-(1<<j); ++i)
Am[i][j] = max(Am[i][j-1], Am[i+(1<<(j-1))][j-1]);
for(int i=0; i<=W-(1<<j); ++i)
Bm[i][j] = max(Bm[i][j-1], Bm[i+(1<<(j-1))][j-1]);
}
}
int MA(int s, int e)
{
int q = lg(e-s+1);
return max(Am[s][q], Am[e+1-(1<<q)][q]);
}
int MB(int s, int e)
{
int q = lg(e-s+1);
return max(Bm[s][q], Bm[e+1-(1<<q)][q]);
}
bool ins(int x, int y){ return 0<=x&&x<H&&0<=y&&y<W; }
map<tuple<int, int, int>, long long > M;
long long solve(int x, int y, int d)
{
auto it = M.find(make_tuple(x, y, d));
if(it != M.end()) return it->second;
if(d==UP)
{
int lo = -1; //pos
int hi = x; //imp
while(lo+1!=hi)
{
int mi = (lo+hi)>>1;
if(MA(mi, x-1) > B[y]) lo = mi;
else hi = mi;
}
//printf("%d %d UP -> %d %d\n", x, y, lo, y);
if(lo==-1) return M[{x, y, d}] = x;
else return M[{x, y, d}] = (x-lo) +
max(solve(lo, y, LEFT), solve(lo, y, RIGHT));
}
if(d==DOWN)
{
int lo = x; //imp
int hi = H; //pos
while(lo+1!=hi)
{
int mi = (lo+hi)>>1;
if(MA(x+1, mi) > B[y]) hi = mi;
else lo = mi;
}
//printf("%d %d DOWN -> %d %d\n", x, y, hi, y);
if(hi == H) return M[{x, y, d}] = H-1-x;
else return M[{x, y, d}] = (hi-x) +
max(solve(hi, y, LEFT), solve(hi, y, RIGHT));
}
if(d==LEFT)
{
int lo = -1; //pos
int hi = y; //imp
while(lo+1!=hi)
{
int mi = (lo+hi)>>1;
if(MB(mi, y-1) > A[x]) lo = mi;
else hi = mi;
}
//printf("%d %d LEFT -> %d %d\n", x, y, x, lo);
if(lo == -1) return M[{x, y, d}] = y;
else return M[{x, y, d}] = (y-lo) +
max(solve(x, lo, UP), solve(x, lo, DOWN));
}
if(d==RIGHT)
{
//puts("HELLO!");
int lo = y; //imp
int hi = W; //pos
while(lo+1!=hi)
{
int mi = (lo+hi)>>1;
if(MB(y+1, mi) > A[x]) hi = mi;
else lo = mi;
}
//printf("%d %d RIGHT -> %d %d\n", x, y, x, hi);
//puts("BYE!");
if(hi == W) return M[{x, y, d}] = W-1-y;
else return M[{x, y, d}] = (hi-y) +
max(solve(x, hi, UP), solve(x, hi, DOWN));
}
assert(false);
}
int main()
{
scanf("%d%d%d", &H, &W, &Q);
for(int i=0; i<H; ++i)
scanf("%d", A+i);
for(int i=0; i<W; ++i)
scanf("%d", B+i);
spinit();
while(Q--)
{
int S, T; scanf("%d%d", &S, &T); --S; --T;
long long ans = 0;
for(int d=0; d<4; ++d)
ans = max(ans, solve(S, T, d));
printf("%lld\n", ans);
}
}
Compilation message
abduction2.cpp: In function 'int main()':
abduction2.cpp:117:10: 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:119:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", A+i);
~~~~~^~~~~~~~~~~
abduction2.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", B+i);
~~~~~^~~~~~~~~~~
abduction2.cpp:125:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int S, T; scanf("%d%d", &S, &T); --S; --T;
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
6 ms |
992 KB |
Output is correct |
20 |
Correct |
8 ms |
1144 KB |
Output is correct |
21 |
Correct |
7 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
6 ms |
992 KB |
Output is correct |
20 |
Correct |
8 ms |
1144 KB |
Output is correct |
21 |
Correct |
7 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
31 ms |
7032 KB |
Output is correct |
24 |
Correct |
31 ms |
7032 KB |
Output is correct |
25 |
Correct |
31 ms |
7032 KB |
Output is correct |
26 |
Correct |
31 ms |
7032 KB |
Output is correct |
27 |
Correct |
31 ms |
6952 KB |
Output is correct |
28 |
Correct |
66 ms |
14800 KB |
Output is correct |
29 |
Correct |
34 ms |
8184 KB |
Output is correct |
30 |
Correct |
154 ms |
18272 KB |
Output is correct |
31 |
Correct |
196 ms |
21628 KB |
Output is correct |
32 |
Correct |
34 ms |
7544 KB |
Output is correct |
33 |
Correct |
62 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
888 KB |
Output is correct |
2 |
Correct |
7 ms |
1004 KB |
Output is correct |
3 |
Correct |
6 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
1016 KB |
Output is correct |
5 |
Correct |
6 ms |
888 KB |
Output is correct |
6 |
Correct |
5 ms |
1016 KB |
Output is correct |
7 |
Correct |
6 ms |
1016 KB |
Output is correct |
8 |
Correct |
12 ms |
1532 KB |
Output is correct |
9 |
Correct |
11 ms |
1528 KB |
Output is correct |
10 |
Correct |
11 ms |
1400 KB |
Output is correct |
11 |
Correct |
12 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
636 KB |
Output is correct |
19 |
Correct |
6 ms |
992 KB |
Output is correct |
20 |
Correct |
8 ms |
1144 KB |
Output is correct |
21 |
Correct |
7 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1528 KB |
Output is correct |
23 |
Correct |
31 ms |
7032 KB |
Output is correct |
24 |
Correct |
31 ms |
7032 KB |
Output is correct |
25 |
Correct |
31 ms |
7032 KB |
Output is correct |
26 |
Correct |
31 ms |
7032 KB |
Output is correct |
27 |
Correct |
31 ms |
6952 KB |
Output is correct |
28 |
Correct |
66 ms |
14800 KB |
Output is correct |
29 |
Correct |
34 ms |
8184 KB |
Output is correct |
30 |
Correct |
154 ms |
18272 KB |
Output is correct |
31 |
Correct |
196 ms |
21628 KB |
Output is correct |
32 |
Correct |
34 ms |
7544 KB |
Output is correct |
33 |
Correct |
62 ms |
10488 KB |
Output is correct |
34 |
Correct |
6 ms |
888 KB |
Output is correct |
35 |
Correct |
7 ms |
1004 KB |
Output is correct |
36 |
Correct |
6 ms |
888 KB |
Output is correct |
37 |
Correct |
7 ms |
1016 KB |
Output is correct |
38 |
Correct |
6 ms |
888 KB |
Output is correct |
39 |
Correct |
5 ms |
1016 KB |
Output is correct |
40 |
Correct |
6 ms |
1016 KB |
Output is correct |
41 |
Correct |
12 ms |
1532 KB |
Output is correct |
42 |
Correct |
11 ms |
1528 KB |
Output is correct |
43 |
Correct |
11 ms |
1400 KB |
Output is correct |
44 |
Correct |
12 ms |
1656 KB |
Output is correct |
45 |
Correct |
37 ms |
7588 KB |
Output is correct |
46 |
Correct |
39 ms |
7776 KB |
Output is correct |
47 |
Correct |
38 ms |
7572 KB |
Output is correct |
48 |
Correct |
70 ms |
7544 KB |
Output is correct |
49 |
Correct |
38 ms |
7544 KB |
Output is correct |
50 |
Correct |
84 ms |
14712 KB |
Output is correct |
51 |
Correct |
96 ms |
15608 KB |
Output is correct |
52 |
Correct |
268 ms |
26820 KB |
Output is correct |
53 |
Correct |
257 ms |
25848 KB |
Output is correct |
54 |
Correct |
241 ms |
24824 KB |
Output is correct |
55 |
Correct |
441 ms |
34300 KB |
Output is correct |
56 |
Correct |
3531 ms |
258208 KB |
Output is correct |
57 |
Correct |
825 ms |
72696 KB |
Output is correct |
58 |
Correct |
811 ms |
69596 KB |
Output is correct |
59 |
Correct |
829 ms |
68924 KB |
Output is correct |