Submit Info #44160

Problem Lang User Status Time Memory
Queue Operate All Composite cpp Forested AC 345 ms 4.82 MiB

ケース詳細
Name Status Time Memory
example_00 AC 1 ms 0.61 MiB
example_01 AC 1 ms 0.66 MiB
large_max_00 AC 287 ms 3.50 MiB
large_max_01 AC 288 ms 3.55 MiB
large_min_00 AC 277 ms 2.14 MiB
large_min_01 AC 278 ms 2.21 MiB
large_triangle_00 AC 282 ms 2.54 MiB
large_triangle_01 AC 278 ms 2.47 MiB
max_random_00 AC 342 ms 4.82 MiB
max_random_01 AC 345 ms 4.79 MiB
max_random_02 AC 343 ms 4.79 MiB
random_00 AC 221 ms 2.33 MiB
random_01 AC 262 ms 2.75 MiB
random_02 AC 33 ms 0.91 MiB
small_00 AC 1 ms 0.60 MiB
small_01 AC 2 ms 0.68 MiB
small_02 AC 1 ms 0.64 MiB
small_03 AC 1 ms 0.61 MiB
small_04 AC 1 ms 0.61 MiB

// Sliding Window Aggregation #include <stack> #include <utility> #include <cassert> template <typename Operator> class SlidingWindowAggregation { public: using ValueType = typename Operator::ValueType; private: std::stack<ValueType> st0; std::stack<ValueType> st1; ValueType st1sum; public: SlidingWindowAggregation() : st1sum(Operator::identity()) {} bool empty() const { return st0.empty() && st1.empty(); } std::size_t size() const { return st0.size() + st1.size(); } void push(ValueType val) { st1.push(val); st1sum = Operator::operation(st1sum, val); } void pop() { assert(!empty()); if (st0.empty()) { { ValueType val = st1.top(); st1.pop(); st0.push(val); } while (!st1.empty()) { ValueType val = st1.top(); st1.pop(); st0.emplace(Operator::operation(val, st0.top())); } st1sum = Operator::identity(); } st0.pop(); } ValueType prod() const { return Operator::operation((st0.empty() ? Operator::identity() : st0.top()), st1sum); } }; // Mod-Int #include <iostream> template <unsigned int mod> struct ModInt { static_assert(mod >= 1 && mod < (1u << 31)); unsigned int val; ModInt() : val(0) {} ModInt(long long x) : val(x < 0 ? x % mod + mod : (unsigned long long)x % mod) {} ModInt<mod> &operator+=(const ModInt<mod> &x) { val += x.val; if (val >= mod) val -= mod; return *this; } ModInt<mod> &operator-=(const ModInt<mod> &x) { if (val < x.val) val += mod; val -= x.val; return *this; } ModInt<mod> &operator*=(const ModInt<mod> &x) { unsigned long long s = (unsigned long long)val * x.val; val = (unsigned int)(s % mod); return *this; } ModInt<mod> operator+() const { return *this; } ModInt<mod> operator-() const { return ModInt<mod>(0u) - *this; } friend ModInt<mod> operator+(const ModInt<mod> &x, const ModInt<mod> &y) { return ModInt<mod>(x) += y; } friend ModInt<mod> operator-(const ModInt<mod> &x, const ModInt<mod> &y) { return ModInt<mod>(x) -= y; } friend ModInt<mod> operator*(const ModInt<mod> &x, const ModInt<mod> &y) { return ModInt<mod>(x) *= y; } friend std::istream &operator>>(std::istream &is, ModInt<mod> &x) { is >> x.val; // x.value %= mod; return is; } friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &x) { os << x.val; return os; } ModInt<mod> pow(unsigned long long x) const { ModInt<mod> res(1), s(*this); while (x) { if (x & 1) res *= s; s *= s; x >>= 1; } return res; } // --- prime number --- ModInt<mod> &operator/=(const ModInt<mod> &x) { *this *= x.pow(mod - 2); return *this; } friend ModInt<mod> operator/(const ModInt<mod> &x, const ModInt<mod> &y) { return ModInt<mod>(x) /= y; } }; constexpr unsigned mod = 998244353; using Mint = ModInt<mod>; struct F { Mint a; Mint b; Mint operator()(Mint x) { return a * x + b; } }; struct FF { using ValueType = F; static ValueType identity() { return F {Mint(1), Mint(0)}; } static ValueType operation(const ValueType &lhs, const ValueType &rhs) { return F {lhs.a * rhs.a, lhs.b * rhs.a + rhs.b}; } }; int main() { SlidingWindowAggregation<FF> swag; size_t q; std::cin >> q; while (q--) { size_t type; std::cin >> type; if (type == 0) { Mint a, b; std::cin >> a >> b; swag.push(F {a, b}); } else if (type == 1) { swag.pop(); } else { Mint x; std::cin >> x; std::cout << swag.prod()(x) << '\n'; } } }