結果

問題 No.2248 max(C)-min(C)
ユーザー kkishikkishi
提出日時 2023-03-17 23:09:18
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 624 ms / 3,000 ms
コード長 10,402 bytes
コンパイル時間 3,527 ms
コンパイル使用メモリ 162,552 KB
実行使用メモリ 32,704 KB
最終ジャッジ日時 2023-10-18 16:08:31
合計ジャッジ時間 22,708 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 522 ms
22,220 KB
testcase_19 AC 43 ms
6,584 KB
testcase_20 AC 622 ms
32,704 KB
testcase_21 AC 574 ms
32,384 KB
testcase_22 AC 372 ms
19,640 KB
testcase_23 AC 206 ms
12,748 KB
testcase_24 AC 66 ms
7,280 KB
testcase_25 AC 535 ms
31,864 KB
testcase_26 AC 464 ms
21,524 KB
testcase_27 AC 527 ms
31,800 KB
testcase_28 AC 32 ms
5,456 KB
testcase_29 AC 486 ms
21,700 KB
testcase_30 AC 156 ms
11,428 KB
testcase_31 AC 624 ms
32,704 KB
testcase_32 AC 76 ms
7,616 KB
testcase_33 AC 138 ms
11,076 KB
testcase_34 AC 589 ms
32,704 KB
testcase_35 AC 588 ms
32,704 KB
testcase_36 AC 599 ms
32,704 KB
testcase_37 AC 260 ms
19,420 KB
testcase_38 AC 581 ms
24,528 KB
testcase_39 AC 581 ms
24,528 KB
testcase_40 AC 576 ms
24,528 KB
testcase_41 AC 464 ms
22,092 KB
testcase_42 AC 591 ms
24,528 KB
testcase_43 AC 502 ms
23,280 KB
testcase_44 AC 571 ms
24,528 KB
testcase_45 AC 423 ms
21,084 KB
testcase_46 AC 196 ms
12,436 KB
testcase_47 AC 590 ms
24,528 KB
testcase_48 AC 239 ms
20,440 KB
testcase_49 AC 614 ms
32,704 KB
testcase_50 AC 614 ms
32,704 KB
testcase_51 AC 619 ms
32,704 KB
testcase_52 AC 609 ms
32,704 KB
testcase_53 AC 66 ms
7,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

struct Compressor {
  Compressor(const std::vector<int64_t>& v) : coord(v) {
    std::sort(coord.begin(), coord.end());
    coord.erase(std::unique(coord.begin(), coord.end()), coord.end());
  }
  int64_t operator()(int64_t x) const {
    return std::lower_bound(coord.begin(), coord.end(), x) - coord.begin();
  }
  std::vector<int64_t> coord;
};

template <typename T>
class DualSegmentTree {
 public:
  using Operation = std::function<T(T, T)>;
  DualSegmentTree(int size, Operation operation, T identity = T())
      : operation_(operation), identity_(identity) {
    int two = 1;
    while (two < size) {
      two <<= 1;
    }
    v_.resize(two * 2 - 1, identity_);
  }
  void Update(int begin, int end, T v) {
    int l = Leaf(begin), r = Leaf(end);
    while (l < r) {
      if (IsRight(l)) {
        v_[l] = operation_(v_[l], v);
        ++l;
      }
      l = Parent(l);
      if (IsRight(r)) {
        v_[r - 1] = operation_(v_[r - 1], v);
      }
      r = Parent(r);
    }
  }
  T Get(int i) const {
    T v = identity_;
    int index = Leaf(i);
    while (true) {
      v = operation_(v, v_[index]);
      if (index == 0) break;
      index = Parent(index);
    }
    return v;
  }

 private:
  int Leaf(int i) const { return i + (v_.size() >> 1); }
  bool IsRight(int i) const { return !(i & 1); }
  int Parent(int i) const { return (i - 1) >> 1; }
  const Operation operation_;
  const T identity_;
  std::vector<T> v_;
};

#ifndef CONSTANTS_H_
#define CONSTANTS_H_

