#include "bits/stdc++.h"
using namespace std;
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif
using ll = long long;
int a[50000], b[50000];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int ndir[] = {1, 0, 3, 2};
int h, w, q;
map<tuple<int, int , int>, ll> m;
int jmph[100005][20];
int jmpw[100005][20];
bool in(int x, int y) {
return (1 <= x && x <= h && 1 <= y && y <= w);
}
ll dp(int x, int y, int dir) {
if (m.count({x, y, dir})) return m[{x, y, dir}];
ll &ans = m[{x, y, dir}];
switch(dir) {
case 0:
{
int loc = x;
for (int j = 19; j >= 0; j--) {
int nxt = (1 << j) - 1;
if (loc + nxt > h) continue;
if (jmph[loc][j] > b[y]) continue;
loc += (nxt + 1);
}
if (loc > h) return ans = abs(loc - 1 - x);
return ans = max(dp(loc, y, 2), dp(loc, y, 3)) + (loc - x);
break;
}
case 1:
{
int loc = x;
for (int j = 19; j >= 0; j--) {
int nxt = (1 << j) - 1;
if (loc - nxt < 1) continue;
if (jmph[loc - nxt][j] > b[y]) continue;
loc -= (nxt + 1);
}
if (loc < 1) return ans = abs(x - loc - 1);
return ans = max(dp(loc, y, 2), dp(loc, y, 3)) + (x - loc);
break;
}
case 2:
{
int loc = y;
for (int j = 19; j >= 0; j--) {
int nxt = (1 << j) - 1;
if (loc + nxt > w) continue;
if (jmpw[loc][j] > a[x]) continue;
loc += (nxt + 1);
}
if (loc > w) return ans = abs(loc - 1 - y);
return ans = max(dp(x, loc, 0), dp(x, loc, 1)) + (loc - y);
break;
}
case 3:
{
int loc = y;
for (int j = 19; j >= 0; j--) {
int nxt = (1 << j) - 1;
if (loc - nxt < 1) continue;
if (jmpw[loc - nxt][j] > a[x]) continue;
loc -= (nxt + 1);
}
if (loc < 1) return ans = abs(y - loc - 1);
return ans = max(dp(x, loc, 0), dp(x, loc, 1)) + (y - loc);
break;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
cin >> h >> w >> q;
for (int i = 1; i <= h; i++) {
cin >> a[i];
jmph[i][0] = a[i];
}
for (int i = 1; i <= w; i++) {
cin >> b[i];
jmpw[i][0] = b[i];
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= h; i++) {
if (i + (1 << (j-1)) >= 100005) break;
jmph[i][j] = max(jmph[i][j-1], jmph[i + (1 << (j-1))][j-1]);
}
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= w; i++) {
if (i + (1 << (j - 1)) >= 100005) break;
jmpw[i][j] = max(jmpw[i][j-1], jmpw[i + (1 << (j-1))][j-1]);
}
}
while (q--) {
int s, t; cin >> s >> t;
ll longest = 0;
for (int i = 0; i < 4; i++) {
int nx = s + dx[i], ny = t + dy[i];
if (in(nx, ny)) {
longest = max(longest, dp(nx, ny, i) + 1);
}
}
cout << longest << "\n";
}
// cout << longest;
}
Compilation message
abduction2.cpp: In function 'll dp(int, int, int)':
abduction2.cpp:107:1: warning: control reaches end of non-void function [-Wreturn-type]
107 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
1088 KB |
Output is correct |
20 |
Correct |
6 ms |
1236 KB |
Output is correct |
21 |
Correct |
4 ms |
1108 KB |
Output is correct |
22 |
Correct |
7 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
1088 KB |
Output is correct |
20 |
Correct |
6 ms |
1236 KB |
Output is correct |
21 |
Correct |
4 ms |
1108 KB |
Output is correct |
22 |
Correct |
7 ms |
1492 KB |
Output is correct |
23 |
Correct |
18 ms |
9472 KB |
Output is correct |
24 |
Correct |
17 ms |
9428 KB |
Output is correct |
25 |
Correct |
18 ms |
9500 KB |
Output is correct |
26 |
Correct |
17 ms |
9428 KB |
Output is correct |
27 |
Correct |
17 ms |
9444 KB |
Output is correct |
28 |
Correct |
45 ms |
16076 KB |
Output is correct |
29 |
Correct |
21 ms |
10344 KB |
Output is correct |
30 |
Correct |
112 ms |
20192 KB |
Output is correct |
31 |
Correct |
143 ms |
23340 KB |
Output is correct |
32 |
Correct |
22 ms |
9912 KB |
Output is correct |
33 |
Correct |
41 ms |
12748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
4 ms |
984 KB |
Output is correct |
3 |
Correct |
4 ms |
1108 KB |
Output is correct |
4 |
Correct |
4 ms |
1196 KB |
Output is correct |
5 |
Correct |
6 ms |
980 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
980 KB |
Output is correct |
8 |
Correct |
10 ms |
1496 KB |
Output is correct |
9 |
Correct |
8 ms |
1492 KB |
Output is correct |
10 |
Correct |
7 ms |
1496 KB |
Output is correct |
11 |
Correct |
8 ms |
1696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
704 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
1088 KB |
Output is correct |
20 |
Correct |
6 ms |
1236 KB |
Output is correct |
21 |
Correct |
4 ms |
1108 KB |
Output is correct |
22 |
Correct |
7 ms |
1492 KB |
Output is correct |
23 |
Correct |
18 ms |
9472 KB |
Output is correct |
24 |
Correct |
17 ms |
9428 KB |
Output is correct |
25 |
Correct |
18 ms |
9500 KB |
Output is correct |
26 |
Correct |
17 ms |
9428 KB |
Output is correct |
27 |
Correct |
17 ms |
9444 KB |
Output is correct |
28 |
Correct |
45 ms |
16076 KB |
Output is correct |
29 |
Correct |
21 ms |
10344 KB |
Output is correct |
30 |
Correct |
112 ms |
20192 KB |
Output is correct |
31 |
Correct |
143 ms |
23340 KB |
Output is correct |
32 |
Correct |
22 ms |
9912 KB |
Output is correct |
33 |
Correct |
41 ms |
12748 KB |
Output is correct |
34 |
Correct |
4 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
984 KB |
Output is correct |
36 |
Correct |
4 ms |
1108 KB |
Output is correct |
37 |
Correct |
4 ms |
1196 KB |
Output is correct |
38 |
Correct |
6 ms |
980 KB |
Output is correct |
39 |
Correct |
3 ms |
1108 KB |
Output is correct |
40 |
Correct |
3 ms |
980 KB |
Output is correct |
41 |
Correct |
10 ms |
1496 KB |
Output is correct |
42 |
Correct |
8 ms |
1492 KB |
Output is correct |
43 |
Correct |
7 ms |
1496 KB |
Output is correct |
44 |
Correct |
8 ms |
1696 KB |
Output is correct |
45 |
Correct |
23 ms |
9988 KB |
Output is correct |
46 |
Correct |
23 ms |
9956 KB |
Output is correct |
47 |
Correct |
23 ms |
10000 KB |
Output is correct |
48 |
Correct |
23 ms |
10064 KB |
Output is correct |
49 |
Correct |
25 ms |
9960 KB |
Output is correct |
50 |
Correct |
53 ms |
16640 KB |
Output is correct |
51 |
Correct |
53 ms |
17228 KB |
Output is correct |
52 |
Correct |
179 ms |
28728 KB |
Output is correct |
53 |
Correct |
174 ms |
27956 KB |
Output is correct |
54 |
Correct |
182 ms |
26856 KB |
Output is correct |
55 |
Correct |
268 ms |
36088 KB |
Output is correct |
56 |
Correct |
2337 ms |
259528 KB |
Output is correct |
57 |
Correct |
551 ms |
74096 KB |
Output is correct |
58 |
Correct |
532 ms |
70836 KB |
Output is correct |
59 |
Correct |
531 ms |
70260 KB |
Output is correct |