#ifndef call_from_test #include using namespace std; #define call_from_test #ifndef call_from_test #include using namespace std; #endif //BEGIN CUT HERE template struct LinkCutTreeBase{ static array pool; size_t ptr; LinkCutTreeBase():ptr(0){} inline Node* create(){ return &pool[ptr++]; } inline Node* create(Node v){ return &(pool[ptr++]=v); } inline size_t idx(Node *t){ return t-&pool[0]; } Node* operator[](size_t k){ return &(pool[k]); } virtual void toggle(Node *t) = 0; virtual Node* eval(Node *t) = 0; virtual void pushup(Node *t) = 0; void rotR(Node *t){ Node *x=t->p,*y=x->p; if((x->l=t->r)) t->r->p=x; t->r=x;x->p=t; pushup(x);pushup(t); if((t->p=y)){ if(y->l==x) y->l=t; if(y->r==x) y->r=t; pushup(y); } } void rotL(Node *t){ Node *x=t->p,*y=x->p; if((x->r=t->l)) t->l->p=x; t->l=x;x->p=t; pushup(x);pushup(t); if((t->p=y)){ if(y->l==x) y->l=t; if(y->r==x) y->r=t; pushup(y); } } // for splay tree (not original tree) bool is_root(Node *t){ return !t->p||(t->p->l!=t&&t->p->r!=t); } void splay(Node *t){ eval(t); while(!is_root(t)){ Node *q=t->p; if(is_root(q)){ eval(q);eval(t); if(q->l==t) rotR(t); else rotL(t); }else{ auto *r=q->p; eval(r);eval(q);eval(t); if(r->l==q){ if(q->l==t) rotR(q),rotR(t); else rotL(t),rotR(t); }else{ if(q->r==t) rotL(q),rotL(t); else rotR(t),rotL(t); } } } } virtual Node* expose(Node *t){ Node *rp=nullptr; for(Node *c=t;c;c=c->p){ splay(c); c->r=rp; pushup(c); rp=c; } splay(t); return rp; } void link(Node *par,Node *c){ expose(c); expose(par); c->p=par; par->r=c; pushup(par); } void cut(Node *c){ expose(c); Node *par=c->l; c->l=nullptr; pushup(c); par->p=nullptr; } void evert(Node *t){ expose(t); toggle(t); eval(t); } Node *parent(Node *t){ expose(t); if(!(t->l)) return nullptr; t=eval(t->l); while(t->r) t=eval(t->r); splay(t); return t; } Node *root(Node *t){ expose(t); while(t->l) t=eval(t->l); splay(t); return t; } bool is_connected(Node *a,Node *b){ return root(a)==root(b); } Node *lca(Node *a,Node *b){ expose(a); return expose(b); } }; template array LinkCutTreeBase::pool; //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #undef call_from_test #endif //BEGIN CUT HERE template struct NodeBase{ using T = Tp; using E = Ep; NodeBase *l,*r,*p; bool rev; T val,dat; E laz; NodeBase(){} NodeBase(T val,E laz): rev(0),val(val),dat(val),laz(laz){ l=r=p=nullptr;} }; template struct Path : LinkCutTreeBase{ using super = LinkCutTreeBase; using Node = Np; using T = typename Node::T; using E = typename Node::E; using F = function; using G = function; using H = function; using S = function; F f; G g; H h; S flip; E ei; Path(F f,G g,H h,E ei): super(),f(f),g(g),h(h),ei(ei){ flip=[](T a){return a;}; } Path(F f,G g,H h,S flip,E ei): super(),f(f),g(g),h(h),flip(flip),ei(ei){} Node* create(T val){ return super::create(Node(val,ei)); } inline void propagate(Node *t,E v){ t->laz=h(t->laz,v); t->val=g(t->val,v); t->dat=g(t->dat,v); } inline void toggle(Node *t){ swap(t->l,t->r); t->dat=flip(t->dat); t->rev^=1; } inline Node* eval(Node *t){ if(t->laz!=ei){ if(t->l) propagate(t->l,t->laz); if(t->r) propagate(t->r,t->laz); t->laz=ei; } if(t->rev){ if(t->l) toggle(t->l); if(t->r) toggle(t->r); t->rev=false; } return t; } inline void pushup(Node *t){ t->dat=t->val; if(t->l) t->dat=f(t->l->dat,t->dat); if(t->r) t->dat=f(t->dat,t->r->dat); } using super::expose; T query(Node *t){ expose(t); return t->dat; } void update(Node *t,E v){ expose(t); propagate(t,v); eval(t); } }; //END CUT HERE #ifndef call_from_test #define call_from_test #ifndef call_from_test #include using namespace std; #endif //BEGIN CUT HERE struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include using namespace std; #endif //BEGIN CUT HERE template struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v constexpr T Mint::mod; template ostream& operator<<(ostream &os,Mint m){os<>h>>w>>k; using M = Mint; M ans{0}; for(int d=1;d using namespace std; #endif //BEGIN CUT HERE template struct SquareMatrix{ typedef array arr; typedef array mat; mat dat; SquareMatrix(){ for(size_t i=0;i>=1; } return res; } }; //END CUT HERE #ifndef call_from_test #define call_from_test #include "../tools/fastio.cpp" #include "../tools/compress.cpp" #include "../mod/mint.cpp" #include "../segtree/basic/ushi.cpp" #undef call_from_test //INSERT ABOVE HERE signed YAHOO2019_FINAL_D(){ using ll = long long; using M = Mint; using MM = SquareMatrix; int n,q; cin>>n>>q; vector ts(q),ls(q),rs(q),ps(q); for(ll i=0;i>ts[i]; if(ts[i]==1) cin>>ps[i]; if(ts[i]==2) cin>>ls[i]>>rs[i]; } vector vs; for(ll i=0;i vt(vs.size()-1,A); for(ll i=0;i+1<(ll)vs.size();i++){ vt[i]=A.pow(vs[i+1]-vs[i]); } MM I=MM::mul_identity(); auto f=[&](MM a,MM b){return b*a;}; SegmentTree seg(f,I); seg.build(vt); vector used(vs.size(),0); for(ll i=0;i>s; struct M{ uint32_t v; M(){*this=add_identity();} M(uint32_t v):v(v){} M operator+(const M &a)const{return M(v+a.v);} M operator*(const M &a)const{return M(v*a.v);} static M add_identity(){return M(0);} static M mul_identity(){return M(1);} }; using SM = SquareMatrix; auto f=[](SM a,SM b){return a*b;}; SM ti=SM::mul_identity(); SegmentTree seg(f,ti); vector vt(s.size(),ti); for(int i=0;i<(int)s.size();i++){ if(s[i]=='D') vt[i][0][1]=1; if(s[i]=='I') vt[i][1][2]=1; if(s[i]=='S') vt[i][2][3]=1; if(s[i]=='C') vt[i][3][4]=1; if(s[i]=='O') vt[i][4][5]=1; } seg.build(vt); int q; cin>>q; for(int i=0;i>l>>r; l--; cout<; using SM = SquareMatrix; using SM2 = pair; using Node = NodeBase; constexpr size_t LIM = 1e6; using LCT = Path; auto f=[](SM2 x,SM2 y){return SM2(x.first*y.first,y.second*x.second);}; auto g=[](SM2 x,SM2 y){(void)x;return y;}; auto flip=[](SM2 x){swap(x.first,x.second);return x;}; SM ti=SM::mul_identity(); SM ei=SM::mul_identity(); SM2 ti2(ti,ti),ei2(ei,ei); LCT lct(f,g,g,flip,ei2); int n; cin>>n; vector< vector > G(n); vector X,Y; for(int i=1;i>a>>b; X.emplace_back(a); Y.emplace_back(b); G[a].emplace_back(b); G[b].emplace_back(a); } for(int i=0;i > rev(n); int idx=n; { using P = pair; queue

