Submission #1005294

# Submission time Handle Problem Language Result Execution time Memory
1005294 2024-06-22T09:56:33 Z 우민규(#10879) Shortcut (IOI16_shortcut) C++14
0 / 100
1 ms 420 KB
#include "shortcut.h"

#include <bits/stdc++.h>
using namespace std;

// range min
struct Seg {
  int n;
  vector<int64_t> seg;
  Seg(int _n) : n(_n) { seg.assign(2 * n, INT64_MAX); }
  void set(int i, int64_t v) {
    i += n;
    seg[i] = v;
    while (i > 1) {
      i /= 2;
      seg[i] = std::min(seg[2 * i], seg[2 * i + 1]);
    }
  }
  int64_t min(int l, int r) {
    l += n, r += n;
    int64_t minimum = INT64_MAX;
    while (l < r) {
      if (l & 1) {
        minimum = std::min(minimum, seg[l]);
        l += 1;
      }
      if (r & 1) {
        r -= 1;
        minimum = std::min(minimum, seg[r]);
      }
      l /= 2, r /= 2;
    }
    return minimum;
  }
};
// range max
struct Seg2 {
  int n;
  vector<int64_t> seg;
  Seg2(int _n) : n(_n) { seg.assign(2 * n, INT64_MIN); }
  void set(int i, int64_t v) {
    i += n;
    seg[i] = v;
    while (i > 1) {
      i /= 2;
      seg[i] = std::max(seg[2 * i], seg[2 * i + 1]);
    }
  }
  int64_t max(int l, int r) {
    l += n, r += n;
    int64_t minimum = INT64_MIN;
    while (l < r) {
      if (l & 1) {
        minimum = std::max(minimum, seg[l]);
        l += 1;
      }
      if (r & 1) {
        r -= 1;
        minimum = std::max(minimum, seg[r]);
      }
      l /= 2, r /= 2;
    }
    return minimum;
  }
};

long long find_shortcut(int n, vector<int> l, vector<int> d, int c) {
  vector<int64_t> x(n), xpdsfx_xmd_min(n + 1), xpdsfx_x_max(n + 1);
  vector<pair<int64_t, int>> xpd(n);
  vector<int> xpd_ord(n);

  x[0] = 0;
  for (int i = 0; i < n - 1; ++i) x[i + 1] = x[i] + l[i];
  for (int i = 0; i < n; ++i) xpd[i] = {x[i] + d[i], i};
  sort(xpd.begin(), xpd.end());

  for (int i = 0; i < n; ++i) xpd_ord[xpd[i].second] = i;

  xpdsfx_xmd_min[n] = INT64_MAX;
  xpdsfx_x_max[n] = INT64_MIN;
  for (int i = n - 1; i >= 0; --i)
    xpdsfx_xmd_min[i] =
        min(xpdsfx_xmd_min[i + 1], x[xpd[i].second] - d[xpd[i].second]),
    xpdsfx_x_max[i] = max(xpdsfx_x_max[i + 1], x[xpd[i].second]);

  vector<int64_t> last_xpd(n, INT64_MIN), first_xmd(n, INT64_MIN);

  // rect of [a, b] transpose w. length <= l
  auto find_rect = [&](int64_t l) -> array<int64_t, 4> {
    int64_t gpl = INT64_MIN, gpu = INT64_MAX, gml = INT64_MIN, gmu = INT64_MAX;
    // xmd only exist for x[v] > x[u], otherwise MAX in xpd order
    Seg sxmd(n);
    Seg2 sxpd(n);
    for (int i = 0; i < n; ++i) {
      sxmd.set(i, x[xpd[i].second] - d[xpd[i].second]);
      sxpd.set(i, x[xpd[i].second] + d[xpd[i].second]);
    }
    for (int u = 0; u < n; ++u) {
      sxmd.set(xpd_ord[u], INT64_MAX);
      sxpd.set(xpd_ord[u], INT64_MIN);
      // |u-v| > l- d[u] - d[v]
      // v < u: pass
      // u < v: v-u > l-d[u] - d[v]
      // v + d[v] > l + u - d[u]
      // xpd[v] > l + u - d[u]
      int v_lower = upper_bound(xpd.begin(), xpd.end(),
                                make_pair(l + x[u] - d[u], INT_MAX)) -
                    xpd.begin();
      if (v_lower == xpd.size()) continue;
      // for v in xpd[v_lower..] and x[v] > x[u]
      // find last elem in xpd w. x[v] > x[u]
      auto last_x_it =
          lower_bound(xpdsfx_x_max.begin(), xpdsfx_x_max.end(), x[u],
                      [&](int64_t x1, int64_t x2) { return x1 > x2; });
      if (last_x_it == xpdsfx_x_max.begin()) continue;
      assert(last_x_it == xpdsfx_x_max.end() || *last_x_it <= x[u]);
      int last_x = *--last_x_it;
      assert(last_x > x[u]);
      int last_v = lower_bound(x.begin(), x.end(), last_x) - x.begin();
      assert(last_v > u);

      int64_t maximum_xpd = sxpd.max(v_lower, n);

      // find elem in xpd[v_lower..] w. x[v] > x[u] and x[v]-d[v] min
      int64_t minimum_xmd = sxmd.min(v_lower, n);

      assert((minimum_xmd == INT64_MAX) == (xpd_ord[last_v] < v_lower));
      assert((maximum_xpd == INT64_MIN) == (minimum_xmd == INT64_MAX));
      if (xpd_ord[last_v] < v_lower) continue;
      assert(x[last_v] + d[last_v] == maximum_xpd);

      // if (minimum_xmd == INT64_MAX) {
      //   for (int i = 0; i < n; ++i) cout << xmd.min(i, i + 1) << " ";
      //   cout << "\n" << flush;
      //   int x = 3;
      // }

      int64_t last_xpd = x[last_v] + d[last_v];
      // xpd[u] + xpd[v] - (l-c) max
      int64_t pl = (x[u] + d[u]) + last_xpd - (l - c);
      // xmd[u] + xmd[v] + (l-c) min
      int64_t pu = (x[u] - d[u]) + minimum_xmd + (l - c);
      // -xmd[u] + xpd[v] - (l-c) max
      int64_t ml = -(x[u] - d[u]) + last_xpd - (l - c);
      // -xpd[u] + xmd[v] + (l-c) min
      int64_t mu = -(x[u] + d[u]) + minimum_xmd + (l - c);

      gpl = max(gpl, pl), gpu = min(gpu, pu);
      gml = max(gml, ml), gmu = min(gmu, mu);
    }
    return {gpl, gpu, gml, gmu};
  };
  auto conatins_lattice_point = [&](array<int64_t, 4> rect) -> bool {
    auto [pl, pu, ml, mu] = rect;
    if (pl > pu || ml > mu) return false;
    if (pl == INT64_MIN) {
      assert(pu == INT64_MAX && ml == INT64_MIN && mu == INT64_MAX);
      return true;
    }
    for (int a = 0; a < n; ++a) {
      // pl <= a+b <= pu
      // ml <= b-a <= mu
      // pl - a <= b <= pu - a
      // ml + a <= b <= mu + a
      int64_t bl = max(pl - x[a], ml + x[a]);
      int64_t bu = min(pu - x[a], mu + x[a]);
      if (bl > bu) continue;
      if (lower_bound(x.begin(), x.end(), bl) !=
          upper_bound(x.begin(), x.end(), bu)) {
        return true;
      }
    }
    return false;
  };
  int64_t ll = 0, lu = INT64_MAX / 2;
  // diameter <= ll impos, <= lu pos
  while (ll + 1 < lu) {
    int64_t lm = ll + (lu - ll) / 2;
    if (conatins_lattice_point(find_rect(lm))) {
      // lm possible
      lu = lm;
    } else {
      ll = lm;
    }
  }
  return lu;
}

#ifndef EVAL
#include "grader.cpp"
#endif

Compilation message

shortcut.cpp: In lambda function:
shortcut.cpp:109:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |       if (v_lower == xpd.size()) continue;
      |           ~~~~~~~~^~~~~~~~~~~~~
shortcut.cpp: In lambda function:
shortcut.cpp:154:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  154 |     auto [pl, pu, ml, mu] = rect;
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n = 4, 80 is a correct answer
2 Correct 0 ms 348 KB n = 9, 110 is a correct answer
3 Correct 0 ms 348 KB n = 4, 21 is a correct answer
4 Correct 0 ms 344 KB n = 3, 4 is a correct answer
5 Correct 0 ms 348 KB n = 2, 62 is a correct answer
6 Correct 0 ms 348 KB n = 2, 3 is a correct answer
7 Correct 0 ms 348 KB n = 3, 29 is a correct answer
8 Correct 0 ms 420 KB n = 2, 3 is a correct answer
9 Correct 0 ms 348 KB n = 2, 3 is a correct answer
10 Correct 0 ms 348 KB n = 2, 2000000001 is a correct answer
11 Correct 0 ms 348 KB n = 2, 3000000000 is a correct answer
12 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
13 Correct 0 ms 348 KB n = 3, 3000000000 is a correct answer
14 Runtime error 1 ms 348 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -