#line 1 "main.cpp" /** * @title Template */ #include #include #include #include #include #include #include #line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp" #line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp" class range { struct iter { std::size_t itr; constexpr iter(std::size_t pos) noexcept: itr(pos) { } constexpr void operator ++ () noexcept { ++itr; } constexpr bool operator != (iter other) const noexcept { return itr != other.itr; } constexpr std::size_t operator * () const noexcept { return itr; } }; struct reviter { std::size_t itr; constexpr reviter(std::size_t pos) noexcept: itr(pos) { } constexpr void operator ++ () noexcept { --itr; } constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; } constexpr std::size_t operator * () const noexcept { return itr; } }; const iter first, last; public: constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { } constexpr iter begin() const noexcept { return first; } constexpr iter end() const noexcept { return last; } constexpr reviter rbegin() const noexcept { return reviter(*last - 1); } constexpr reviter rend() const noexcept { return reviter(*first - 1); } }; /** * @title Range */ #line 15 "main.cpp" using i32 = std::int32_t; using i64 = std::int64_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using isize = std::ptrdiff_t; using usize = std::size_t; constexpr i32 inf32 = (u32) ~0 >> 2; constexpr i64 inf64 = (u64) ~0 >> 2; template using Vec = std::vector; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); usize N; std::cin >> N; Vec X(N); for (auto &x: X) { std::cin >> x; } Vec> seg(2 * N); for (auto i: range(0, N)) { seg[N + i].push_back(X[i]); } for (usize i = N; --i > 0;) { seg[i].reserve(seg[i * 2].size() + seg[i * 2 + 1].size()); std::copy(seg[i * 2].begin(), seg[i * 2].end(), std::back_inserter(seg[i])); std::copy(seg[i * 2 + 1].begin(), seg[i * 2 + 1].end(), std::back_inserter(seg[i])); std::inplace_merge(seg[i].begin(), seg[i].begin() + seg[i * 2].size(), seg[i].end()); } usize Q; std::cin >> Q; const auto calc = [&](const std::vector &vec, const u32 val) { const auto itr = std::lower_bound(vec.cbegin(), vec.cend(), val); if (itr == vec.end()) { return val - *std::prev(itr); } if (itr == vec.begin()) { return *itr - val; } return std::min(val - *std::prev(itr), *itr - val); }; while (Q--) { usize l, r; u32 x; std::cin >> l >> r >> x; l -= 1; l += N; r += N; u32 ans = inf32; while (l < r) { if (l & 1) { ans = std::min(ans, calc(seg[l], x)); l += 1; } l >>= 1; if (r & 1) { r -= 1; ans = std::min(ans, calc(seg[r], x)); } r >>= 1; } std::cout << ans << '\n'; } return 0; }