#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1.1e9;
const int MAXN = 5.1e4;
const int MAXQ = 110;
int H, W;
int Q;
struct RMQ {
int N;
int A[MAXN];
int table[16][MAXN];
void init() {
A[0] = A[N+1] = INF;
N += 2;
for (int i = 0; i < N; i++) {
table[0][i] = A[i];
}
for (int i = 1; (1 << i) <= N; i++) {
for (int j = 0; j + (1 << i) <= N; j++) {
table[i][j] = max(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
}
}
}
int query(int l, int r) { // exclusive
assert(l < r);
int k = 31 - __builtin_clz(r-l);
return max(table[k][l], table[k][r - (1 << k)]);
}
int find_prev(int i, int v) {
int mi = 0;
int ma = i;
while (ma - mi > 1) {
int md = (mi + ma) / 2;
if (query(md, i) > v) {
mi = md;
} else {
ma = md;
}
}
assert(mi < i);
return mi;
}
int find_next(int i, int v) {
int mi = i;
int ma = N-1;
while (ma - mi > 1) {
int md = (mi + ma) / 2;
if (query(i+1, md+1) > v) {
ma = md;
} else {
mi = md;
}
}
assert(ma > i);
return ma;
}
} rmqs[2];
map<int, ll> dp[2][MAXN];
ll go(int dir, int r, int c) {
if (dp[dir][r].count(c)) {
return dp[dir][r][c];
}
ll ans = 0;
if (dir == 0) {
int r0 = rmqs[0].find_prev(r, rmqs[1].A[c]);
int r1 = rmqs[0].find_next(r, rmqs[1].A[c]);
ans = max(ans, r0 >= 1 ? go(1, r0, c) + r - r0 : r-1);
ans = max(ans, r1 <= H ? go(1, r1, c) + r1 - r : H-r);
} else {
int c0 = rmqs[1].find_prev(c, rmqs[0].A[r]);
int c1 = rmqs[1].find_next(c, rmqs[0].A[r]);
ans = max(ans, c0 >= 1 ? go(0, r, c0) + c - c0 : c-1);
ans = max(ans, c1 <= W ? go(0, r, c1) + c1 - c : W-c);
}
return dp[dir][r][c] = ans;
}
int main() {
cin >> H >> W >> Q;
for (int i = 1; i <= H; i++) {
cin >> rmqs[0].A[i];
}
for (int i = 1; i <= W; i++) {
cin >> rmqs[1].A[i];
}
rmqs[0].N = H;
rmqs[1].N = W;
rmqs[0].init();
rmqs[1].init();
while (Q--) {
int s, t; cin >> s >> t;
ll ans = max(go(0, s, t), go(1, s, t));
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
12 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5240 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
12 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5240 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
12 |
Correct |
10 ms |
5496 KB |
Output is correct |
13 |
Correct |
23 ms |
5496 KB |
Output is correct |
14 |
Correct |
10 ms |
5496 KB |
Output is correct |
15 |
Correct |
10 ms |
5416 KB |
Output is correct |
16 |
Correct |
10 ms |
5496 KB |
Output is correct |
17 |
Correct |
10 ms |
5496 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
19 |
Correct |
11 ms |
5624 KB |
Output is correct |
20 |
Correct |
11 ms |
5760 KB |
Output is correct |
21 |
Correct |
11 ms |
5624 KB |
Output is correct |
22 |
Correct |
13 ms |
5880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
12 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5240 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
12 |
Correct |
10 ms |
5496 KB |
Output is correct |
13 |
Correct |
23 ms |
5496 KB |
Output is correct |
14 |
Correct |
10 ms |
5496 KB |
Output is correct |
15 |
Correct |
10 ms |
5416 KB |
Output is correct |
16 |
Correct |
10 ms |
5496 KB |
Output is correct |
17 |
Correct |
10 ms |
5496 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
19 |
Correct |
11 ms |
5624 KB |
Output is correct |
20 |
Correct |
11 ms |
5760 KB |
Output is correct |
21 |
Correct |
11 ms |
5624 KB |
Output is correct |
22 |
Correct |
13 ms |
5880 KB |
Output is correct |
23 |
Correct |
87 ms |
12488 KB |
Output is correct |
24 |
Correct |
87 ms |
12320 KB |
Output is correct |
25 |
Correct |
86 ms |
12408 KB |
Output is correct |
26 |
Correct |
87 ms |
12280 KB |
Output is correct |
27 |
Correct |
87 ms |
12380 KB |
Output is correct |
28 |
Correct |
112 ms |
17528 KB |
Output is correct |
29 |
Correct |
88 ms |
13116 KB |
Output is correct |
30 |
Correct |
139 ms |
18552 KB |
Output is correct |
31 |
Correct |
156 ms |
20476 KB |
Output is correct |
32 |
Correct |
87 ms |
12600 KB |
Output is correct |
33 |
Correct |
99 ms |
14292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5624 KB |
Output is correct |
2 |
Correct |
12 ms |
5752 KB |
Output is correct |
3 |
Correct |
12 ms |
5624 KB |
Output is correct |
4 |
Correct |
12 ms |
5624 KB |
Output is correct |
5 |
Correct |
12 ms |
5624 KB |
Output is correct |
6 |
Correct |
11 ms |
5624 KB |
Output is correct |
7 |
Correct |
12 ms |
5624 KB |
Output is correct |
8 |
Correct |
13 ms |
5880 KB |
Output is correct |
9 |
Correct |
13 ms |
5880 KB |
Output is correct |
10 |
Correct |
13 ms |
5880 KB |
Output is correct |
11 |
Correct |
13 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
12 ms |
5112 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5240 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
12 |
Correct |
10 ms |
5496 KB |
Output is correct |
13 |
Correct |
23 ms |
5496 KB |
Output is correct |
14 |
Correct |
10 ms |
5496 KB |
Output is correct |
15 |
Correct |
10 ms |
5416 KB |
Output is correct |
16 |
Correct |
10 ms |
5496 KB |
Output is correct |
17 |
Correct |
10 ms |
5496 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
19 |
Correct |
11 ms |
5624 KB |
Output is correct |
20 |
Correct |
11 ms |
5760 KB |
Output is correct |
21 |
Correct |
11 ms |
5624 KB |
Output is correct |
22 |
Correct |
13 ms |
5880 KB |
Output is correct |
23 |
Correct |
87 ms |
12488 KB |
Output is correct |
24 |
Correct |
87 ms |
12320 KB |
Output is correct |
25 |
Correct |
86 ms |
12408 KB |
Output is correct |
26 |
Correct |
87 ms |
12280 KB |
Output is correct |
27 |
Correct |
87 ms |
12380 KB |
Output is correct |
28 |
Correct |
112 ms |
17528 KB |
Output is correct |
29 |
Correct |
88 ms |
13116 KB |
Output is correct |
30 |
Correct |
139 ms |
18552 KB |
Output is correct |
31 |
Correct |
156 ms |
20476 KB |
Output is correct |
32 |
Correct |
87 ms |
12600 KB |
Output is correct |
33 |
Correct |
99 ms |
14292 KB |
Output is correct |
34 |
Correct |
12 ms |
5624 KB |
Output is correct |
35 |
Correct |
12 ms |
5752 KB |
Output is correct |
36 |
Correct |
12 ms |
5624 KB |
Output is correct |
37 |
Correct |
12 ms |
5624 KB |
Output is correct |
38 |
Correct |
12 ms |
5624 KB |
Output is correct |
39 |
Correct |
11 ms |
5624 KB |
Output is correct |
40 |
Correct |
12 ms |
5624 KB |
Output is correct |
41 |
Correct |
13 ms |
5880 KB |
Output is correct |
42 |
Correct |
13 ms |
5880 KB |
Output is correct |
43 |
Correct |
13 ms |
5880 KB |
Output is correct |
44 |
Correct |
13 ms |
6008 KB |
Output is correct |
45 |
Correct |
89 ms |
12748 KB |
Output is correct |
46 |
Correct |
91 ms |
12812 KB |
Output is correct |
47 |
Correct |
92 ms |
12796 KB |
Output is correct |
48 |
Correct |
93 ms |
12648 KB |
Output is correct |
49 |
Correct |
93 ms |
12792 KB |
Output is correct |
50 |
Correct |
124 ms |
16988 KB |
Output is correct |
51 |
Correct |
135 ms |
17528 KB |
Output is correct |
52 |
Correct |
192 ms |
22752 KB |
Output is correct |
53 |
Correct |
187 ms |
22136 KB |
Output is correct |
54 |
Correct |
178 ms |
21724 KB |
Output is correct |
55 |
Correct |
224 ms |
26820 KB |
Output is correct |
56 |
Correct |
1832 ms |
137460 KB |
Output is correct |
57 |
Correct |
443 ms |
44728 KB |
Output is correct |
58 |
Correct |
435 ms |
43128 KB |
Output is correct |
59 |
Correct |
432 ms |
42880 KB |
Output is correct |