結果

問題 No.1568 Sushi
ユーザー KoDKoD
提出日時 2021-04-17 13:07:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 5,113 bytes
コンパイル時間 3,272 ms
コンパイル使用メモリ 212,000 KB
実行使用メモリ 116,480 KB
最終ジャッジ日時 2024-06-25 09:54:25
合計ジャッジ時間 13,206 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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 IdempotentMonoid> class SparseTable {
    using M = IdempotentMonoid;
    std::vector<std::vector<M>> table;

  public:
    SparseTable() : SparseTable(std::vector<M>()) {}
    explicit SparseTable(const std::vector<M>& 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<M>(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 <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T> 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<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);
    }
    SparseTable<Monoid> table;
    {
        Vec<Monoid> 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<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);
            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;
    }
}
0