Submission #1080582

# Submission time Handle Problem Language Result Execution time Memory
1080582 2024-08-29T11:25:19 Z veehz Abduction 2 (JOI17_abduction2) C++17
100 / 100
2054 ms 326228 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const ll MAXN = 50001;
const ll K = 16;
ll sta[K + 1][MAXN];
ll stb[K + 1][MAXN];
// vector<vector<ll>> sta(K + 1, vector<ll>(MAXN)),
//     stb(K + 1, vector<ll>(MAXN));

ll h, w, q;
vector<ll> a, b;

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

ll max(ll st[K + 1][MAXN], ll l, ll r) {
// ll max(vector<vector<ll>> &st, ll l, ll r) {
  ll j = log2_floor(r - l + 1);
  return max(st[j][l], st[j][r - (1 << j) + 1]);
}

ll find_next(ll st[K + 1][MAXN], ll x, ll l, ll r) {
// ll find_next(vector<vector<ll>> &st, ll x, ll l, ll r) {
  if (l > r) return -1;
  if (max(st, l, r) < x) return -1;
  ll L = l, R = r;
  while (L < R) {
    ll M = (L + R) / 2;
    if (max(st, l, M) < x) {
      L = M + 1;
    } else {
      R = M;
    }
  }
  return L;
}

ll find_prev(ll st[K + 1][MAXN], ll x, ll l, ll r) {
// ll find_prev(vector<vector<ll>> &st, ll x, ll l, ll r) {
  if (l > r) return -1;
  if (max(st, l, r) < x) return -1;
  ll L = l, R = r;
  while (L < R) {
    ll M = (L + R + 1) / 2;
    if (max(st, M, r) < x) {
      R = M - 1;
    } else {
      L = M;
    }
  }
  return L;
}

map<tuple<ll, ll, ll>, ll> memo;
ll _dp(ll x, ll y, ll dir);
ll dp(ll x, ll y, ll dir) {
  // cout << "call dp(" << x << ", " << y << ", " << dir << ")" << endl;
  ll val = _dp(x, y, dir);
  // cout << "dp(" << x << ", " << y << ", " << dir << ") = " << val << endl;
  return val;
}
ll _dp(ll x, ll y, ll dir) {
  if (memo.count({x, y, dir})) return memo[{x, y, dir}];
  if (dir <= 1) {
    // UP/DOWN
    ll cur = b[y];
    ll 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
    ll cur = a[x];
    ll 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);
  }
}

ll dp(ll x, ll 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 (ll i = 0; i < h; i++) {
    cin >> a[i];
    sta[0][i] = a[i];
  }
  for (ll i = 0; i < w; i++) {
    cin >> b[i];
    stb[0][i] = b[i];
  }

  //   for (ll i = 0; i < h; i++) {
  //     cin >> sta[0][i];
  //   }
  //   for (ll i = 0; i < w; i++) {
  //     cin >> stb[0][i];
  //   }

  for (ll i = 1; i <= K; i++) {
    for (ll j = 0; j + (1 << i) <= h; j++) {
      sta[i][j] = max(sta[i - 1][j], sta[i - 1][j + (1 << (i - 1))]);
    }
    for (ll j = 0; j + (1 << i) <= w; j++) {
      stb[i][j] = max(stb[i - 1][j], stb[i - 1][j + (1 << (i - 1))]);
    }
  }

  while (q--) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << dp(x, y) << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 448 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 444 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 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 448 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 444 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 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 800 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 6 ms 1368 KB Output is correct
22 Correct 5 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 448 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 444 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 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 800 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 6 ms 1368 KB Output is correct
22 Correct 5 ms 1884 KB Output is correct
23 Correct 34 ms 13548 KB Output is correct
24 Correct 31 ms 13392 KB Output is correct
25 Correct 29 ms 13396 KB Output is correct
26 Correct 29 ms 13396 KB Output is correct
27 Correct 30 ms 13368 KB Output is correct
28 Correct 43 ms 22608 KB Output is correct
29 Correct 30 ms 14676 KB Output is correct
30 Correct 109 ms 27728 KB Output is correct
31 Correct 130 ms 31312 KB Output is correct
32 Correct 34 ms 14160 KB Output is correct
33 Correct 52 ms 17656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1372 KB Output is correct
2 Correct 4 ms 1328 KB Output is correct
3 Correct 5 ms 1348 KB Output is correct
4 Correct 5 ms 1372 KB Output is correct
5 Correct 6 ms 1328 KB Output is correct
6 Correct 3 ms 1372 KB Output is correct
7 Correct 3 ms 1372 KB Output is correct
8 Correct 7 ms 1884 KB Output is correct
9 Correct 8 ms 1884 KB Output is correct
10 Correct 6 ms 1884 KB Output is correct
11 Correct 12 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 448 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 444 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 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 1 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 2 ms 800 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 6 ms 1368 KB Output is correct
22 Correct 5 ms 1884 KB Output is correct
23 Correct 34 ms 13548 KB Output is correct
24 Correct 31 ms 13392 KB Output is correct
25 Correct 29 ms 13396 KB Output is correct
26 Correct 29 ms 13396 KB Output is correct
27 Correct 30 ms 13368 KB Output is correct
28 Correct 43 ms 22608 KB Output is correct
29 Correct 30 ms 14676 KB Output is correct
30 Correct 109 ms 27728 KB Output is correct
31 Correct 130 ms 31312 KB Output is correct
32 Correct 34 ms 14160 KB Output is correct
33 Correct 52 ms 17656 KB Output is correct
34 Correct 4 ms 1372 KB Output is correct
35 Correct 4 ms 1328 KB Output is correct
36 Correct 5 ms 1348 KB Output is correct
37 Correct 5 ms 1372 KB Output is correct
38 Correct 6 ms 1328 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1372 KB Output is correct
41 Correct 7 ms 1884 KB Output is correct
42 Correct 8 ms 1884 KB Output is correct
43 Correct 6 ms 1884 KB Output is correct
44 Correct 12 ms 2140 KB Output is correct
45 Correct 34 ms 14164 KB Output is correct
46 Correct 36 ms 14176 KB Output is correct
47 Correct 33 ms 14212 KB Output is correct
48 Correct 35 ms 14160 KB Output is correct
49 Correct 35 ms 14172 KB Output is correct
50 Correct 55 ms 22868 KB Output is correct
51 Correct 57 ms 23800 KB Output is correct
52 Correct 178 ms 37888 KB Output is correct
53 Correct 186 ms 36908 KB Output is correct
54 Correct 164 ms 35408 KB Output is correct
55 Correct 287 ms 47364 KB Output is correct
56 Correct 2054 ms 326228 KB Output is correct
57 Correct 501 ms 94288 KB Output is correct
58 Correct 490 ms 90452 KB Output is correct
59 Correct 476 ms 89720 KB Output is correct