This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/queue_operate_all_composite
#include "ds/deque_agg.hpp"
#include <mod/modint.hpp>
using mint = atcoder::modint998244353;
struct affine {
mint a = 1, b = 0;
affine then(affine rhs) const {
return {a * rhs.a, b * rhs.a + rhs.b};
}
};
int main() {
int q; scanf("%d", &q);
monoid compose(CONST(affine{}), [&](auto&& a, auto&& b) { return a.then(b); });
deque_aggregation deq(compose);
while (q--) {
int cmd; scanf("%d", &cmd);
if (cmd == 0) {
int a, b; scanf("%d%d", &a, &b);
deq.push_back(affine{mint(a), mint(b)});
} else if (cmd == 1) {
deq.pop_front();
} else {
int x; scanf("%d", &x);
auto [a, b] = deq.prod();
printf("%d\n", (a*x+b).val());
}
}
}
#line 1 "test/ds/deque_agg.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/queue_operate_all_composite
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "algebra.hpp"
#define CONST(val) [=] { return val; }
#define WRAP_FN(func) \
[](auto&&... args) { return func(forward<decltype(args)>(args)...); }
template <class Unit, class Op>
struct monoid : private Unit, private Op {
using type = decltype(declval<Unit>()());
monoid(Unit unit, Op op) : Unit(unit), Op(op) {}
type unit() const { return Unit::operator()(); }
type op(type a, type b) const { return Op::operator()(a, b); }
};
template <class Unit, class Op, class Inv>
struct group : monoid<Unit, Op>, private Inv {
using type = typename monoid<Unit, Op>::type;
group(Unit unit, Op op, Inv inv) : monoid<Unit, Op>(unit, op), Inv(inv) {}
type inv(type a) const { return Inv::operator()(a); }
};
template <class T>
struct addition {
using type = T;
type unit() const { return 0; }
type op(type a, type b) const { return a + b; }
type inv(type a) const { return -a; }
};
template <class T>
struct maximum {
using type = T;
type unit() const { return numeric_limits<T>::min(); }
type op(type a, type b) const { return a > b ? a : b; }
};
template <class T>
struct minimum {
using type = T;
type unit() const { return numeric_limits<T>::max(); }
type op(type a, type b) const { return a > b ? b : a; }
};
template <class T, T nul = -1>
struct assign {
using type = T;
type unit() const { return nul; }
type op(type a, type b) const { return b == nul ? a : b; }
};
#line 3 "ds/deque_agg.hpp"
template <class M>
class deque_aggregation {
public:
using value_type = typename M::type;
deque_aggregation(M m = M())
: m(m), front_cuml({m.unit()}), back_cuml({m.unit()}) {}
void push_back(value_type v) {
back_cuml.push_back(m.op(back_cuml.back(), v));
back.push_back(v);
}
void push_front(value_type v) {
front_cuml.push_back(m.op(v, front_cuml.back()));
front.push_back(v);
}
value_type pop_front() {
if (front.empty()) prepare_front();
value_type res = front.back();
front.pop_back(), front_cuml.pop_back();
return res;
}
value_type pop_back() {
if (back.empty()) prepare_back();
value_type res = back.back();
back.pop_back(), back_cuml.pop_back();
return res;
}
value_type prod() const { return m.op(front_cuml.back(), back_cuml.back()); }
private:
M m;
vector<value_type> front, front_cuml, back, back_cuml;
void balance(int n, int m, vector<value_type> data) {
front.clear(), front_cuml.resize(1), back.clear(), back_cuml.resize(1);
repr(i, n) push_front(data[i]);
rep(i, m) push_back(data[n + i]);
}
void prepare_front() {
int n = (back.size() + 1) / 2;
assert(n != 0);
balance(n, back.size() - n, back);
}
void prepare_back() {
int m = (front.size() + 1) / 2;
assert(m != 0);
reverse(all(front));
balance(front.size() - m, m, front);
}
};
#line 4 "test/ds/deque_agg.test.cpp"
#include <mod/modint.hpp>
using mint = atcoder::modint998244353;
struct affine {
mint a = 1, b = 0;
affine then(affine rhs) const {
return {a * rhs.a, b * rhs.a + rhs.b};
}
};
int main() {
int q; scanf("%d", &q);
monoid compose(CONST(affine{}), [&](auto&& a, auto&& b) { return a.then(b); });
deque_aggregation deq(compose);
while (q--) {
int cmd; scanf("%d", &cmd);
if (cmd == 0) {
int a, b; scanf("%d%d", &a, &b);
deq.push_back(affine{mint(a), mint(b)});
} else if (cmd == 1) {
deq.pop_front();
} else {
int x; scanf("%d", &x);
auto [a, b] = deq.prod();
printf("%d\n", (a*x+b).val());
}
}
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-12 | example_00 |
![]() |
12 ms | 8 MB |
g++-12 | example_01 |
![]() |
12 ms | 8 MB |
g++-12 | large_max_00 |
![]() |
195 ms | 24 MB |
g++-12 | large_max_01 |
![]() |
198 ms | 24 MB |
g++-12 | large_min_00 |
![]() |
196 ms | 13 MB |
g++-12 | large_min_01 |
![]() |
197 ms | 13 MB |
g++-12 | large_triangle_00 |
![]() |
177 ms | 14 MB |
g++-12 | large_triangle_01 |
![]() |
177 ms | 14 MB |
g++-12 | max_random_00 |
![]() |
209 ms | 25 MB |
g++-12 | max_random_01 |
![]() |
208 ms | 25 MB |
g++-12 | max_random_02 |
![]() |
207 ms | 25 MB |
g++-12 | random_00 |
![]() |
151 ms | 16 MB |
g++-12 | random_01 |
![]() |
178 ms | 17 MB |
g++-12 | random_02 |
![]() |
34 ms | 9 MB |
g++-12 | small_00 |
![]() |
12 ms | 8 MB |
g++-12 | small_01 |
![]() |
12 ms | 8 MB |
g++-12 | small_02 |
![]() |
12 ms | 8 MB |
g++-12 | small_03 |
![]() |
12 ms | 8 MB |
g++-12 | small_04 |
![]() |
12 ms | 8 MB |