#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
int h, w, q, vh[50002], vw[50002];
int rmqL[20][50002], rmqC[20][50002];
int aa, bb;
map<pair<pair<int, int>, int>, ll> mp;
ll solve(int L, int C, int dir)
{
if(mp.find({{L, C}, dir}) != mp.end())
return mp[{{L, C}, dir}];
ll ans = 0;
if(dir == 0)
{
int cd = L;
int init = vw[C];
for(int i = aa; i >= 0; --i)
{
if(cd - (1<<i) + 1 >= 1)
{
if(rmqL[i][cd - (1<<i) + 1] > init);
else
cd -= (1<<i);
}
}
if(!(cd == 0 || cd > h))
{
int dif = L - cd + 1;
if(C > 1)
ans = max(ans, dif + solve(cd, C - 1, 3));
if(C < w)
ans = max(ans, dif + solve(cd, C + 1, 1));
}
else
ans = max(ans, 1LL * L - 1);
// cout << L << " " << C << " " << dir << " " << cd << " " << ans << '\n';
}
if(dir == 2)
{
int cd = L;
int init = vw[C];
for(int i = aa; i >= 0; --i)
{
if(cd + (1<<i) - 1 <= h)
{
if(rmqL[i][cd] > init);
else
cd += (1<<i);
}
}
if(!(cd == 0 || cd > h))
{
int dif = cd - L + 1;
if(C > 1)
ans = max(ans, dif + solve(cd, C - 1, 3));
if(C < w)
ans = max(ans, dif + solve(cd, C + 1, 1));
}
else
ans = max(ans, 1LL * h - L);
// cout << L << " " << C << " " << dir << " " << cd << " " << ans << '\n';
}
if(dir == 1)
{
int cd = C;
int init = vh[L];
for(int i = bb; i >= 0; --i)
{
if(cd + (1<<i) - 1 <= w)
{
if(rmqC[i][cd] > init);
else
cd += (1<<i);
}
}
if(!(cd == 0 || cd > w))
{
int dif = cd - C + 1;
if(L > 1)
ans = max(ans, dif + solve(L - 1, cd, 0));
if(L < h)
ans = max(ans, dif + solve(L + 1, cd, 2));
}
else
ans = max(ans, 1LL * w - C);
// cout << L << " " << C << " " << dir << " " << cd << " " << ans << '\n';
}
if(dir == 3)
{
int cd = C;
int init = vh[L];
for(int i = bb; i >= 0; --i)
{
if(cd - (1<<i) + 1 >= 1)
{
if(rmqC[i][cd - (1<<i) + 1] > init);
else
cd -= (1<<i);
}
}
if(!(cd == 0 || cd > w))
{
int dif = C - cd + 1;
if(L > 1)
ans = max(ans, dif + solve(L - 1, cd, 0));
if(L < h)
ans = max(ans, dif + solve(L + 1, cd, 2));
}
else
ans = max(ans, 1LL * C - 1);
// cout << L << " " << C << " " << dir << " " << cd << " " << ans << '\n';
}
mp[{{L, C}, dir}] = ans;
return ans;
}
int main()
{
cin >> h >> w >> q;
for(int i = 1; i <= h; ++i)
cin >> vh[i], rmqL[0][i] = vh[i];
for(int i = 1; i <= w; ++i)
cin >> vw[i], rmqC[0][i] = vw[i];
for(int i = 1; (1<<i) <= h; ++i)
{
aa = i;
for(int j = 1; j + (1<<i) - 1 <= h; ++j)
rmqL[i][j] = max(rmqL[i-1][j], rmqL[i-1][j + (1<<(i-1))]);
}
for(int i = 1; (1<<i) <= w; ++i)
{
bb = i;
for(int j = 1; j + (1<<i) - 1 <= w; ++j)
rmqC[i][j] = max(rmqC[i-1][j], rmqC[i-1][j + (1<<(i-1))]);
}
for(int i = 1; i <= q; ++i)
{
int L, C;
cin >> L >> C;
ll ans = 0;
if(L > 1)
ans = max(ans, 1 + solve(L-1, C, 0));
if(C < w)
ans = max(ans, 1 + solve(L, C+1, 1));
if(L < h)
ans = max(ans, 1 + solve(L+1, C, 2));
if(C > 1)
ans = max(ans, 1 + solve(L, C-1, 3));
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
1 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
632 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
5 ms |
656 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
10 ms |
1148 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1400 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 |
1 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
632 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
5 ms |
656 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
10 ms |
1148 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1400 KB |
Output is correct |
23 |
Correct |
80 ms |
6612 KB |
Output is correct |
24 |
Correct |
80 ms |
6636 KB |
Output is correct |
25 |
Correct |
86 ms |
6576 KB |
Output is correct |
26 |
Correct |
84 ms |
6520 KB |
Output is correct |
27 |
Correct |
81 ms |
6620 KB |
Output is correct |
28 |
Correct |
122 ms |
13816 KB |
Output is correct |
29 |
Correct |
85 ms |
7560 KB |
Output is correct |
30 |
Correct |
197 ms |
17656 KB |
Output is correct |
31 |
Correct |
240 ms |
20728 KB |
Output is correct |
32 |
Correct |
85 ms |
7032 KB |
Output is correct |
33 |
Correct |
123 ms |
9952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
888 KB |
Output is correct |
2 |
Correct |
9 ms |
888 KB |
Output is correct |
3 |
Correct |
10 ms |
888 KB |
Output is correct |
4 |
Correct |
10 ms |
1016 KB |
Output is correct |
5 |
Correct |
9 ms |
1016 KB |
Output is correct |
6 |
Correct |
7 ms |
1020 KB |
Output is correct |
7 |
Correct |
8 ms |
1016 KB |
Output is correct |
8 |
Correct |
14 ms |
1528 KB |
Output is correct |
9 |
Correct |
14 ms |
1400 KB |
Output is correct |
10 |
Correct |
13 ms |
1400 KB |
Output is correct |
11 |
Correct |
15 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 |
1 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
5 ms |
632 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
5 ms |
656 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
10 ms |
1148 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1400 KB |
Output is correct |
23 |
Correct |
80 ms |
6612 KB |
Output is correct |
24 |
Correct |
80 ms |
6636 KB |
Output is correct |
25 |
Correct |
86 ms |
6576 KB |
Output is correct |
26 |
Correct |
84 ms |
6520 KB |
Output is correct |
27 |
Correct |
81 ms |
6620 KB |
Output is correct |
28 |
Correct |
122 ms |
13816 KB |
Output is correct |
29 |
Correct |
85 ms |
7560 KB |
Output is correct |
30 |
Correct |
197 ms |
17656 KB |
Output is correct |
31 |
Correct |
240 ms |
20728 KB |
Output is correct |
32 |
Correct |
85 ms |
7032 KB |
Output is correct |
33 |
Correct |
123 ms |
9952 KB |
Output is correct |
34 |
Correct |
10 ms |
888 KB |
Output is correct |
35 |
Correct |
9 ms |
888 KB |
Output is correct |
36 |
Correct |
10 ms |
888 KB |
Output is correct |
37 |
Correct |
10 ms |
1016 KB |
Output is correct |
38 |
Correct |
9 ms |
1016 KB |
Output is correct |
39 |
Correct |
7 ms |
1020 KB |
Output is correct |
40 |
Correct |
8 ms |
1016 KB |
Output is correct |
41 |
Correct |
14 ms |
1528 KB |
Output is correct |
42 |
Correct |
14 ms |
1400 KB |
Output is correct |
43 |
Correct |
13 ms |
1400 KB |
Output is correct |
44 |
Correct |
15 ms |
1656 KB |
Output is correct |
45 |
Correct |
90 ms |
7192 KB |
Output is correct |
46 |
Correct |
97 ms |
7016 KB |
Output is correct |
47 |
Correct |
91 ms |
7136 KB |
Output is correct |
48 |
Correct |
89 ms |
7160 KB |
Output is correct |
49 |
Correct |
90 ms |
7136 KB |
Output is correct |
50 |
Correct |
130 ms |
13908 KB |
Output is correct |
51 |
Correct |
137 ms |
14712 KB |
Output is correct |
52 |
Correct |
318 ms |
26028 KB |
Output is correct |
53 |
Correct |
310 ms |
25264 KB |
Output is correct |
54 |
Correct |
291 ms |
24308 KB |
Output is correct |
55 |
Correct |
540 ms |
33596 KB |
Output is correct |
56 |
Correct |
4516 ms |
256984 KB |
Output is correct |
57 |
Correct |
982 ms |
71524 KB |
Output is correct |
58 |
Correct |
928 ms |
68356 KB |
Output is correct |
59 |
Correct |
926 ms |
67772 KB |
Output is correct |