#include using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using isize = std::ptrdiff_t; using usize = std::size_t; class rep { struct Iter { usize itr; constexpr Iter(const usize pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; } constexpr usize operator*() const noexcept { return itr; } }; const Iter first, last; public: explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {} constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; constexpr u64 bit_lzeros(const u64 x) { return x == 0 ? 64 : __builtin_clzll(x); } constexpr u64 bit_width(const u64 x) { return 64 - bit_lzeros(x); } template class SparseTable { using M = IdempotentMonoid; std::vector> table; public: SparseTable() : SparseTable(std::vector()) {} explicit SparseTable(const std::vector& vec) { const auto size = vec.size(); const auto height = bit_width(size); table.reserve(height); table.push_back(vec); for (const usize d : rep(1, height)) { table.push_back(std::vector(size - (1 << d) + 1, M::zero())); for (const usize i : rep(0, table[d].size())) { table[d][i] = table[d - 1][i] + table[d - 1][i + (1 << (d - 1))]; } } } usize size() const { return table[0].size(); } M fold(const usize l, const usize r) const { if (l == r) return M::zero(); assert(l < r); const auto d = bit_width(r - l) - 1; return table[d][l] + table[d][r - (1 << d)]; } }; template constexpr T INFTY = std::numeric_limits::max() / Div; template bool setmin(T& lhs, const T& rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } constexpr i64 MAX_T = 1000000000000000; struct Monoid { static Monoid zero() { return Monoid{-INFTY, INFTY, 0, 0}; } i64 max, min, difmax, difmin; Monoid operator+(const Monoid& other) const { return Monoid{std::max(max, other.max), std::min(min, other.min), std::max({difmax, other.difmax, other.max - min}), std::min({difmin, other.difmin, other.min - max})}; } }; template using Vec = std::vector; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); i64 L; usize N, Q; std::cin >> L >> N >> Q; Vec A(N + 2); for (const auto i : rep(0, N)) { std::cin >> A[i + 1]; } A[0] = 0; A[N + 1] = 2 * MAX_T + 5; Vec X(N + 2); for (const auto i : rep(0, N + 1)) { X[i + 1] = X[i] + (A[i + 1] - A[i]) * (i % 2 == 0 ? 1 : -1); } SparseTable table; { Vec vec(N + 2, Monoid::zero()); for (const auto i : rep(0, N + 2)) { vec[i].max = vec[i].min = X[i]; } table = SparseTable(vec); } i64 LAST = 0; while (Q--) { i64 B, C; std::cin >> B >> C; const i64 x = (B + LAST) % L; const i64 t = (C + LAST) % MAX_T; const usize first = std::upper_bound(A.begin(), A.end(), t) - A.begin(); assert(first > 0); i64 ans = INFTY; if (x == 0) { ans = 0; } else { const auto rotL = x; const auto rotR = L - x; const auto start = X[first - 1] + (t - A[first - 1]) * ((first - 1) % 2 == 0 ? 1 : -1); usize ok = N + 2, ng = first; while (ok - ng > 1) { const auto md = (ok + ng) / 2; const auto m = table.fold(first, md); if (m.max - start >= rotL or m.min - start <= -rotR or m.difmax >= rotL or m.difmin <= -rotR) { ok = md; } else { ng = md; } } const auto m = table.fold(first, ok); if ((ok - 2) % 2 == 0) { if (m.max - start >= rotL) { setmin(ans, (A[ok - 1] - t) - ((m.max - start) - rotL)); } if (m.difmax >= rotL) { setmin(ans, (A[ok - 1] - t) - ((m.difmax - rotL))); } } else { if (m.min - start <= -rotR) { setmin(ans, (A[ok - 1] - t) - ((-rotR) - (m.min - start))); } if (m.difmin <= -rotR) { setmin(ans, (A[ok - 1] - t) - ((-rotR) - m.difmin)); } } } std::cout << ans << '\n'; LAST = ans; } }