結果
問題 | No.1568 Sushi |
ユーザー | KoD |
提出日時 | 2021-04-17 13:58:31 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 294 ms / 2,000 ms |
コード長 | 7,768 bytes |
コンパイル時間 | 4,551 ms |
コンパイル使用メモリ | 209,156 KB |
実行使用メモリ | 28,800 KB |
最終ジャッジ日時 | 2024-06-25 09:56:48 |
合計ジャッジ時間 | 15,243 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 7 ms
5,376 KB |
testcase_05 | AC | 7 ms
5,376 KB |
testcase_06 | AC | 6 ms
5,376 KB |
testcase_07 | AC | 6 ms
5,376 KB |
testcase_08 | AC | 7 ms
5,376 KB |
testcase_09 | AC | 294 ms
26,792 KB |
testcase_10 | AC | 244 ms
16,512 KB |
testcase_11 | AC | 248 ms
16,640 KB |
testcase_12 | AC | 242 ms
16,512 KB |
testcase_13 | AC | 269 ms
26,624 KB |
testcase_14 | AC | 245 ms
16,384 KB |
testcase_15 | AC | 279 ms
28,416 KB |
testcase_16 | AC | 245 ms
16,324 KB |
testcase_17 | AC | 292 ms
28,800 KB |
testcase_18 | AC | 268 ms
26,496 KB |
testcase_19 | AC | 267 ms
26,652 KB |
testcase_20 | AC | 245 ms
16,384 KB |
testcase_21 | AC | 268 ms
26,496 KB |
testcase_22 | AC | 262 ms
25,972 KB |
testcase_23 | AC | 283 ms
28,244 KB |
testcase_24 | AC | 269 ms
25,984 KB |
testcase_25 | AC | 281 ms
27,376 KB |
testcase_26 | AC | 282 ms
28,160 KB |
testcase_27 | AC | 278 ms
28,800 KB |
testcase_28 | AC | 248 ms
17,280 KB |
ソースコード
#line 1 "correct.cpp" #include <bits/stdc++.h> #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 Monoid> class SegmentTree { using M = Monoid; usize internal_size, seg_size; std::vector<M> 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<M>(size, value)) { } explicit SegmentTree(const std::vector<M>& vec): internal_size(vec.size()) { seg_size = 1 << ceil_log2(internal_size); data = std::vector<M>(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 <class F> 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 <class F> 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 <class T, T Div = 2> constexpr T INFTY = std::numeric_limits<T>::max() / Div; #line 2 "/Users/kodamankod/Desktop/CppProcon/Library/proconlib/utility/setmin.cpp" template <class T> 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<i64>, INFTY<i64>, 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 <class T> using Vec = std::vector<T>; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); i64 L; usize N, Q; std::cin >> L >> N >> Q; Vec<i64> 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<i64> 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<Monoid> seg; { Vec<Monoid> 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<i64>; 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; } }