#include using namespace std; #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for(ll i=0; i<(ll)(n); i++) template bool chmax(T& a, T b) { return a bool chmin(T& a, T b) { return a>b ? a=b, true : false; } using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using VI=vector; using VVI=vector; using VL=vector; using VVL=vector; using PL=pair; using VP=vector; using WG=vector>>; #ifdef LOCAL #include "./debug.hpp" #else #define debug(...) #define print_line #endif template vector> MatMul(const vector>& A, const vector>& B) { int N=A.size(); vector> ret(N,vector(N)); for(int i=0; i vector> MatPow(vector> A, ll b) { int N=A.size(); vector> ret(N,vector(N)); for(int i=0; i>=1; } return ret; } /// @brief 実行時に mod を変更できる ModInt struct ModIntDynamic { ll value; static ll mod; static void set_mod(ll x) { mod=x; } static ll get_mod() { return mod; } ModIntDynamic(ll x=0) { if(x>=0) value=x%mod; else value=mod-(-x)%mod; } ModIntDynamic operator-() const { return ModIntDynamic(-value); } ModIntDynamic operator+() const { return ModIntDynamic(*this); } ModIntDynamic& operator+=(const ModIntDynamic& other) { value+=other.value; if(value>=mod) value-=mod; return*this; } ModIntDynamic& operator-=(const ModIntDynamic& other) { value+=mod-other.value; if(value>=mod) value-=mod; return*this; } ModIntDynamic& operator*=(const ModIntDynamic other) { value=value*other.value%mod; return*this; } ModIntDynamic& operator/=(ModIntDynamic other) { (*this)*=other.inv(); return*this; } ModIntDynamic operator+(const ModIntDynamic& other) const { return ModIntDynamic(*this)+=other; } ModIntDynamic operator-(const ModIntDynamic& other) const { return ModIntDynamic(*this)-=other; } ModIntDynamic operator*(const ModIntDynamic& other) const { return ModIntDynamic(*this)*=other; } ModIntDynamic operator/(const ModIntDynamic& other) const { return ModIntDynamic(*this)/=other; } ModIntDynamic pow(ll x) const { ModIntDynamic ret(1),mul(value); while(x) { if(x&1) ret*=mul; mul*=mul; x>>=1; } return ret; } ModIntDynamic inv() const { return pow(mod-2); } bool operator==(const ModIntDynamic& other) const { return value==other.value; } bool operator!=(const ModIntDynamic& other) const { return value!=other.value; } friend ostream& operator<<(ostream& os, const ModIntDynamic& x) { return os<>(istream& is, ModIntDynamic& x) { ll v; is>>v; x=ModIntDynamic(v); return is; } }; ll ModIntDynamic::mod=998244353; /// @brief セグメント木 template struct SegTree { using Type=typename Monoid::Type; SegTree()=default; /// @brief 要素数 n のセグ木を構築する SegTree(int n) { this->n=n; dat=vector(n<<1,Monoid::id()); cand.reserve(100); cand_l.reserve(100); cand_r.reserve(100); } /// @brief 配列 v からセグ木を構築する /// @note O(N) SegTree(const vector& v) { this->n=v.size(); dat=vector(n<<1,Monoid::id()); for(int i=0; i0; i--) dat[i]=Monoid::op(dat[i<<1],dat[i<<1|1]); cand.reserve(100); cand_l.reserve(100); cand_r.reserve(100); } /// @brief i 番目の要素を x に変更する /// @note O(log(N)) void set(int i, Type x) { i+=n; dat[i]=x; while(i>>=1) dat[i]=Monoid::op(dat[i<<1],dat[i<<1|1]); } /// @brief 区間 [l, r) のモノイド積を返す /// @note O(log(N)) Type fold(int l, int r) { Type retl=Monoid::id(),retr=Monoid::id(); l+=n; r+=n; while(l>=1; r>>=1; } return Monoid::op(retl,retr); } /// @brief 区間 [l, x) のモノイド積が f を満たすような最大の x >= l を返す /// @attention `f(Monoid::id())=true` が成り立つ必要がある /// @note O(log(N)) template int find_right(int l, F f) { assert(f(Monoid::id())); if(l==n) return n; l+=n; int r=n+n; cand_l.clear(); cand_r.clear(); while(l>=1; r>>=1; } cand=cand_l; reverse(cand_r.begin(),cand_r.end()); cand.insert(cand.end(),cand_r.begin(),cand_r.end()); Type val=Monoid::id(); for(int i:cand) { if(f(Monoid::op(val,dat[i]))) { val=Monoid::op(val,dat[i]); } else { while(i int find_left(int r,F f) { assert(f(Monoid::id())); if(r==0) return 0; r+=n; int l=n; cand_l.clear(); cand_r.clear(); while(l>=1; r>>=1; } cand=cand_r; reverse(cand_l.begin(),cand_l.end()); cand.insert(cand.end(),cand_l.begin(),cand_l.end()); Type val=Monoid::id(); for(int i:cand) { if(f(Monoid::op(dat[i],val))) { val=Monoid::op(dat[i],val); } else { while(i dat; vector cand,cand_l,cand_r; }; /// @brief モノイド namespace Monoid { /// @brief Minモノイド /// @tparam max_value 単位元 template struct Min { using Type=T; static Type id() { return max_value; } static Type op(const Type& a, const Type& b) { return min(a,b); } }; /// @brief Maxモノイド /// @tparam min_value 単位元 template struct Max { using Type=T; static Type id() { return min_value; } static Type op(const Type& a, const Type& b) { return max(a,b); } }; /// @brief 和 template struct Sum { using Type=T; static Type id() { return 0; } static Type op(const Type& a, const Type& b) { return a+b; } }; /// @brief (和,区間の長さ) template struct SumPair { using Type=pair; static Type id() { return make_pair(T(0),0); } static Type op(const Type& a, const Type& b) { return {a.first+b.first,a.second+b.second}; } }; } /// @brief 区間クエリ namespace RangeQuery { /// @brief 1点変更 / 区間 min template struct Min { using Type=struct SegTree>; }; /// @brief 1点変更 / 区間 max template struct Max { using Type=struct SegTree>; }; /// @brief 1点変更 / 区間和 template struct Sum { using Type=struct SegTree>; }; } //---------------------------------------------------------- using MID=ModIntDynamic; struct Mon { using Type=vector>; static Type op(const Type& l, const Type& r) { return MatMul(l,r); } static Type id() { return {{1,0},{0,1}}; } }; void solve() { ll K,N; cin>>K>>N; MID::set_mod(K); vector>> init(N); REP(i,N) { init[i].resize(2); REP(j,2) { init[i][j].resize(2); REP(k,2) cin>>init[i][j][k]; } } auto seg=SegTree(init); ll Q; cin>>Q; while(Q--) { ll k,l,r; cin>>k>>l>>r; k--; l--; vector> y(2,vector(2)); REP(i,2) REP(j,2) cin>>y[i][j]; seg.set(k,y); auto res=seg.fold(l,r); REP(i,2) { REP(j,2) cout<>T; while(T--) solve(); }