답안 #958388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958388 2024-04-05T17:11:21 Z Soumya1 유괴 2 (JOI17_abduction2) C++17
100 / 100
898 ms 132076 KB
#include <bits/stdc++.h>
using namespace std;
const int mxN = 5e4 + 5;
const int lg = 16;
int a[mxN][lg], b[mxN][lg];
map<long long, long long> dp;
int n, m, q;
long long solve(int x, int y, int dir) {
  if (x <= 0 || y <= 0 || x > n || y > m) return -1;
  long long node = (1LL * x * (m + 1) + y) * 2 + dir;
  if (dp.find(node) != dp.end()) return dp[node];
  long long ans = 0;
  if (dir == 0) {
    int ry = y + 1, ly = y - 1;
    for (int i = lg - 1; i >= 0; i--) {
      if (ry + (1 << i) - 1 <= m && b[ry][i] <= a[x][0]) ry += (1 << i);
      if (ly - (1 << i) + 1 >= 1 && b[ly - (1 << i) + 1][i] <= a[x][0]) ly -= (1 << i);
    }
    ans = max(ans, solve(x, ry, 1) + ry - y);
    ans = max(ans, solve(x, ly, 1) + y - ly);
  } else {
    int rx = x + 1, lx = x - 1;
    for (int i = lg - 1; i >= 0; i--) {
      if (rx + (1 << i) - 1 <= n && a[rx][i] <= b[y][0]) rx += (1 << i);
      if (lx - (1 << i) + 1 >= 1 && a[lx - (1 << i) + 1][i] <= b[y][0]) lx -= (1 << i);
    }
    ans = max(ans, solve(rx, y, 0) + rx - x);
    ans = max(ans, solve(lx, y, 0) + x - lx);
  }
  return dp[node] = ans;
}
void testCase() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) cin >> a[i][0];
  for (int i = 1; i <= m; i++) cin >> b[i][0];
  for (int j = 1; j < lg; j++) {
    for (int i = 1; i + (1 << j) - 1 <= n; i++) {
      a[i][j] = max(a[i][j - 1], a[i + (1 << (j - 1))][j - 1]);
    }
  }
  for (int j = 1; j < lg; j++) {
    for (int i = 1; i + (1 << j) - 1 <= m; i++) {
      b[i][j] = max(b[i][j - 1], b[i + (1 << (j - 1))][j - 1]);
    }
  }
  while (q--) {
    int x, y;
    cin >> x >> y;
    cout << max(solve(x, y, 0), solve(x, y, 1)) << "\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  testCase();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 11 ms 6492 KB Output is correct
24 Correct 11 ms 6492 KB Output is correct
25 Correct 11 ms 6488 KB Output is correct
26 Correct 12 ms 6492 KB Output is correct
27 Correct 13 ms 6492 KB Output is correct
28 Correct 20 ms 11612 KB Output is correct
29 Correct 13 ms 7260 KB Output is correct
30 Correct 44 ms 12884 KB Output is correct
31 Correct 53 ms 14640 KB Output is correct
32 Correct 13 ms 7004 KB Output is correct
33 Correct 20 ms 8688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 3108 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 3 ms 2904 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 4 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 4 ms 2908 KB Output is correct
23 Correct 11 ms 6492 KB Output is correct
24 Correct 11 ms 6492 KB Output is correct
25 Correct 11 ms 6488 KB Output is correct
26 Correct 12 ms 6492 KB Output is correct
27 Correct 13 ms 6492 KB Output is correct
28 Correct 20 ms 11612 KB Output is correct
29 Correct 13 ms 7260 KB Output is correct
30 Correct 44 ms 12884 KB Output is correct
31 Correct 53 ms 14640 KB Output is correct
32 Correct 13 ms 7004 KB Output is correct
33 Correct 20 ms 8688 KB Output is correct
34 Correct 3 ms 2652 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2652 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
39 Correct 2 ms 3108 KB Output is correct
40 Correct 2 ms 2904 KB Output is correct
41 Correct 3 ms 2908 KB Output is correct
42 Correct 3 ms 2904 KB Output is correct
43 Correct 3 ms 2908 KB Output is correct
44 Correct 4 ms 3164 KB Output is correct
45 Correct 13 ms 6972 KB Output is correct
46 Correct 13 ms 6748 KB Output is correct
47 Correct 14 ms 6812 KB Output is correct
48 Correct 13 ms 6992 KB Output is correct
49 Correct 15 ms 7000 KB Output is correct
50 Correct 29 ms 11208 KB Output is correct
51 Correct 25 ms 11868 KB Output is correct
52 Correct 71 ms 16980 KB Output is correct
53 Correct 66 ms 16296 KB Output is correct
54 Correct 84 ms 16168 KB Output is correct
55 Correct 100 ms 21072 KB Output is correct
56 Correct 898 ms 132076 KB Output is correct
57 Correct 223 ms 39760 KB Output is correct
58 Correct 221 ms 38588 KB Output is correct
59 Correct 225 ms 37968 KB Output is correct