Submission #288285

# Submission time Handle Problem Language Result Execution time Memory
288285 2020-09-01T11:36:32 Z rama_pang Abduction 2 (JOI17_abduction2) C++14
100 / 100
656 ms 5372 KB
#include <bits/stdc++.h>
using namespace std;

// Speed up looking next line change with sparse table,
// and memoize based on [left_end, right_end] of each
// street. State is O(H + W), since for every line x and
// the section we enter it, and from that line we go left
// or right to change to another line y and z, then if we 
// change the path and instead goes through a line that passes
// through y and z, we will never stop at x. Thus for each
// line only one "region" matters.
// Time: O(Q * (H + W))

using lint = long long;
const int MAXN = 100005;

int H, W, Q;
array<int, 3> A[MAXN];

int st[MAXN];
int et[MAXN];
lint dpl[MAXN];
lint dpr[MAXN];
int revpos[MAXN][2];

lint Solve(int X, int Y, int mask) {
  lint res = 0;
  int xl = 0, xr = H - 1;
  int yl = 0, yr = W - 1;
  memset(dpl, 0, sizeof(dpl));
  memset(dpr, 0, sizeof(dpr));
  for (int i = 0; i < H + W; i++) {
    if (A[i][2] == 0) {
      st[i] = yl, et[i] = yr;
      if (revpos[st[i]][A[i][2] ^ 1] < i) {
        int p = revpos[st[i]][A[i][2] ^ 1];
        dpl[i] = max(dpl[p] + abs(st[p] - A[i][1]), dpr[p] + abs(et[p] - A[i][1]));
      }
      if (revpos[et[i]][A[i][2] ^ 1] < i) {
        int p = revpos[et[i]][A[i][2] ^ 1];
        dpr[i] = max(dpl[p] + abs(st[p] - A[i][1]), dpr[p] + abs(et[p] - A[i][1]));
      }
      if (A[i][1] + !!(mask & 1) <= X) {
        xl = max(xl, A[i][1]);
      } else {
        xr = min(xr, A[i][1]);
      }
    } else if (A[i][2] == 1) {
      st[i] = xl, et[i] = xr;
      if (revpos[st[i]][A[i][2] ^ 1] < i) {
        int p = revpos[st[i]][A[i][2] ^ 1];
        dpl[i] = max(dpl[p] + abs(st[p] - A[i][1]), dpr[p] + abs(et[p] - A[i][1]));
      }
      if (revpos[et[i]][A[i][2] ^ 1] < i) {
        int p = revpos[et[i]][A[i][2] ^ 1];
        dpr[i] = max(dpl[p] + abs(st[p] - A[i][1]), dpr[p] + abs(et[p] - A[i][1]));
      }
      if (A[i][1] + !!(mask & 2) <= Y) {
        yl = max(yl, A[i][1]);
      } else {
        yr = min(yr, A[i][1]);
      }
    }
    if (A[i][1] == X && A[i][2] == 0) {
      res = max(res, dpl[i] + abs(Y - st[i]));
      res = max(res, dpr[i] + abs(Y - et[i]));
    }
    if (A[i][1] == Y && A[i][2] == 1) {
      res = max(res, dpl[i] + abs(X - st[i]));
      res = max(res, dpr[i] + abs(X - et[i]));
    }
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> H >> W >> Q;
  for (int i = 0; i < H; i++) {
    cin >> A[i][0];
    A[i][1] = i;
    A[i][2] = 0;
  }
  for (int i = 0; i < W; i++) {
    cin >> A[i + H][0];
    A[i + H][1] = i;
    A[i + H][2] = 1;
  }
  sort(begin(A), end(A));
  reverse(begin(A), end(A));
  for (int i = 0; i < H + W; i++) {
    revpos[A[i][1]][A[i][2]] = i;
  }
  for (int i = 0; i < Q; i++) {
    int S, T;
    cin >> S >> T;
    S--, T--;
    cout << max({Solve(S, T, 0), Solve(S, T, 1), 
                 Solve(S, T, 2), Solve(S, T, 3)}) << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3072 KB Output is correct
2 Correct 12 ms 3072 KB Output is correct
3 Correct 12 ms 3072 KB Output is correct
4 Correct 12 ms 3072 KB Output is correct
5 Correct 12 ms 3072 KB Output is correct
6 Correct 11 ms 3072 KB Output is correct
7 Correct 12 ms 3072 KB Output is correct
8 Correct 12 ms 3072 KB Output is correct
9 Correct 11 ms 3072 KB Output is correct
10 Correct 11 ms 3072 KB Output is correct
11 Correct 11 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3072 KB Output is correct
2 Correct 12 ms 3072 KB Output is correct
3 Correct 12 ms 3072 KB Output is correct
4 Correct 12 ms 3072 KB Output is correct
5 Correct 12 ms 3072 KB Output is correct
6 Correct 11 ms 3072 KB Output is correct
7 Correct 12 ms 3072 KB Output is correct
8 Correct 12 ms 3072 KB Output is correct
9 Correct 11 ms 3072 KB Output is correct
10 Correct 11 ms 3072 KB Output is correct
11 Correct 11 ms 3072 KB Output is correct
12 Correct 16 ms 3200 KB Output is correct
13 Correct 15 ms 3200 KB Output is correct
14 Correct 12 ms 3200 KB Output is correct
15 Correct 13 ms 3200 KB Output is correct
16 Correct 12 ms 3200 KB Output is correct
17 Correct 14 ms 3200 KB Output is correct
18 Correct 12 ms 3200 KB Output is correct
19 Correct 12 ms 3200 KB Output is correct
20 Correct 12 ms 3200 KB Output is correct
21 Correct 12 ms 3200 KB Output is correct
22 Correct 12 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3072 KB Output is correct
2 Correct 12 ms 3072 KB Output is correct
3 Correct 12 ms 3072 KB Output is correct
4 Correct 12 ms 3072 KB Output is correct
5 Correct 12 ms 3072 KB Output is correct
6 Correct 11 ms 3072 KB Output is correct
7 Correct 12 ms 3072 KB Output is correct
8 Correct 12 ms 3072 KB Output is correct
9 Correct 11 ms 3072 KB Output is correct
10 Correct 11 ms 3072 KB Output is correct
11 Correct 11 ms 3072 KB Output is correct
12 Correct 16 ms 3200 KB Output is correct
13 Correct 15 ms 3200 KB Output is correct
14 Correct 12 ms 3200 KB Output is correct
15 Correct 13 ms 3200 KB Output is correct
16 Correct 12 ms 3200 KB Output is correct
17 Correct 14 ms 3200 KB Output is correct
18 Correct 12 ms 3200 KB Output is correct
19 Correct 12 ms 3200 KB Output is correct
20 Correct 12 ms 3200 KB Output is correct
21 Correct 12 ms 3200 KB Output is correct
22 Correct 12 ms 3200 KB Output is correct
23 Correct 32 ms 5240 KB Output is correct
24 Correct 33 ms 5248 KB Output is correct
25 Correct 32 ms 5256 KB Output is correct
26 Correct 32 ms 5368 KB Output is correct
27 Correct 32 ms 5248 KB Output is correct
28 Correct 32 ms 5248 KB Output is correct
29 Correct 33 ms 5240 KB Output is correct
30 Correct 27 ms 5248 KB Output is correct
31 Correct 27 ms 5240 KB Output is correct
32 Correct 27 ms 5248 KB Output is correct
33 Correct 29 ms 5240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 3232 KB Output is correct
2 Correct 57 ms 3200 KB Output is correct
3 Correct 65 ms 3200 KB Output is correct
4 Correct 62 ms 3320 KB Output is correct
5 Correct 56 ms 3200 KB Output is correct
6 Correct 51 ms 3200 KB Output is correct
7 Correct 51 ms 3200 KB Output is correct
8 Correct 53 ms 3200 KB Output is correct
9 Correct 52 ms 3200 KB Output is correct
10 Correct 52 ms 3200 KB Output is correct
11 Correct 48 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3072 KB Output is correct
2 Correct 12 ms 3072 KB Output is correct
3 Correct 12 ms 3072 KB Output is correct
4 Correct 12 ms 3072 KB Output is correct
5 Correct 12 ms 3072 KB Output is correct
6 Correct 11 ms 3072 KB Output is correct
7 Correct 12 ms 3072 KB Output is correct
8 Correct 12 ms 3072 KB Output is correct
9 Correct 11 ms 3072 KB Output is correct
10 Correct 11 ms 3072 KB Output is correct
11 Correct 11 ms 3072 KB Output is correct
12 Correct 16 ms 3200 KB Output is correct
13 Correct 15 ms 3200 KB Output is correct
14 Correct 12 ms 3200 KB Output is correct
15 Correct 13 ms 3200 KB Output is correct
16 Correct 12 ms 3200 KB Output is correct
17 Correct 14 ms 3200 KB Output is correct
18 Correct 12 ms 3200 KB Output is correct
19 Correct 12 ms 3200 KB Output is correct
20 Correct 12 ms 3200 KB Output is correct
21 Correct 12 ms 3200 KB Output is correct
22 Correct 12 ms 3200 KB Output is correct
23 Correct 32 ms 5240 KB Output is correct
24 Correct 33 ms 5248 KB Output is correct
25 Correct 32 ms 5256 KB Output is correct
26 Correct 32 ms 5368 KB Output is correct
27 Correct 32 ms 5248 KB Output is correct
28 Correct 32 ms 5248 KB Output is correct
29 Correct 33 ms 5240 KB Output is correct
30 Correct 27 ms 5248 KB Output is correct
31 Correct 27 ms 5240 KB Output is correct
32 Correct 27 ms 5248 KB Output is correct
33 Correct 29 ms 5240 KB Output is correct
34 Correct 57 ms 3232 KB Output is correct
35 Correct 57 ms 3200 KB Output is correct
36 Correct 65 ms 3200 KB Output is correct
37 Correct 62 ms 3320 KB Output is correct
38 Correct 56 ms 3200 KB Output is correct
39 Correct 51 ms 3200 KB Output is correct
40 Correct 51 ms 3200 KB Output is correct
41 Correct 53 ms 3200 KB Output is correct
42 Correct 52 ms 3200 KB Output is correct
43 Correct 52 ms 3200 KB Output is correct
44 Correct 48 ms 3200 KB Output is correct
45 Correct 631 ms 5264 KB Output is correct
46 Correct 636 ms 5264 KB Output is correct
47 Correct 622 ms 5240 KB Output is correct
48 Correct 656 ms 5264 KB Output is correct
49 Correct 629 ms 5240 KB Output is correct
50 Correct 495 ms 5240 KB Output is correct
51 Correct 488 ms 5372 KB Output is correct
52 Correct 585 ms 5248 KB Output is correct
53 Correct 522 ms 5240 KB Output is correct
54 Correct 531 ms 5240 KB Output is correct
55 Correct 377 ms 5248 KB Output is correct
56 Correct 370 ms 4984 KB Output is correct
57 Correct 370 ms 5112 KB Output is correct
58 Correct 372 ms 4984 KB Output is correct
59 Correct 373 ms 4860 KB Output is correct