library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_queue_operate_all_composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 998244353;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#include "../util/modint.cpp"

#include "../datastructure/swag.cpp"

struct SemiGroup {
    using T = pair<mint, mint>;
    static T f(T a, T b) { return {a.first*b.first, a.second*b.first + b.second}; }
    static T e() { return {1, 0}; }
};

int main() {
    int q;
    cin >> q;
    SWAG<SemiGroup> Q;
    while(q--){
        int no; scanf("%d", &no);
        if(no == 0){
            int a, b; scanf("%d %d", &a, &b);
            Q.push(make_pair(mint(a), mint(b)));
        }else if(no == 1){
            Q.pop();
        }else {
            int x; scanf("%d", &x);
            auto ret = Q.fold();
            printf("%d\n", (ret.first*mint(x) + ret.second).val);
        }
    }
    return 0;
}
#line 1 "test/yosupo_queue_operate_all_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 998244353;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#line 1 "util/modint.cpp"
template <uint M>
struct modint {
    uint val;
public:
    static modint raw(int v) { modint x; x.val = v; return x; }
    modint() : val(0) {}
    template <class T>
    modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = uint(x); }
    modint(bool v) { val = ((unsigned int)(v) % M); }
    modint& operator++() { val++; if (val == M) val = 0; return *this; }
    modint& operator--() { if (val == 0) val = M; val--; return *this; }
    modint operator++(int) { modint result = *this; ++*this; return result; }
    modint operator--(int) { modint result = *this; --*this; return result; }
    modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; }
    modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; }
    modint& operator*=(const modint& b) { ull z = val; z *= b.val; val = (uint)(z % M); return *this; }
    modint& operator/=(const modint& b) { return *this = *this * b.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint() - *this; }
    modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
    modint inv() const { return pow(M-2); }
    friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; }
    friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; }
    friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; }
    friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; }
    friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; }
    friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; }
};
using mint = modint<MOD>;

/**
 * @brief modint(固定MOD)
 * @docs _md/modint.md
 */
#line 22 "test/yosupo_queue_operate_all_composite.test.cpp"

#line 1 "datastructure/swag.cpp"
template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};
/*
struct Monoid {
    using T = int;
    static T f(T a, T b) { return a+b; }
    static T e() { return 0; }
};
*/
#line 24 "test/yosupo_queue_operate_all_composite.test.cpp"

struct SemiGroup {
    using T = pair<mint, mint>;
    static T f(T a, T b) { return {a.first*b.first, a.second*b.first + b.second}; }
    static T e() { return {1, 0}; }
};

int main() {
    int q;
    cin >> q;
    SWAG<SemiGroup> Q;
    while(q--){
        int no; scanf("%d", &no);
        if(no == 0){
            int a, b; scanf("%d %d", &a, &b);
            Q.push(make_pair(mint(a), mint(b)));
        }else if(no == 1){
            Q.pop();
        }else {
            int x; scanf("%d", &x);
            auto ret = Q.fold();
            printf("%d\n", (ret.first*mint(x) + ret.second).val);
        }
    }
    return 0;
}
Back to top page