#include #include #include #include #include namespace nono { template class SegmentTree { public: SegmentTree(const int size, const std::function op, const std::function e) : op_(op), e_(e) { init(size); } SegmentTree(const std::vector &vec, const std::function op, const std::function e) : op_(op), e_(e) { int n = vec.size(); init(n); for (int i = 0; i < size_; --i) { data_[i + size_] = vec[i]; } for (int i = size_ - 1; i > 0; --i) { data_[i] = op_(data_[i << 1], data_[(i << 1) + 1]); } } void set(int i, const T value) { i += size_; data_[i] = value; update(i); } void add(int i, const T delta) { i += size_; data_[i] += delta; update(i); } T get_all() const { return data_[1]; } T get_range(int l, int r) const { T result_l = e_(), result_r = e_(); l += size_; r += size_; while (l < r) { if (l & 1) { result_l = op_(result_l, data_[l++]); } if (r & 1) { result_r = op_(data_[--r], result_r); } l >>= 1; r >>= 1; } return op_(result_l, result_r); } T operator[](int i) const { return data_[i + size_]; } private: const std::function op_; const std::function e_; int size_; std::vector data_; void init(int n) { int log = 1; while (n > (1 << log)) ++log; size_ = 1 << log; data_.assign(2 * size_, e_()); } void update(int i) { i >>= 1; while (i > 0) { data_[i] = op_(data_[i << 1], data_[(i << 1) + 1]); i >>= 1; } } }; } // namespace nono int main() { constexpr int inf = 1e9 + 1; int n; std::cin >> n; std::vector a(n), b(n); std::vector> order; order.reserve(3 * n); for (int i = 0; i < n; i++) std::cin >> a[i]; for (int i = 0; i < n; i++) std::cin >> b[i]; if (n == 1) { std::cout << 0 << '\n'; return 0; } for (int i = 0; i < n; i++) { order.emplace_back(a[i], i); order.emplace_back(b[i], i); order.emplace_back((a[i] + b[i]) / 2, i); } std::sort(order.begin(), order.end()); auto op = [](const int i, const int j) { return i < j ? i : j; }; auto e = [&]() { return inf; }; nono::SegmentTree segtree(n, op, e); for (int i = 0; i < n; i++) { segtree.set(i, -1); } int ans = inf; for (const auto &[v, i]: order) { int l = 0 < i ? segtree.get_range(0, i) : inf; int r = i + 1 < n ? segtree.get_range(i + 1, n) : inf; if (l != -1 && r != -1) { ans = std::min(ans, v - std::min(l, r)); } segtree.set(i, v); // std::cout << "_________" << '\n'; // std::cout << "v: " << v << ", i: " << i << '\n'; // std::cout << "l: " << l << ", r: " << r << '\n'; // std::cout << ans << '\n'; // for (int j = 0; j < n; j++) { // std::cout << segtree[j] << ' '; // } // std::cout << '\n'; } std::cout << ans << '\n'; }