답안 #958385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
958385 2024-04-05T17:04:48 Z Soumya1 유괴 2 (JOI17_abduction2) C++17
44 / 100
1051 ms 132608 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, 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;
  int node = (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 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 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 1 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 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 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 1 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 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2904 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 3 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 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 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 1 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 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2904 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 11 ms 6492 KB Output is correct
24 Correct 13 ms 6716 KB Output is correct
25 Correct 11 ms 6492 KB Output is correct
26 Correct 11 ms 6720 KB Output is correct
27 Correct 11 ms 6492 KB Output is correct
28 Correct 20 ms 11628 KB Output is correct
29 Correct 12 ms 7260 KB Output is correct
30 Correct 39 ms 12892 KB Output is correct
31 Correct 47 ms 14636 KB Output is correct
32 Correct 12 ms 7000 KB Output is correct
33 Correct 19 ms 8672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 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 2648 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2908 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 3 ms 2908 KB Output is correct
9 Correct 4 ms 2908 KB Output is correct
10 Correct 3 ms 2908 KB Output is correct
11 Correct 3 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 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 0 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 1 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 2392 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 3 ms 2904 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 11 ms 6492 KB Output is correct
24 Correct 13 ms 6716 KB Output is correct
25 Correct 11 ms 6492 KB Output is correct
26 Correct 11 ms 6720 KB Output is correct
27 Correct 11 ms 6492 KB Output is correct
28 Correct 20 ms 11628 KB Output is correct
29 Correct 12 ms 7260 KB Output is correct
30 Correct 39 ms 12892 KB Output is correct
31 Correct 47 ms 14636 KB Output is correct
32 Correct 12 ms 7000 KB Output is correct
33 Correct 19 ms 8672 KB Output is correct
34 Correct 2 ms 2648 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 2648 KB Output is correct
38 Correct 2 ms 2652 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2652 KB Output is correct
41 Correct 3 ms 2908 KB Output is correct
42 Correct 4 ms 2908 KB Output is correct
43 Correct 3 ms 2908 KB Output is correct
44 Correct 3 ms 3164 KB Output is correct
45 Correct 18 ms 7004 KB Output is correct
46 Correct 12 ms 6976 KB Output is correct
47 Correct 13 ms 7004 KB Output is correct
48 Correct 13 ms 6764 KB Output is correct
49 Correct 12 ms 6744 KB Output is correct
50 Correct 23 ms 11100 KB Output is correct
51 Correct 23 ms 11892 KB Output is correct
52 Correct 66 ms 16976 KB Output is correct
53 Correct 69 ms 16472 KB Output is correct
54 Correct 71 ms 16028 KB Output is correct
55 Correct 98 ms 21072 KB Output is correct
56 Incorrect 1051 ms 132608 KB Output isn't correct
57 Halted 0 ms 0 KB -