This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"
#include "../container/queue_aggregation.cpp"
#include <iostream>
#include "../math/static_modint.cpp"
#include "../utility/int_alias.cpp"
#include "../traits/affine_composite_monoid.cpp"
using Fp = Modint998244353;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int Q;
std::cin >> Q;
QueueAggregation<AffineCompositeMonoid<Fp>> que;
while (Q--) {
int t;
std::cin >> t;
if (t == 0) {
u32 a, b;
std::cin >> a >> b;
que.push(Affine<Fp>(Fp(a), Fp(b)));
} else if (t == 1) {
que.pop();
} else {
u32 x;
std::cin >> x;
std::cout << que.fold().eval(Fp(x)) << '\n';
}
}
}
#line 1 "test/queue_aggregation.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"
#line 2 "traits/reversed_monoid.cpp"
template <class M> struct ReversedMonoid {
using Type = typename M::Type;
static constexpr Type identity() { return M::identity(); }
static constexpr Type operation(const Type& l, const Type& r) { return M::operation(r, l); }
};
#line 2 "container/stack_aggregation.cpp"
#include <cassert>
#include <vector>
template <class M> class StackAggregation {
using T = typename M::Type;
struct Node {
T value, fold;
explicit Node(const T value, const T fold) : value(value), fold(fold) {}
};
std::vector<Node> st;
public:
StackAggregation() = default;
bool empty() const { return st.empty(); }
T top() const {
assert(!empty());
return st.back().value;
}
T fold() const { return st.empty() ? M::identity() : st.back().fold; }
void push(const T& x) { st.emplace_back(x, M::operation(fold(), x)); }
void pop() {
assert(!empty());
st.pop_back();
}
};
#line 4 "container/queue_aggregation.cpp"
template <class M> class QueueAggregation {
using T = typename M::Type;
using R = ReversedMonoid<M>;
StackAggregation<R> front_st;
StackAggregation<M> back_st;
public:
QueueAggregation() = default;
bool empty() const { return front_st.empty(); }
T fold() const { return M::operation(front_st.fold(), back_st.fold()); }
void push(const T& x) {
if (empty())
front_st.push(x);
else
back_st.push(x);
}
void pop() {
assert(!empty());
front_st.pop();
if (front_st.empty()) {
while (!back_st.empty()) {
front_st.push(back_st.top());
back_st.pop();
}
}
}
};
#line 3 "test/queue_aggregation.test.cpp"
#include <iostream>
#line 2 "math/static_modint.cpp"
#include <ostream>
#include <type_traits>
#line 2 "utility/int_alias.cpp"
#include <cstdint>
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;
#line 3 "math/rem_euclid.cpp"
template <class T> constexpr T rem_euclid(T value, const T& mod) {
assert(mod > 0);
return (value %= mod) >= 0 ? value : value + mod;
}
#line 2 "math/totient.cpp"
template <class T> constexpr T totient(T x) {
T ret = x;
for (T i = 2; i * i <= x; ++i) {
if (x % i == 0) {
ret /= i;
ret *= i - 1;
while (x % i == 0) x /= i;
}
}
if (x > 1) {
ret /= x;
ret *= x - 1;
}
return ret;
}
#line 7 "math/static_modint.cpp"
template <u32 MOD, std::enable_if_t<((u32)1 <= MOD and MOD <= ((u32)1 << 31))>* = nullptr> class StaticModint {
using Self = StaticModint;
static inline constexpr u32 PHI = totient(MOD);
u32 v;
public:
static constexpr u32 mod() noexcept { return MOD; }
template <class T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
static constexpr T normalize(const T& x) noexcept {
return rem_euclid<std::common_type_t<T, i64>>(x, MOD);
}
constexpr StaticModint() noexcept : v(0) {}
template <class T> constexpr StaticModint(const T& x) noexcept : v(normalize(x)) {}
template <class T> static constexpr Self raw(const T& x) noexcept {
Self ret;
ret.v = x;
return ret;
}
constexpr u32 val() const noexcept { return v; }
constexpr Self neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
constexpr Self inv() const noexcept { return pow(PHI - 1); }
constexpr Self pow(u64 exp) const noexcept {
Self ret(1), mult(*this);
for (; exp > 0; exp >>= 1) {
if (exp & 1) ret *= mult;
mult *= mult;
}
return ret;
}
constexpr Self operator-() const noexcept { return neg(); }
constexpr Self operator~() const noexcept { return inv(); }
constexpr Self operator+(const Self& rhs) const noexcept { return Self(*this) += rhs; }
constexpr Self& operator+=(const Self& rhs) noexcept {
if ((v += rhs.v) >= MOD) v -= MOD;
return *this;
}
constexpr Self operator-(const Self& rhs) const noexcept { return Self(*this) -= rhs; }
constexpr Self& operator-=(const Self& rhs) noexcept {
if (v < rhs.v) v += MOD;
v -= rhs.v;
return *this;
}
constexpr Self operator*(const Self& rhs) const noexcept { return Self(*this) *= rhs; }
constexpr Self& operator*=(const Self& rhs) noexcept {
v = (u64)v * rhs.v % MOD;
return *this;
}
constexpr Self operator/(const Self& rhs) const noexcept { return Self(*this) /= rhs; }
constexpr Self& operator/=(const Self& rhs) noexcept { return *this *= rhs.inv(); }
constexpr bool operator==(const Self& rhs) const noexcept { return v == rhs.v; }
constexpr bool operator!=(const Self& rhs) const noexcept { return v != rhs.v; }
friend std::ostream& operator<<(std::ostream& stream, const Self& rhs) { return stream << rhs.v; }
};
using Modint1000000007 = StaticModint<1000000007>;
using Modint998244353 = StaticModint<998244353>;
#line 2 "traits/affine_composite_monoid.cpp"
template <class T> struct Affine {
T a, b;
constexpr Affine(const T& a = 1, const T& b = 0) : a(a), b(b) {}
constexpr T eval(const T& x) const { return a * x + b; }
constexpr Affine operator+(const Affine& other) const { return affine(a + other.a, b + other.b); }
constexpr Affine composite(const Affine& other) const { return Affine(a * other.a, b * other.a + other.b); }
};
template <class T> struct AffineCompositeMonoid {
using Type = Affine<T>;
static constexpr Type identity() { return Type(); }
static constexpr Type operation(const Type& l, const Type& r) noexcept { return l.composite(r); }
};
#line 7 "test/queue_aggregation.test.cpp"
using Fp = Modint998244353;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int Q;
std::cin >> Q;
QueueAggregation<AffineCompositeMonoid<Fp>> que;
while (Q--) {
int t;
std::cin >> t;
if (t == 0) {
u32 a, b;
std::cin >> a >> b;
que.push(Affine<Fp>(Fp(a), Fp(b)));
} else if (t == 1) {
que.pop();
} else {
u32 x;
std::cin >> x;
std::cout << que.fold().eval(Fp(x)) << '\n';
}
}
}