sotanishy's code snippets for competitive programming
View the Project on GitHub sotanishy/cp-library-cpp
#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite" #include <bits/stdc++.h> #include "../../data-structure/sliding_window_aggregation.hpp" #include "../../math/modint.hpp" using namespace std; using mint = Modint<998244353>; struct M { using T = pair<mint, mint>; static T op(T a, T b) { return {a.first * b.first, a.second * b.first + b.second}; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int Q; cin >> Q; SlidingWindowAggregation<M> que; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 0) { int a, b; cin >> a >> b; que.push({a, b}); } else if (t == 1) { que.pop(); } else { int x; cin >> x; pair<mint, mint> p = que.empty() ? make_pair(1, 0) : que.fold(); cout << p.first * x + p.second << "\n"; } } }
#line 1 "test/yosupo/queue_operate_all_composite.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite" #include <bits/stdc++.h> #line 5 "data-structure/sliding_window_aggregation.hpp" template <typename S> class SlidingWindowAggregation { using T = typename S::T; public: void push(const T& x) { if (back.empty()) back.emplace(x, x); else back.emplace(x, S::op(back.top().second, x)); } void pop() { assert(!empty()); if (front.empty()) { T x = back.top().first; back.pop(); front.emplace(x, x); while (!back.empty()) { x = back.top().first; back.pop(); front.emplace(x, S::op(x, front.top().second)); } } front.pop(); } bool empty() const { return front.empty() && back.empty(); } T fold() const { assert(!empty()); if (front.empty()) return back.top().second; if (back.empty()) return front.top().second; return S::op(front.top().second, back.top().second); } private: std::stack<std::pair<T, T>> front, back; }; #line 4 "math/modint.hpp" /** * @brief Mod int */ template <int m> class Modint { using mint = Modint; static_assert(m > 0, "Modulus must be positive"); public: static constexpr int mod() { return m; } constexpr Modint(long long y = 0) : x(y >= 0 ? y % m : (y % m + m) % m) {} constexpr int val() const { return x; } constexpr mint& operator+=(const mint& r) { if ((x += r.x) >= m) x -= m; return *this; } constexpr mint& operator-=(const mint& r) { if ((x += m - r.x) >= m) x -= m; return *this; } constexpr mint& operator*=(const mint& r) { x = static_cast<int>(1LL * x * r.x % m); return *this; } constexpr mint& operator/=(const mint& r) { return *this *= r.inv(); } constexpr bool operator==(const mint& r) const { return x == r.x; } constexpr mint operator+() const { return *this; } constexpr mint operator-() const { return mint(-x); } constexpr friend mint operator+(const mint& l, const mint& r) { return mint(l) += r; } constexpr friend mint operator-(const mint& l, const mint& r) { return mint(l) -= r; } constexpr friend mint operator*(const mint& l, const mint& r) { return mint(l) *= r; } constexpr friend mint operator/(const mint& l, const mint& r) { return mint(l) /= r; } constexpr mint inv() const { int a = x, b = m, u = 1, v = 0; while (b > 0) { int t = a / b; std::swap(a -= t * b, b); std::swap(u -= t * v, v); } return mint(u); } constexpr mint pow(long long n) const { mint ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend std::ostream& operator<<(std::ostream& os, const mint& r) { return os << r.x; } friend std::istream& operator>>(std::istream& is, mint& r) { long long t; is >> t; r = mint(t); return is; } private: int x; }; #line 7 "test/yosupo/queue_operate_all_composite.test.cpp" using namespace std; using mint = Modint<998244353>; struct M { using T = pair<mint, mint>; static T op(T a, T b) { return {a.first * b.first, a.second * b.first + b.second}; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int Q; cin >> Q; SlidingWindowAggregation<M> que; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 0) { int a, b; cin >> a >> b; que.push({a, b}); } else if (t == 1) { que.pop(); } else { int x; cin >> x; pair<mint, mint> p = que.empty() ? make_pair(1, 0) : que.fold(); cout << p.first * x + p.second << "\n"; } } }