Submit Info #44157

Problem Lang User Status Time Memory
Queue Operate All Composite cpp Forested AC 344 ms 5.47 MiB

ケース詳細
Name Status Time Memory
example_00 AC 2 ms 0.61 MiB
example_01 AC 1 ms 0.68 MiB
large_max_00 AC 318 ms 5.43 MiB
large_max_01 AC 289 ms 5.47 MiB
large_min_00 AC 275 ms 2.18 MiB
large_min_01 AC 276 ms 2.21 MiB
large_triangle_00 AC 278 ms 2.80 MiB
large_triangle_01 AC 273 ms 2.80 MiB
max_random_00 AC 344 ms 4.84 MiB
max_random_01 AC 344 ms 4.80 MiB
max_random_02 AC 344 ms 4.83 MiB
random_00 AC 223 ms 2.45 MiB
random_01 AC 266 ms 3.61 MiB
random_02 AC 33 ms 0.89 MiB
small_00 AC 1 ms 0.62 MiB
small_01 AC 1 ms 0.65 MiB
small_02 AC 1 ms 0.68 MiB
small_03 AC 1 ms 0.68 MiB
small_04 AC 1 ms 0.68 MiB

// Sliding Window Aggregation #include <stack> #include <utility> #include <cassert> template <typename Operator> class SlidingWindowAggregation { public: using ValueType = typename Operator::ValueType; private: std::stack<std::pair<ValueType, 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.emplace(val, val); } while (!st1.empty()) { ValueType val = st1.top(); st0.emplace(val, Operator::operation(val, st0.top().second)); st1.pop(); } st1sum = Operator::identity(); } st0.pop(); } ValueType prod() { ValueType ret = Operator::identity(); if (!st0.empty()) ret = Operator::operation(ret, st0.top().second); ret = Operator::operation(ret, st1sum); return ret; } }; // 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'; } } }