This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite" #include <iostream> #include "../../data_structure/queue_aggregation.hpp" #include "../../modint/modint.hpp" using mint = ebi::modint998244353; struct F { mint a, b; F(mint a, mint b) : a(a), b(b) {} }; F op(F f1, F f2) { return F(f2.a * f1.a, f2.a * f1.b + f2.b); } int main() { ebi::queue_aggregation<F, op> swag; int q; std::cin >> q; while (q--) { int t; std::cin >> t; if (t == 0) { int a, b; std::cin >> a >> b; swag.push(F(a, b)); } else if (t == 1) { swag.pop(); } else { int x; std::cin >> x; if (swag.empty()) { std::cout << x << std::endl; continue; } auto f = swag.fold_all(); std::cout << (f.a * (mint)x + f.b).val() << std::endl; } } }
#line 1 "test/data_structure/Queue_Operate_All_Composite.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite" #include <iostream> #line 2 "data_structure/queue_aggregation.hpp" /* reference: https://scrapbox.io/data-structures/Sliding_Window_Aggregation */ #include <cassert> #include <stack> namespace ebi { template <class Semigroup, Semigroup (*op)(Semigroup, Semigroup)> struct queue_aggregation { private: struct Node { Semigroup val; Semigroup fold; }; void move() { assert(!_back.empty()); Node p = _back.top(); _back.pop(); _front.push({p.val, p.val}); while (!_back.empty()) { Semigroup x = _back.top().val; _back.pop(); _front.push({x, op(x, _front.top().fold)}); } } public: queue_aggregation() {} int size() { return _front.size() + _back.size(); } bool empty() { return size() == 0; } void push(Semigroup x) { Node node = {x, x}; if (!_back.empty()) { node.fold = op(_back.top().fold, x); } _back.push(node); } Semigroup front() { assert(!empty()); if (_front.empty()) { move(); } return _front.top().val; } void pop() { assert(!empty()); if (_front.empty()) { move(); } _front.pop(); } void clear() { _front = std::stack<Node>(); _back = std::stack<Node>(); } Semigroup fold_all() { assert(!empty()); if (_front.empty()) { return _back.top().fold; } else if (_back.empty()) { return _front.top().fold; } else { return op(_front.top().fold, _back.top().fold); } } private: std::stack<Node> _front, _back; }; } // namespace ebi #line 2 "modint/modint.hpp" #line 5 "modint/modint.hpp" #line 2 "modint/base.hpp" #include <concepts> #line 5 "modint/base.hpp" #include <utility> namespace ebi { template <class T> concept Modint = requires(T a, T b) { a + b; a - b; a * b; a / b; a.inv(); a.val(); a.pow(std::declval<long long>()); T::mod(); }; template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) { long long x; os >> x; a = x; return os; } template <Modint mint> std::ostream &operator<<(std::ostream &os, const mint &a) { return os << a.val(); } } // namespace ebi #line 7 "modint/modint.hpp" namespace ebi { template <int m> struct static_modint { private: using modint = static_modint; public: static constexpr int mod() { return m; } static constexpr modint raw(int v) { modint x; x._v = v; return x; } constexpr static_modint() : _v(0) {} constexpr static_modint(long long v) { v %= (long long)umod(); if (v < 0) v += (long long)umod(); _v = (unsigned int)v; } constexpr unsigned int val() const { return _v; } constexpr unsigned int value() const { return val(); } constexpr modint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } constexpr modint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } constexpr modint operator++(int) { modint res = *this; ++*this; return res; } constexpr modint operator--(int) { modint res = *this; --*this; return res; } constexpr modint &operator+=(const modint &rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } constexpr modint &operator-=(const modint &rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } constexpr modint &operator*=(const modint &rhs) { unsigned long long x = _v; x *= rhs._v; _v = (unsigned int)(x % (unsigned long long)umod()); return *this; } constexpr modint &operator/=(const modint &rhs) { return *this = *this * rhs.inv(); } constexpr modint operator+() const { return *this; } constexpr modint operator-() const { return modint() - *this; } constexpr modint pow(long long n) const { assert(0 <= n); modint x = *this, res = 1; while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr modint inv() const { assert(_v); return pow(umod() - 2); } friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += rhs; } friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= rhs; } friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= rhs; } friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= rhs; } friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.val() == rhs.val(); } friend bool operator!=(const modint &lhs, const modint &rhs) { return !(lhs == rhs); } private: unsigned int _v = 0; static constexpr unsigned int umod() { return m; } }; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; } // namespace ebi #line 7 "test/data_structure/Queue_Operate_All_Composite.test.cpp" using mint = ebi::modint998244353; struct F { mint a, b; F(mint a, mint b) : a(a), b(b) {} }; F op(F f1, F f2) { return F(f2.a * f1.a, f2.a * f1.b + f2.b); } int main() { ebi::queue_aggregation<F, op> swag; int q; std::cin >> q; while (q--) { int t; std::cin >> t; if (t == 0) { int a, b; std::cin >> a >> b; swag.push(F(a, b)); } else if (t == 1) { swag.pop(); } else { int x; std::cin >> x; if (swag.empty()) { std::cout << x << std::endl; continue; } auto f = swag.fold_all(); std::cout << (f.a * (mint)x + f.b).val() << std::endl; } } }