// big = 2305843009213693951 = 2^61-1 ~= 2.3*10^18
const int64_t big = std::numeric_limits<int64_t>::max() / 4;

#endif  // CONSTANTS_H_
#ifndef DEBUG_H_
#define DEBUG_H_

#ifndef TYPE_TRAITS_H_
#define TYPE_TRAITS_H_

template <typename T, typename = void>
struct is_dereferenceable : std::false_type {};
template <typename T>
struct is_dereferenceable<T, std::void_t<decltype(*std::declval<T>())>>
    : std::true_type {};

template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = void>
struct is_applicable : std::false_type {};
template <typename T>
struct is_applicable<T, std::void_t<decltype(std::tuple_size<T>::value)>>
    : std::true_type {};

#endif  // TYPE_TRAITS_H

template <typename T, typename... Ts>
void debug(std::ostream& os, const T& value, const Ts&... args);
template <typename T>
void debug(std::ostream& os, const T& v) {
  if constexpr (std::is_same<int64_t, std::decay_t<T>>::value) {
    if (v == big) {
      os << "big";
    } else {
      os << v;
    }
  } else if constexpr (std::is_same<char*, std::decay_t<T>>::value ||
                       std::is_same<std::string, T>::value) {
    os << v;
  } else if constexpr (is_dereferenceable<T>::value) {
    os << "{";
    if (v) {
      debug(os, *v);
    } else {
      os << "nil";
    }
    os << "}";
  } else if constexpr (is_iterable<T>::value) {
    os << "{";
    for (auto it = std::begin(v); it != std::end(v); ++it) {
      if (it != std::begin(v)) os << ", ";
      debug(os, *it);
    }
    os << "}";
  } else if constexpr (is_applicable<T>::value) {
    os << "{";
    std::apply([&os](const auto&... args) { debug(os, args...); }, v);
    os << "}";
  } else {
    os << v;
  }
}
template <typename T, typename... Ts>
void debug(std::ostream& os, const T& value, const Ts&... args) {
  debug(os, value);
  os << ", ";
  debug(os, args...);
}
#if DEBUG
#define dbg(...)                             \
  do {                                       \
    std::cerr << #__VA_ARGS__ << ": ";       \
    debug(std::cerr, __VA_ARGS__);           \
    std::cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

#endif  // DEBUG_H_
#ifndef FIX_H_
#define FIX_H_

template <class F>
struct FixPoint {
  F f;
  template <class... Args>
  decltype(auto) operator()(Args&&... args) const {
    return f(std::ref(*this), std::forward<Args>(args)...);
  }
};
template <class F>
FixPoint<std::decay_t<F>> Fix(F&& f) {
  return {std::forward<F>(f)};
}

#endif  // FIX_H_
#ifndef IO_H_
#define IO_H

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);
#define ints(...) rd(int, __VA_ARGS__);
#define strings(...) rd(string, __VA_ARGS__);

const char *yes_str = "Yes", *no_str = "No";

template <typename T>
void write_to_cout(const T& value) {
  if constexpr (std::is_same<T, bool>::value) {
    std::cout << (value ? yes_str : no_str);
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    for (auto it = std::begin(value); it != std::end(value); ++it) {
      if (it != std::begin(value)) std::cout << " ";
      std::cout << *it;
    }
  } else {
    std::cout << value;
  }
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  write_to_cout(value);
  std::cout << ' ';
  write_to_cout(args...);
}
#define wt(...)                 \
  do {                          \
    write_to_cout(__VA_ARGS__); \
    cout << '\n';               \
  } while (0)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T, typename U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}

#endif  // IO_H_
#ifndef MACROS_H_
#define MACROS_H_

#define all(x) (x).begin(), (x).end()
#define eb(...) emplace_back(__VA_ARGS__)
#define pb(...) push_back(__VA_ARGS__)

#define dispatch(_1, _2, _3, name, ...) name

