Submission #1080579

# Submission time Handle Problem Language Result Execution time Memory
1080579 2024-08-29T11:21:00 Z veehz Abduction 2 (JOI17_abduction2) C++17
44 / 100
234 ms 34900 KB
#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<int> a, b;

int log2_floor(unsigned long long i) {
  return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
}

int 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>, int> memo;
int _dp(int x, int y, int dir);
int 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;
}
int _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);
  }
}

int 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;
  }
}
# Verdict Execution time Memory 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 344 KB Output is correct
5 Correct 0 ms 444 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 344 KB Output is correct
5 Correct 0 ms 444 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 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 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 1072 KB Output is correct
20 Correct 4 ms 1116 KB Output is correct
21 Correct 4 ms 1116 KB Output is correct
22 Correct 6 ms 1372 KB Output is correct
# Verdict Execution time Memory 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 344 KB Output is correct
5 Correct 0 ms 444 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 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 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 1072 KB Output is correct
20 Correct 4 ms 1116 KB Output is correct
21 Correct 4 ms 1116 KB Output is correct
22 Correct 6 ms 1372 KB Output is correct
23 Correct 29 ms 7504 KB Output is correct
24 Correct 29 ms 7504 KB Output is correct
25 Correct 26 ms 7456 KB Output is correct
26 Correct 29 ms 7516 KB Output is correct
27 Correct 27 ms 7508 KB Output is correct
28 Correct 46 ms 15188 KB Output is correct
29 Correct 31 ms 8632 KB Output is correct
30 Correct 98 ms 18712 KB Output is correct
31 Correct 122 ms 22096 KB Output is correct
32 Correct 32 ms 8020 KB Output is correct
33 Correct 48 ms 11088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 856 KB Output is correct
2 Correct 4 ms 856 KB Output is correct
3 Correct 4 ms 860 KB Output is correct
4 Correct 4 ms 860 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 3 ms 1116 KB Output is correct
7 Correct 3 ms 1116 KB Output is correct
8 Correct 7 ms 1372 KB Output is correct
9 Correct 7 ms 1624 KB Output is correct
10 Correct 6 ms 1532 KB Output is correct
11 Correct 9 ms 1884 KB Output is correct
# Verdict Execution time Memory 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 344 KB Output is correct
5 Correct 0 ms 444 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 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 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 1072 KB Output is correct
20 Correct 4 ms 1116 KB Output is correct
21 Correct 4 ms 1116 KB Output is correct
22 Correct 6 ms 1372 KB Output is correct
23 Correct 29 ms 7504 KB Output is correct
24 Correct 29 ms 7504 KB Output is correct
25 Correct 26 ms 7456 KB Output is correct
26 Correct 29 ms 7516 KB Output is correct
27 Correct 27 ms 7508 KB Output is correct
28 Correct 46 ms 15188 KB Output is correct
29 Correct 31 ms 8632 KB Output is correct
30 Correct 98 ms 18712 KB Output is correct
31 Correct 122 ms 22096 KB Output is correct
32 Correct 32 ms 8020 KB Output is correct
33 Correct 48 ms 11088 KB Output is correct
34 Correct 4 ms 856 KB Output is correct
35 Correct 4 ms 856 KB Output is correct
36 Correct 4 ms 860 KB Output is correct
37 Correct 4 ms 860 KB Output is correct
38 Correct 4 ms 860 KB Output is correct
39 Correct 3 ms 1116 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 7 ms 1372 KB Output is correct
42 Correct 7 ms 1624 KB Output is correct
43 Correct 6 ms 1532 KB Output is correct
44 Correct 9 ms 1884 KB Output is correct
45 Correct 32 ms 8016 KB Output is correct
46 Correct 31 ms 7948 KB Output is correct
47 Correct 41 ms 8020 KB Output is correct
48 Correct 31 ms 8016 KB Output is correct
49 Correct 30 ms 8028 KB Output is correct
50 Correct 50 ms 15220 KB Output is correct
51 Correct 52 ms 15952 KB Output is correct
52 Correct 157 ms 27020 KB Output is correct
53 Correct 156 ms 26492 KB Output is correct
54 Correct 143 ms 25196 KB Output is correct
55 Incorrect 234 ms 34900 KB Output isn't correct
56 Halted 0 ms 0 KB -