Approach
Stack-based processing
For Queue Operate All Composite, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 33 lines of C++ from the credited upstream file yosupo-swag.test.cpp.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1#define PROBLEM "https://judge.yosupo.jp/problem/queue_operate_all_composite"2 3#include "../../template/template.hpp"4#include "../../data-structure/slide-window-aggregation.hpp"5#include "../../misc/fastio.hpp"6#include "../../modint/montgomery-modint.hpp"7 8using namespace Nyaan; void Nyaan::solve() {9 using mint = LazyMontgomeryModInt<998244353>;10 using p = pair<mint, mint>;11 auto f = [](const p &a, const p &b) {12 return p{a.first * b.first, a.second * b.first + b.second};13 };14 SlideWindowAggregation<p, decltype(f)> swag(f, p{1, 0});15 int Q;16 rd(Q);17 rep(_, Q) {18 int cmd;19 rd(cmd);20 if (cmd == 0) {21 int a, b;22 rd(a, b);23 swag.push(p{a, b});24 } else if (cmd == 1) {25 swag.pop();26 } else {27 int x;28 rd(x);29 p q = swag.query();30 wtn((q.first * x + q.second).get());31 }32 }33}