#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<int, pair<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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 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 |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 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 |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
5 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 |
632 KB |
Output is correct |
19 |
Correct |
9 ms |
1016 KB |
Output is correct |
20 |
Correct |
11 ms |
1380 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 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 |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
5 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 |
632 KB |
Output is correct |
19 |
Correct |
9 ms |
1016 KB |
Output is correct |
20 |
Correct |
11 ms |
1380 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1528 KB |
Output is correct |
23 |
Correct |
81 ms |
7716 KB |
Output is correct |
24 |
Correct |
82 ms |
7568 KB |
Output is correct |
25 |
Correct |
83 ms |
7544 KB |
Output is correct |
26 |
Correct |
79 ms |
7548 KB |
Output is correct |
27 |
Correct |
85 ms |
7536 KB |
Output is correct |
28 |
Correct |
123 ms |
14684 KB |
Output is correct |
29 |
Correct |
86 ms |
8568 KB |
Output is correct |
30 |
Correct |
224 ms |
18552 KB |
Output is correct |
31 |
Correct |
271 ms |
21752 KB |
Output is correct |
32 |
Correct |
89 ms |
8144 KB |
Output is correct |
33 |
Correct |
126 ms |
10872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1016 KB |
Output is correct |
2 |
Correct |
9 ms |
1016 KB |
Output is correct |
3 |
Correct |
9 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
1016 KB |
Output is correct |
5 |
Correct |
9 ms |
1016 KB |
Output is correct |
6 |
Correct |
8 ms |
1060 KB |
Output is correct |
7 |
Correct |
8 ms |
1016 KB |
Output is correct |
8 |
Correct |
15 ms |
1528 KB |
Output is correct |
9 |
Correct |
15 ms |
1528 KB |
Output is correct |
10 |
Correct |
14 ms |
1500 KB |
Output is correct |
11 |
Correct |
16 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 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 |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
5 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 |
632 KB |
Output is correct |
19 |
Correct |
9 ms |
1016 KB |
Output is correct |
20 |
Correct |
11 ms |
1380 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
13 ms |
1528 KB |
Output is correct |
23 |
Correct |
81 ms |
7716 KB |
Output is correct |
24 |
Correct |
82 ms |
7568 KB |
Output is correct |
25 |
Correct |
83 ms |
7544 KB |
Output is correct |
26 |
Correct |
79 ms |
7548 KB |
Output is correct |
27 |
Correct |
85 ms |
7536 KB |
Output is correct |
28 |
Correct |
123 ms |
14684 KB |
Output is correct |
29 |
Correct |
86 ms |
8568 KB |
Output is correct |
30 |
Correct |
224 ms |
18552 KB |
Output is correct |
31 |
Correct |
271 ms |
21752 KB |
Output is correct |
32 |
Correct |
89 ms |
8144 KB |
Output is correct |
33 |
Correct |
126 ms |
10872 KB |
Output is correct |
34 |
Correct |
8 ms |
1016 KB |
Output is correct |
35 |
Correct |
9 ms |
1016 KB |
Output is correct |
36 |
Correct |
9 ms |
1016 KB |
Output is correct |
37 |
Correct |
9 ms |
1016 KB |
Output is correct |
38 |
Correct |
9 ms |
1016 KB |
Output is correct |
39 |
Correct |
8 ms |
1060 KB |
Output is correct |
40 |
Correct |
8 ms |
1016 KB |
Output is correct |
41 |
Correct |
15 ms |
1528 KB |
Output is correct |
42 |
Correct |
15 ms |
1528 KB |
Output is correct |
43 |
Correct |
14 ms |
1500 KB |
Output is correct |
44 |
Correct |
16 ms |
1784 KB |
Output is correct |
45 |
Correct |
90 ms |
8152 KB |
Output is correct |
46 |
Correct |
90 ms |
8056 KB |
Output is correct |
47 |
Correct |
97 ms |
8056 KB |
Output is correct |
48 |
Correct |
91 ms |
8184 KB |
Output is correct |
49 |
Correct |
90 ms |
8056 KB |
Output is correct |
50 |
Correct |
134 ms |
14968 KB |
Output is correct |
51 |
Correct |
140 ms |
15736 KB |
Output is correct |
52 |
Correct |
368 ms |
27044 KB |
Output is correct |
53 |
Correct |
357 ms |
26240 KB |
Output is correct |
54 |
Correct |
336 ms |
25208 KB |
Output is correct |
55 |
Correct |
661 ms |
34512 KB |
Output is correct |
56 |
Correct |
4568 ms |
257676 KB |
Output is correct |
57 |
Correct |
1026 ms |
72224 KB |
Output is correct |
58 |
Correct |
985 ms |
68912 KB |
Output is correct |
59 |
Correct |
1062 ms |
68472 KB |
Output is correct |