#define as_i64(x)                                                          \
  (                                                                        \
      [] {                                                                 \
        static_assert(                                                     \
            std::is_integral<                                              \
                typename std::remove_reference<decltype(x)>::type>::value, \
            "rep macro supports std integral types only");                 \
      },                                                                   \
      static_cast<int64_t>(x))

#define rep3(i, a, b) for (int64_t i = as_i64(a); i < as_i64(b); ++i)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(_loop_variable_, n)
#define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep3(i, a, b) for (int64_t i = as_i64(b) - 1; i >= as_i64(a); --i)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep1(n) rrep2(_loop_variable_, n)
#define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define each3(k, v, c) for (auto&& [k, v] : c)
#define each2(e, c) for (auto&& e : c)
#define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__)

template <typename T, typename U>
bool chmax(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
bool chmin(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
auto max(T a, U b) {
  return a > b ? a : b;
}

template <typename T, typename U>
auto min(T a, U b) {
  return a < b ? a : b;
}

template <typename T>
auto max(const T& v) {
  return *std::max_element(v.begin(), v.end());
}

template <typename T>
auto min(const T& v) {
  return *std::min_element(v.begin(), v.end());
}

template <typename T>
int64_t sz(const T& v) {
  return std::size(v);
}

template <typename T>
int64_t popcount(T i) {
  return std::bitset<std::numeric_limits<T>::digits>(i).count();
}

template <typename T>
bool hasbit(T s, int i) {
  return std::bitset<std::numeric_limits<T>::digits>(s)[i];
}

template <typename T, typename U>
auto div_floor(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -((-n + d - 1) / d);
  }
  return n / d;
};

template <typename T, typename U>
auto div_ceil(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -(-n / d);
  }
  return (n + d - 1) / d;
}

template <typename T>
bool even(T x) {
  return x % 2 == 0;
}

std::array<std::pair<int64_t, int64_t>, 4> adjacent(int64_t i, int64_t j) {
  return {{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}}};
}

bool inside(int64_t i, int64_t j, int64_t I, int64_t J) {
  return 0 <= i && i < I && 0 <= j && j < J;
}

template <typename T>
void sort(T& v) {
  return std::sort(v.begin(), v.end());
}

template <typename T, typename Compare>
void sort(T& v, Compare comp) {
  return std::sort(v.begin(), v.end(), comp);
}

template <typename T>
void reverse(T& v) {
  return std::reverse(v.begin(), v.end());
}

template <typename T>
typename T::value_type accumulate(const T& v) {
  return std::accumulate(v.begin(), v.end(), typename T::value_type());
}

using i64 = int64_t;
using i32 = int32_t;

template <typename T>
using low_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

#endif  // MACROS_H_

void Main();

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}

using namespace std;

#define int i64

void Main() {
  ints(n);
  V<int> a(n), b(n);
  cin >> a >> b;
  V<int> v;
  rep(i, n) {
    v.eb(a[i]);
    v.eb(b[i]);
    v.eb((a[i] + b[i]) / 2);
  }
  v.eb(0);
  v.eb(big);
  Compressor c(v);
  int N = sz(c.coord);
  DualSegmentTree<int> t(
      N, [](int a, int b) { return max(a, b); }, -big);
  rep(i, N) t.Update(i, i + 1, i);
  rep(i, N) dbg(i, c.coord[i], t.Get(i));
  rep(i, n) {
    V<int> v;
    v.eb(0);
    v.eb(N - 1);  // big
    v.eb(c(a[i]));
    v.eb(c(b[i]));
    v.eb(c((a[i] + b[i]) / 2));
    sort(v);
    dbg(v);
    rep(i, sz(v) - 1) t.Update(v[i] + 1, v[i + 1] + 1, v[i + 1]);
  }
  rep(i, N) dbg(i, t.Get(i));
  rep(i, N) dbg(c.coord[i], c.coord[t.Get(i)]);
  int ans = big;
  rep(i, 1, N - 1) chmin(ans, c.coord[t.Get(i)] - c.coord[i]);
  wt(ans);
}
0