kyopro_library

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

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: test/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 <iostream>

#include "../../../lib/math/modint.hpp"
#include "../../../lib/data_structure/deswag.hpp"

using namespace std;

using mint = ModInt<998244353>;

using T = pair<mint, mint>;
T op(T x1, T x2){
    return {x1.first * x2.first, x2.first * x1.second + x2.second};
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    DESWAG<T, op> 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{
            long long x; cin >> x;
            if(swag.empty()){
                cout << x << "\n";
                continue;
            }
            T k = swag.fold();
            mint ans = k.first * x + k.second;
            cout << ans << "\n";
        }
    }
}
#line 1 "test/library_checker/data_structure/deque_operate_all_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite"
#include <iostream>

#line 2 "lib/math/modint.hpp"

#line 4 "lib/math/modint.hpp"
#include <cassert>

/**
 * @brief ModInt
 * @docs docs/math/modint.md
 */

template <long long Modulus>
struct ModInt{
    long long val;
    static constexpr int mod() { return Modulus; }
    constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
        normalize();
    }
    void normalize(){
        val = (val % Modulus + Modulus) % Modulus;
    }
    inline ModInt &operator+=(const ModInt &rhs) noexcept {
        if(val += rhs.val, val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt &operator-=(const ModInt &rhs) noexcept {
        if(val -= rhs.val, val < 0) val += Modulus;
        return *this;
    }
    inline ModInt &operator*=(const ModInt &rhs) noexcept {
        val = val * rhs.val % Modulus;
        return *this;
    }
    inline ModInt &operator/=(const ModInt &rhs) noexcept {
        val = val * inv(rhs.val).val % Modulus;
        return *this;
    }
    inline ModInt &operator++() noexcept {
        if(++val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt operator++(int) noexcept {
        ModInt t = val;
        if(++val >= Modulus) val -= Modulus;
        return t;
    }
    inline ModInt &operator--() noexcept {
        if(--val < 0) val += Modulus;
        return *this;
    }
    inline ModInt operator--(int) noexcept {
        ModInt t = val;
        if(--val < 0) val += Modulus;
        return t;
    }
    inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
    inline ModInt inv(void) const { return inv(val); }
    ModInt pow(long long n) const {
        assert(0 <= n);
        ModInt x = *this, r = 1;
        while(n){
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    ModInt inv(const long long n) const {
        long long a = n, b = Modulus, u = 1, v = 0;
        while(b){
            long long t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        u %= Modulus;
        if(u < 0) u += Modulus;
        return u;
    }
    friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
    friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
    friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
    friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
    friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
    friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
    friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
        is >> x.val;
        x.normalize();
        return is;
    }
    friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 2 "lib/data_structure/deswag.hpp"

/**
 * @brief DESWAG
 * @docs docs/data_structure/deswag.md
 */

#include <stack>
#line 10 "lib/data_structure/deswag.hpp"

template <typename T, T (*op)(T, T)>
struct DESWAG{
private:
    struct node{
    public:
        T val, sum;
        node(const T &val, const T &sum) : val(val), sum(sum) {}
    };

    std::stack<node> front_stack, back_stack, temp_stack;

public:
    DESWAG() : front_stack(), back_stack(), temp_stack() {}

    bool empty(){
        return front_stack.empty() && back_stack.empty();
    }

    size_t size(){
        return front_stack.size() + back_stack.size();
    }

    T fold(){
        if(front_stack.empty()){
            return back_stack.top().sum;
        } else if(back_stack.empty()){
            return front_stack.top().sum;
        } else{
            return op(front_stack.top().sum, back_stack.top().sum);
        }
    }

    void push_front(const T &x){
        if(front_stack.empty()){
            front_stack.emplace(x, x);
        } else{
            T s = op(x, front_stack.top().sum);
            front_stack.emplace(x, s);
        }
    }

    void push_back(const T &x){
        if(back_stack.empty()){
            back_stack.emplace(x, x);
        } else{
            T s = op(back_stack.top().sum, x);
            back_stack.emplace(x, s);
        }
    }

    void pop_front(){
        if(front_stack.empty()){
            size_t half = (back_stack.size() + 1) / 2;
            while(!back_stack.empty()){
                if(back_stack.size() == half){
                    front_stack.emplace(back_stack.top().val, back_stack.top().val);
                } else if(back_stack.size() < half){
                    T s = op(back_stack.top().val, front_stack.top().sum);
                    front_stack.emplace(back_stack.top().val, s);
                } else{
                    temp_stack.emplace(back_stack.top().val, back_stack.top().val);
                }
                back_stack.pop();
            }
            if(!temp_stack.empty()){
                back_stack.emplace(temp_stack.top().val, temp_stack.top().val);
                temp_stack.pop();
                while(!temp_stack.empty()){
                    T s = op(back_stack.top().sum, temp_stack.top().val);
                    back_stack.emplace(temp_stack.top().val, s);
                    temp_stack.pop();
                }
            }
        }
        front_stack.pop();
    }

    void pop_back(){
        if(back_stack.empty()){
            size_t half = (front_stack.size() + 1) / 2;
            while(!front_stack.empty()){
                if(front_stack.size() == half){
                    back_stack.emplace(front_stack.top().val, front_stack.top().val);
                } else if(front_stack.size() < half){
                    T s = op(back_stack.top().sum, front_stack.top().val);
                    back_stack.emplace(front_stack.top().val, s);
                } else{
                    temp_stack.emplace(front_stack.top().val, front_stack.top().val);
                }
                front_stack.pop();
            }
            if(!temp_stack.empty()){
                front_stack.emplace(temp_stack.top().val, temp_stack.top().val);
                temp_stack.pop();
                while(!temp_stack.empty()){
                    T s = op(temp_stack.top().val, front_stack.top().sum);
                    front_stack.emplace(temp_stack.top().val, s);
                    temp_stack.pop();
                }
            }
        }
        back_stack.pop();
    }
};
#line 6 "test/library_checker/data_structure/deque_operate_all_composite.test.cpp"

using namespace std;

using mint = ModInt<998244353>;

using T = pair<mint, mint>;
T op(T x1, T x2){
    return {x1.first * x2.first, x2.first * x1.second + x2.second};
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    DESWAG<T, op> 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{
            long long x; cin >> x;
            if(swag.empty()){
                cout << x << "\n";
                continue;
            }
            T k = swag.fold();
            mint ans = k.first * x + k.second;
            cout << ans << "\n";
        }
    }
}
Back to top page