답안 #958384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958384 2024-04-05T17:03:31 Z Soumya1 유괴 2 (JOI17_abduction2) C++17
44 / 100
87 ms 18516 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<int, int> dp;
int n, m, q;
int solve(int x, int y, int dir) {
  if (x <= 0 || y <= 0 || x > n || y > m) return -1;
  int node = (x * (m + 1) + y) * 2 + dir;
  if (dp.find(node) != dp.end()) return dp[node];
  int 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 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 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 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 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 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2804 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2528 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 2 ms 2788 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 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 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2804 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2528 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 2 ms 2788 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 11 ms 7516 KB Output is correct
24 Correct 12 ms 7640 KB Output is correct
25 Correct 11 ms 7908 KB Output is correct
26 Correct 11 ms 7516 KB Output is correct
27 Correct 11 ms 7688 KB Output is correct
28 Correct 19 ms 11688 KB Output is correct
29 Correct 12 ms 8028 KB Output is correct
30 Correct 38 ms 12372 KB Output is correct
31 Correct 47 ms 13696 KB Output is correct
32 Correct 12 ms 7772 KB Output is correct
33 Correct 20 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 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 2780 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2776 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 3 ms 2908 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 3 ms 2860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 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 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2804 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2528 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2648 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 2 ms 2788 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 11 ms 7516 KB Output is correct
24 Correct 12 ms 7640 KB Output is correct
25 Correct 11 ms 7908 KB Output is correct
26 Correct 11 ms 7516 KB Output is correct
27 Correct 11 ms 7688 KB Output is correct
28 Correct 19 ms 11688 KB Output is correct
29 Correct 12 ms 8028 KB Output is correct
30 Correct 38 ms 12372 KB Output is correct
31 Correct 47 ms 13696 KB Output is correct
32 Correct 12 ms 7772 KB Output is correct
33 Correct 20 ms 9052 KB Output is correct
34 Correct 2 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 2780 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 2 ms 2776 KB Output is correct
41 Correct 3 ms 2904 KB Output is correct
42 Correct 3 ms 2908 KB Output is correct
43 Correct 3 ms 2908 KB Output is correct
44 Correct 3 ms 2860 KB Output is correct
45 Correct 13 ms 7768 KB Output is correct
46 Correct 13 ms 7768 KB Output is correct
47 Correct 13 ms 7772 KB Output is correct
48 Correct 13 ms 7740 KB Output is correct
49 Correct 13 ms 7772 KB Output is correct
50 Correct 21 ms 11096 KB Output is correct
51 Correct 22 ms 11612 KB Output is correct
52 Correct 63 ms 15276 KB Output is correct
53 Correct 63 ms 15108 KB Output is correct
54 Correct 55 ms 14796 KB Output is correct
55 Incorrect 87 ms 18516 KB Output isn't correct
56 Halted 0 ms 0 KB -