q; q.emplace(0,-1); while(!q.empty()){ int v,p; tie(v,p)=q.front();q.pop(); if(~p){ lct.link(lct[p],lct[idx]); lct.link(lct[idx],lct[v]); rev[p][v]=rev[v][p]=idx++; } for(int u:G[v]) if(u!=p) q.emplace(u,v); } } int q; cin>>q; for(int i=0;i>c; if(c=='x'){ int v,a,b,c,d; cin>>v>>a>>b>>c>>d; int z=rev[X[v]][Y[v]]; lct.expose(lct[z]); SM sm; sm[0][0]=a;sm[0][1]=b; sm[1][0]=c;sm[1][1]=d; lct[z]->val=SM2(sm,sm); lct.pushup(lct[z]); } if(c=='g'){ int x,y; cin>>x>>y; lct.evert(lct[x]); SM ans=lct.query(lct[y]).first; cout<>n>>m; using Node = NodeBase; constexpr size_t LIM = 1e6; using LCT = Path; auto f=[](int a,int b){return a+b;}; LCT lct(f,f,f,0); for(int i=0;i>s>>a>>b; a--;b--; if(s=="add"s){ lct.evert(lct[b]); lct.link(lct[a],lct[b]); } if(s=="rem"s){ auto v=lct.lca(lct[a],lct[b])==lct[a]?lct[b]:lct[a]; lct.cut(v); } if(s=="conn"s) cout<<(lct.is_connected(lct[a],lct[b])?"YES\n":"NO\n"); } cout<