sotanishy's code snippets for competitive programming
View the Project on GitHub sotanishy/cp-library-cpp
#define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite" #include "../../data-structure/foldable_deque.hpp" #include "../../math/modint.hpp" #include <bits/stdc++.h> 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; FoldableDeque<M> que; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 0) { int a, b; cin >> a >> b; que.push_front({a, b}); } else if (t == 1) { int a, b; cin >> a >> b; que.push_back({a, b}); } else if (t == 2) { que.pop_front(); } else if (t == 3) { que.pop_back(); } 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/deque_operate_all_composite.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/deque_operate_all_composite" #line 2 "data-structure/foldable_deque.hpp" #include <cassert> #include <stack> #include <utility> #include <vector> template <typename S> class FoldableDeque { using T = typename S::T; public: void push_front(const T& x) { if (front.empty()) front.emplace(x, x); else front.emplace(x, S::op(x, front.top().second)); } void push_back(const T& x) { if (back.empty()) back.emplace(x, x); else back.emplace(x, S::op(back.top().second, x)); } void pop_front() { assert(!empty()); if (front.empty()) { std::vector<T> xs; while (!back.empty()) { xs.push_back(back.top().first); back.pop(); } int n = xs.size(); for (int i = n / 2; i < n; ++i) push_front(xs[i]); for (int i = n / 2 - 1; i >= 0; --i) push_back(xs[i]); } front.pop(); } void pop_back() { assert(!empty()); if (back.empty()) { std::vector<T> xs; while (!front.empty()) { xs.push_back(front.top().first); front.pop(); } int n = xs.size(); for (int i = n / 2 - 1; i >= 0; --i) push_front(xs[i]); for (int i = n / 2; i < n; ++i) push_back(xs[i]); } back.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 2 "math/modint.hpp" #include <algorithm> #include <iostream> /** * @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 5 "test/yosupo/deque_operate_all_composite.test.cpp" #include <bits/stdc++.h> 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; FoldableDeque<M> que; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 0) { int a, b; cin >> a >> b; que.push_front({a, b}); } else if (t == 1) { int a, b; cin >> a >> b; que.push_back({a, b}); } else if (t == 2) { que.pop_front(); } else if (t == 3) { que.pop_back(); } else { int x; cin >> x; pair<mint, mint> p = que.empty() ? make_pair(1, 0) : que.fold(); cout << p.first * x + p.second << "\n"; } } }