#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int MAXN = 50001;
const int K = 16;
int sta[K + 1][MAXN];
int stb[K + 1][MAXN];
// vector<vector<int>> sta(K + 1, vector<int>(MAXN)),
// stb(K + 1, vector<int>(MAXN));
int h, w, q;
vector<ll> a, b;
int log2_floor(unsigned long long i) {
return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
}
ll max(int st[K + 1][MAXN], int l, int r) {
// int max(vector<vector<int>> &st, int l, int r) {
int j = log2_floor(r - l + 1);
return max(st[j][l], st[j][r - (1 << j) + 1]);
}
int find_next(int st[K + 1][MAXN], int x, int l, int r) {
// int find_next(vector<vector<int>> &st, int x, int l, int r) {
if (l > r) return -1;
if (max(st, l, r) < x) return -1;
int L = l, R = r;
while (L < R) {
int M = (L + R) / 2;
if (max(st, l, M) < x) {
L = M + 1;
} else {
R = M;
}
}
return L;
}
int find_prev(int st[K + 1][MAXN], int x, int l, int r) {
// int find_prev(vector<vector<int>> &st, int x, int l, int r) {
if (l > r) return -1;
if (max(st, l, r) < x) return -1;
int L = l, R = r;
while (L < R) {
int M = (L + R + 1) / 2;
if (max(st, M, r) < x) {
R = M - 1;
} else {
L = M;
}
}
return L;
}
map<tuple<int, int, int>, ll> memo;
ll _dp(int x, int y, int dir);
ll dp(int x, int y, int dir) {
// cout << "call dp(" << x << ", " << y << ", " << dir << ")" << endl;
int val = _dp(x, y, dir);
// cout << "dp(" << x << ", " << y << ", " << dir << ") = " << val << endl;
return val;
}
ll _dp(int x, int y, int dir) {
if (memo.count({x, y, dir})) return memo[{x, y, dir}];
if (dir <= 1) {
// UP/DOWN
int cur = b[y];
int pos = dir == 0 ? find_prev(sta, cur, 0, x - 1)
: find_next(sta, cur, x + 1, h - 1);
if (pos == -1) return memo[{x, y, dir}] = dir == 0 ? x : h - x - 1;
return memo[{x, y, dir}] = max(dp(pos, y, 2), dp(pos, y, 3)) + abs(x - pos);
} else {
// LEFT/RIGHT
int cur = a[x];
int pos = dir == 2 ? find_prev(stb, cur, 0, y - 1)
: find_next(stb, cur, y + 1, w - 1);
if (pos == -1) return memo[{x, y, dir}] = dir == 2 ? y : w - y - 1;
return memo[{x, y, dir}] = max(dp(x, pos, 0), dp(x, pos, 1)) + abs(y - pos);
}
}
ll dp(int x, int y) {
return max({dp(x, y, 0), dp(x, y, 1), dp(x, y, 2), dp(x, y, 3)});
}
int main() {
cin >> h >> w >> q;
a.resize(h);
b.resize(w);
for (int i = 0; i < h; i++) {
cin >> a[i];
sta[0][i] = a[i];
}
for (int i = 0; i < w; i++) {
cin >> b[i];
stb[0][i] = b[i];
}
// for (int i = 0; i < h; i++) {
// cin >> sta[0][i];
// }
// for (int i = 0; i < w; i++) {
// cin >> stb[0][i];
// }
for (int i = 1; i <= K; i++) {
for (int j = 0; j + (1 << i) <= h; j++) {
sta[i][j] = max(sta[i - 1][j], sta[i - 1][j + (1 << (i - 1))]);
}
for (int j = 0; j + (1 << i) <= w; j++) {
stb[i][j] = max(stb[i - 1][j], stb[i - 1][j + (1 << (i - 1))]);
}
}
while (q--) {
int x, y;
cin >> x >> y;
x--;
y--;
cout << dp(x, y) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
528 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
780 KB |
Output is correct |
19 |
Correct |
3 ms |
1000 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
960 KB |
Output is correct |
22 |
Correct |
6 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
528 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
780 KB |
Output is correct |
19 |
Correct |
3 ms |
1000 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
960 KB |
Output is correct |
22 |
Correct |
6 ms |
1628 KB |
Output is correct |
23 |
Correct |
26 ms |
7764 KB |
Output is correct |
24 |
Correct |
37 ms |
7900 KB |
Output is correct |
25 |
Correct |
27 ms |
7760 KB |
Output is correct |
26 |
Correct |
28 ms |
7756 KB |
Output is correct |
27 |
Correct |
26 ms |
7928 KB |
Output is correct |
28 |
Correct |
43 ms |
15588 KB |
Output is correct |
29 |
Correct |
28 ms |
9060 KB |
Output is correct |
30 |
Correct |
100 ms |
19024 KB |
Output is correct |
31 |
Correct |
124 ms |
22408 KB |
Output is correct |
32 |
Correct |
36 ms |
8528 KB |
Output is correct |
33 |
Correct |
46 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1116 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
4 ms |
1116 KB |
Output is correct |
4 |
Correct |
4 ms |
1116 KB |
Output is correct |
5 |
Correct |
4 ms |
876 KB |
Output is correct |
6 |
Correct |
3 ms |
1116 KB |
Output is correct |
7 |
Correct |
2 ms |
1024 KB |
Output is correct |
8 |
Correct |
6 ms |
1628 KB |
Output is correct |
9 |
Correct |
8 ms |
1648 KB |
Output is correct |
10 |
Correct |
6 ms |
1368 KB |
Output is correct |
11 |
Correct |
8 ms |
1796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
528 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
780 KB |
Output is correct |
19 |
Correct |
3 ms |
1000 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
960 KB |
Output is correct |
22 |
Correct |
6 ms |
1628 KB |
Output is correct |
23 |
Correct |
26 ms |
7764 KB |
Output is correct |
24 |
Correct |
37 ms |
7900 KB |
Output is correct |
25 |
Correct |
27 ms |
7760 KB |
Output is correct |
26 |
Correct |
28 ms |
7756 KB |
Output is correct |
27 |
Correct |
26 ms |
7928 KB |
Output is correct |
28 |
Correct |
43 ms |
15588 KB |
Output is correct |
29 |
Correct |
28 ms |
9060 KB |
Output is correct |
30 |
Correct |
100 ms |
19024 KB |
Output is correct |
31 |
Correct |
124 ms |
22408 KB |
Output is correct |
32 |
Correct |
36 ms |
8528 KB |
Output is correct |
33 |
Correct |
46 ms |
11356 KB |
Output is correct |
34 |
Correct |
4 ms |
1116 KB |
Output is correct |
35 |
Correct |
4 ms |
1116 KB |
Output is correct |
36 |
Correct |
4 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1116 KB |
Output is correct |
38 |
Correct |
4 ms |
876 KB |
Output is correct |
39 |
Correct |
3 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
1024 KB |
Output is correct |
41 |
Correct |
6 ms |
1628 KB |
Output is correct |
42 |
Correct |
8 ms |
1648 KB |
Output is correct |
43 |
Correct |
6 ms |
1368 KB |
Output is correct |
44 |
Correct |
8 ms |
1796 KB |
Output is correct |
45 |
Correct |
31 ms |
8472 KB |
Output is correct |
46 |
Correct |
31 ms |
8540 KB |
Output is correct |
47 |
Correct |
32 ms |
8504 KB |
Output is correct |
48 |
Correct |
32 ms |
8528 KB |
Output is correct |
49 |
Correct |
33 ms |
8528 KB |
Output is correct |
50 |
Correct |
50 ms |
15444 KB |
Output is correct |
51 |
Correct |
62 ms |
16468 KB |
Output is correct |
52 |
Correct |
173 ms |
27420 KB |
Output is correct |
53 |
Correct |
159 ms |
26704 KB |
Output is correct |
54 |
Correct |
157 ms |
25768 KB |
Output is correct |
55 |
Incorrect |
234 ms |
35152 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |