#line 1 "correct.cpp" #include #line 4 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/int_alias.cpp" 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; #line 4 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/rep.cpp" 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; } }; #line 4 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/revrep.cpp" class revrep { 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 revrep(const usize first, const usize last) noexcept: first(last - 1), last(std::min(first, last) - 1) { } constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; #line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/bit/ceil_log2.cpp" constexpr u64 ceil_log2(const u64 x) { u64 e = 0; while (((u64) 1 << e) < x) ++e; return e; } #line 8 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/container/segment_tree.cpp" template class SegmentTree { using M = Monoid; usize internal_size, seg_size; std::vector data; void fetch(const usize k) { data[k] = data[2 * k] + data[2 * k + 1]; } public: explicit SegmentTree(const usize size = 0, const M& value = M::zero()): SegmentTree(std::vector(size, value)) { } explicit SegmentTree(const std::vector& vec): internal_size(vec.size()) { seg_size = 1 << ceil_log2(internal_size); data = std::vector(2 * seg_size, M::zero()); for (const usize i: rep(0, internal_size)) data[seg_size + i] = vec[i]; for (const usize i: revrep(1, seg_size)) fetch(i); } usize size() const { return internal_size; } void assign(usize i, const M& value) { assert(i < internal_size); i += seg_size; data[i] = value; while (i > 1) { i >>= 1; fetch(i); } } M fold() const { return data[1]; } M fold(usize l, usize r) const { assert(l <= r and r <= internal_size); l += seg_size; r += seg_size; M ret_l = M::zero(), ret_r = M::zero(); while (l < r) { if (l & 1) ret_l = ret_l + data[l++]; if (r & 1) ret_r = data[--r] + ret_r; l >>= 1; r >>= 1; } return ret_l + ret_r; } template usize max_right(usize l, const F& f) const { assert(l <= internal_size); assert(f(M::zero())); if (l == internal_size) return internal_size; l += seg_size; M sum = M::zero(); do { while (!(l & 1)) l >>= 1; if (!f(sum + data[l])) { while (l < seg_size) { l = 2 * l; if (f(sum + data[l])) sum = sum + data[l++]; } return l - seg_size; } sum = sum + data[l++]; } while ((l & -l) != l); return internal_size; } template usize min_left(usize r, const F& f) const { assert(r <= internal_size); assert(f(M::zero())); if (r == 0) return 0; r += seg_size; M sum = M::zero(); do { r -= 1; while (r > 1 and (r & 1)) r >>= 1; if (!f(data[r] + sum)) { while (r < seg_size) { r = 2 * r + 1; if (f(data[r] + sum)) sum = data[r--] + sum; } return r + 1 - seg_size; } sum = data[r] + sum; } while ((r & -r) != r); return 0; } }; #line 3 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/infty.cpp" template constexpr T INFTY = std::numeric_limits::max() / Div; #line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/setmin.cpp" template bool setmin(T& lhs, const T& rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } #line 7 "correct.cpp" 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); } SegmentTree seg; { Vec vec(N + 2, Monoid::zero()); for (const auto i: rep(0, N + 2)) { vec[i].max = vec[i].min = X[i]; } seg = SegmentTree(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); const auto ok = seg.max_right(first, [&](const Monoid& m) { return !(m.max - start >= rotL or m.min - start <= -rotR or m.difmax >= rotL or m.difmin <= -rotR); }) + 1; const auto m = seg.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; } }