Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/data_structure/deque_operate_all_composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite"
#include "../../../src/template/template.hpp"
#include "../../../src/template/static_modint.hpp"
#include "../../../src/data_structure/slide_window_aggregation_deque.hpp"
using mint = modint998244353;
struct S {
    mint a, b;
};
S op(S x, S y) {
    return {x.a * y.a, x.b * y.a + y.b};
}
S e() {
    return {1, 0};
}
int main(void) {
    SlideWindowAggregationDeque<S, op, e> swag;
    int q;
    cin >> q;
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            mint a, b;
            cin >> a >> b;
            swag.push_front({a, b});
        } else if(t == 1) {
            mint a, b;
            cin >> a >> b;
            swag.push_back({a, b});
        } else if(t == 2) {
            swag.pop_front();
        } else if(t == 3) {
            swag.pop_back();
        } else {
            mint x;
            cin >> x;
            S res = swag.prod();
            cout << res.a * x + res.b << '\n';
        }
    }
}
#line 1 "verify/library_checker/data_structure/deque_operate_all_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/template/static_modint.hpp"
template <uint32_t m>
struct StaticModint {
    using mint = StaticModint;
    static constexpr uint32_t mod() {
        return m;
    }
    static constexpr mint raw(const uint32_t v) {
        mint a;
        a._v = v;
        return a;
    }
    constexpr StaticModint()
        : _v(0) {}
    template <class T>
    constexpr StaticModint(const T& v) {
        static_assert(is_integral_v<T>);
        if constexpr(is_signed_v<T>) {
            int64_t x = int64_t(v % int64_t(m));
            if(x < 0) x += m;
            _v = uint32_t(x);
        } else _v = uint32_t(v % m);
    }
    constexpr uint32_t val() const {
        return _v;
    }
    constexpr mint& operator++() {
        return *this += 1;
    }
    constexpr mint& operator--() {
        return *this -= 1;
    }
    constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr mint& operator+=(mint rhs) {
        if(_v >= m - rhs._v) _v -= m;
        _v += rhs._v;
        return *this;
    }
    constexpr mint& operator-=(mint rhs) {
        if(_v < rhs._v) _v += m;
        _v -= rhs._v;
        return *this;
    }
    constexpr mint& operator*=(mint rhs) {
        return *this = *this * rhs;
    }
    constexpr mint& operator/=(mint rhs) {
        return *this *= rhs.inv();
    }
    constexpr mint operator+() const {
        return *this;
    }
    constexpr mint operator-() const {
        return mint{} - *this;
    }
    constexpr mint pow(long long n) const {
        assert(0 <= n);
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    constexpr mint inv() const {
        if constexpr(prime) {
            assert(_v);
            return pow(m - 2);
        } else {
            const auto eg = inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }
    friend constexpr mint operator+(mint lhs, mint rhs) {
        return lhs += rhs;
    }
    friend constexpr mint operator-(mint lhs, mint rhs) {
        return lhs -= rhs;
    }
    friend constexpr mint operator*(mint lhs, mint rhs) {
        return uint64_t(lhs._v) * rhs._v;
    }
    friend constexpr mint operator/(mint lhs, mint rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(mint lhs, mint rhs) {
        return lhs._v == rhs._v;
    }
    friend constexpr bool operator!=(mint lhs, mint rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    uint32_t _v = 0;
    static constexpr bool prime = []() -> bool {
        if(m == 1) return 0;
        if(m == 2 or m == 7 or m == 61) return 1;
        if(m % 2 == 0) return 0;
        uint32_t d = m - 1;
        while(d % 2 == 0) d /= 2;
        for(uint32_t a : {2, 7, 61}) {
            uint32_t t = d;
            mint y = mint(a).pow(t);
            while(t != m - 1 && y != 1 && y != m - 1) {
                y *= y;
                t <<= 1;
            }
            if(y != m - 1 && t % 2 == 0) return 0;
        }
        return 1;
    }();
    static constexpr pair<int32_t, int32_t> inv_gcd(const int32_t a, const int32_t b) {
        if(a == 0) return {b, 0};
        int32_t s = b, t = a, m0 = 0, m1 = 1;
        while(t) {
            const int32_t u = s / t;
            s -= t * u;
            m0 -= m1 * u;
            swap(s, t);
            swap(m0, m1);
        }
        if(m0 < 0) m0 += b / s;
        return {s, m0};
    }
};
using modint998244353 = StaticModint<998244353>;
using modint1000000007 = StaticModint<1000000007>;
#line 3 "src/data_structure/slide_window_aggregation_deque.hpp"
template <typename S, auto op, auto e>
struct SlideWindowAggregationDeque {
    void push_front(const S& t) {
        push0(t);
    }
    void push_back(const S& t) {
        push1(t);
    }
    S front() const {
        return a0.empty() ? a1.front() : a0.back();
    }
    S back() const {
        return a1.empty() ? a0.front() : a1.back();
    }
    void pop_front() {
        if(a0.empty()) rebalance();
        assert(!a0.empty());
        a0.pop_back(), r0.pop_back();
    }
    void pop_back() {
        if(a1.empty()) rebalance();
        assert(!a1.empty());
        a1.pop_back(), r1.pop_back();
    }
    S prod() const {
        return op(get0(), get1());
    }

   private:
    vector<S> a0, a1, r0, r1;
    S get0() const {
        return r0.empty() ? e() : r0.back();
    }
    S get1() const {
        return r1.empty() ? e() : r1.back();
    }
    void push0(const S& x) {
        a0.emplace_back(x);
        r0.emplace_back(op(x, get0()));
    }
    void push1(const S& x) {
        a1.emplace_back(x);
        r1.emplace_back(op(get1(), x));
    }
    void rebalance() {
        const int n = a0.size() + a1.size();
        const int s0 = n / 2 + (a0.empty() ? n % 2 : 0);
        vector<S> a{a0};
        reverse(begin(a), end(a));
        copy(begin(a1), end(a1), back_inserter(a));
        a0.clear(), r0.clear();
        a1.clear(), r1.clear();
        for(int i = s0 - 1; i >= 0; --i) push0(a[i]);
        for(int i = s0; i < n; ++i) push1(a[i]);
    }
};
#line 5 "verify/library_checker/data_structure/deque_operate_all_composite.test.cpp"
using mint = modint998244353;
struct S {
    mint a, b;
};
S op(S x, S y) {
    return {x.a * y.a, x.b * y.a + y.b};
}
S e() {
    return {1, 0};
}
int main(void) {
    SlideWindowAggregationDeque<S, op, e> swag;
    int q;
    cin >> q;
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            mint a, b;
            cin >> a >> b;
            swag.push_front({a, b});
        } else if(t == 1) {
            mint a, b;
            cin >> a >> b;
            swag.push_back({a, b});
        } else if(t == 2) {
            swag.pop_front();
        } else if(t == 3) {
            swag.pop_back();
        } else {
            mint x;
            cin >> x;
            S res = swag.prod();
            cout << res.a * x + res.b << '\n';
        }
    }
}
Back to top page