exACL

· · 个人记录

\textbf{Segtree}

template<class S,S(*op)(S,S),S(*e)()>
struct segtree{
    ll n;
    vector<S>t;
    #define ls(p) (p*2)
    #define rs(p) (p*2+1)
    #define mid (l+r)/2
    void pushup(ll p){
        t[p]=op(t[ls(p)],t[rs(p)]);
    }
    void build(ll p,ll l,ll r,const vector<S>&v){
        if(l==r){
            t[p]=v[l];
            return;
        }
        build(ls(p),l,mid,v);
        build(rs(p),mid+1,r,v);
        pushup(p);
    }
    segtree(ll n):segtree(vector<S>(n,e())){}
    segtree(const vector<S>&v):n(v.size()){
        t.resize(n<<2);
        build(1,0,n-1,v);
    }
    void set(ll p,ll l,ll r,ll x,S k){
        if(l==r&&r==x){
            t[p]=k;
            return;
        }
        if(x<=mid)set(ls(p),l,mid,x,k);
        else set(rs(p),mid+1,r,x,k);
        pushup(p);
    }
    void set(ll x,S k){
        set(1,0,n-1,x,k);
    }
    S get(ll p,ll l,ll r,ll x){
        if(l==r&&r==x){
            return t[p];
        }
        if(x<=mid)return get(ls(p),l,mid,x);
        else return get(rs(p),mid+1,r,x);
    }
    S get(ll x){
        return get(1,0,n-1,x);
    }
    S prod(ll p,ll l,ll r,ll ql,ll qr){
        if(ql<=l&&r<=qr){
            return t[p];
        }
        if(qr<=mid)return prod(ls(p),l,mid,ql,qr);
        if(ql>mid)return prod(rs(p),mid+1,r,ql,qr);
        return op(prod(ls(p),l,mid,ql,qr),prod(rs(p),mid+1,r,ql,qr));
    }
    S prod(ll l,ll r){
        return prod(1,0,n-1,l,r-1);
    }
    #undef ls
    #undef rs
    #undef mid
};

\textbf{Lazy Segtree}

template<class S,S(*op)(S,S),S(*e)(),class F,S(*mapping)(F,S),F(*composition)(F,F),F(*id)()>
struct lazy_segtree{
    ll n;
    vector<S>t;
    vector<F>lz;
    #define ls(p) (p*2)
    #define rs(p) (p*2+1)
    #define mid (l+r)/2
    void pushup(ll p){
        t[p]=op(t[ls(p)],t[rs(p)]);
    }
    void all_apply(ll p,F f,bool leaf){
        t[p]=mapping(f,t[p]);
        if(!leaf)lz[p]=composition(f,lz[p]);
    }
    void pushdown(ll p,ll l,ll r){
        if(lz[p]==id())return;
        all_apply(ls(p),lz[p],l==mid);
        all_apply(rs(p),lz[p],mid+1==r);
        lz[p]=id();
    }
    void build(ll p,ll l,ll r,const vector<S>&v){
        if(l==r){
            t[p]=v[l];
            return;
        }
        build(ls(p),l,mid,v);
        build(rs(p),mid+1,r,v);
        pushup(p);
    }
    lazy_segtree(){}
    lazy_segtree(ll n):lazy_segtree(vector<S>(n,e())){}
    lazy_segtree(const vector<S>&v):n(v.size()){
        t.resize(n<<2,e()),lz.resize(n<<2,id());
        build(1,0,n-1,v);
    }
    void resize(const vector<S>&v){
        n=v.size();
        t.resize(n<<2,e()),lz.resize(n<<2,id());
        build(1,0,n-1,v);
    }
    void resize(ll _n){
        n=_n;
        t.resize(n<<2,e()),lz.resize(n<<2,id());
        vector<S>v(n,e());
        build(1,0,n-1,v);
    }
    void apply(ll p,ll l,ll r,ll ql,ll qr,F f){
        if(ql<=l&&r<=qr){
            all_apply(p,f,l==r);
            return;
        }
        pushdown(p,l,r);
        if(ql<=mid)apply(ls(p),l,mid,ql,qr,f);
        if(qr>mid)apply(rs(p),mid+1,r,ql,qr,f);
        pushup(p);
    }
    void apply(ll l,ll r,F f){
        apply(1,0,n-1,l,r-1,f);
    }
    void set(ll p,ll l,ll r,ll x,S k){
        if(l==r){
            t[p]=k;
            return;
        }
        pushdown(p,l,r);
        if(x<=mid)set(ls(p),l,mid,x,k);
        else set(rs(p),mid+1,r,x,k);
        pushup(p);
    }
    void set(ll x,S k){
        set(1,0,n-1,x,k);
    }
    S prod(ll p,ll l,ll r,ll ql,ll qr){
        if(ql<=l&&r<=qr){
            return t[p];
        }
        pushdown(p,l,r);
        if(qr<=mid)return prod(ls(p),l,mid,ql,qr);
        if(ql>mid)return prod(rs(p),mid+1,r,ql,qr);
        return op(prod(ls(p),l,mid,ql,qr),prod(rs(p),mid+1,r,ql,qr));
    }
    S prod(ll l,ll r){
        return prod(1,0,n-1,l,r-1);
    }
    void merge(ll p,ll q,ll l,ll r,lazy_segtree &b){
        if(l==r){
            t[p]=op(t[p],b.t[q]);
            return;
        }
        pushdown(p,l,r);
        b.pushdown(q,l,r);
        merge(ls(p),ls(q),l,mid,b);
        merge(rs(p),rs(q),mid+1,r,b);
        pushup(p);
    }
    void merge(lazy_segtree &b){
        assert(n==b.n);
        merge(1,1,0,n-1,b);
    }
    #undef ls
    #undef rs
    #undef mid
};
template <class G>
    ll max_right(ll p,ll l,ll r,ll ql,S& sm,G& g){
        if(ql<=l){
            S nxt=op(sm,t[p]);
            if(g(nxt)){ sm=nxt; return r+1; }
            if(l == r) return l;
        }
        pushdown(p,l,r);
        if(ql<=mid){
            ll res=max_right(ls(p),l,mid,ql,sm,g);
            if(res<=mid) return res;
        }
        return max_right(rs(p),mid+1,r,ql,sm,g);
    }
    template <class G>
    ll max_right(ll p,ll l,ll r,ll ql,S& sm,G& g){
        if(ql<=l){
            S nxt=op(sm,t[p]);
            if(g(nxt))return sm=nxt,r+1;
            if(l==r) return l;
        }
        if(ql<=mid){
            ll res=max_right(ls(p),l,mid,ql,sm,g);
            if(res<=mid) return res;
        }
        return max_right(rs(p),mid+1,r,ql,sm,g);
    }
    template <class G>
    ll min_left(ll p,ll l,ll r,ll qr,S& sm,G& g){
        if(r<=qr){
            S nxt=op(t[p],sm);
            if(g(nxt))return sm=nxt,l;
            if(l==r)return l+1;
        }
        if(qr>mid){
            ll res=min_left(rs(p),mid+1,r,qr,sm,g);
            if(res>mid+1) return res;
        }
        return min_left(ls(p),l,mid,qr,sm,g);
    }
    template <class G>
    ll max_right(ll l,G g){
        if(l==n) return n;
        S sm=e();
        return max_right(1,0,n-1,l,sm,g);
    }
    template <class G>
    ll min_left(ll r,G g){
        if(r==0) return 0;
        S sm=e();
        return min_left(1,0,n-1,r-1,sm,g);
    }

\textbf{Dynamic Lazy Segtree}

template<class S,S(*op)(S,S),S(*e)(),class F,S(*mp)(F,S,ll),F(*cmp)(F,F),F(*id)()>
struct dynamic_lazy_segtree{
    ll rt,n;
    vector<S>t;
    vector<F>lz;
    vll len,ls,rs;
    #define mid (l+r)/2
    ll newnode(ll l,ll r){
        t.push_back(e());
        lz.push_back(id());
        len.push_back(r-l+1);
        ls.push_back(0);
        rs.push_back(0);
        return t.size()-1;
    }
    void up(ll p){
        S L=ls[p]?t[ls[p]]:e();
        S R=rs[p]?t[rs[p]]:e();
        t[p]=op(L,R);
    }
    void all(ll p,F f,bool l){
        t[p]=mp(f,t[p],len[p]);
        if(!l)lz[p]=cmp(f,lz[p]);
    }
    void down(ll p,ll l,ll r){
        if(l==r||lz[p]==id())return;
        if(!ls[p])ls[p]=newnode(l,mid);
        if(!rs[p])rs[p]=newnode(mid+1,r);
        all(ls[p],lz[p],l==mid);
        all(rs[p],lz[p],mid+1==r);
        lz[p]=id();
    }
    void apply(ll&p,ll l,ll r,ll ql,ll qr,F f){
        if(ql<=l&&r<=qr){
            if(!p)p=newnode(l,r);
            all(p,f,l==r);
            return;
        }
        if(!p)p=newnode(l,r);
        down(p,l,r);
        if(ql<=mid)apply(ls[p],l,mid,ql,qr,f);
        if(qr>mid)apply(rs[p],mid+1,r,ql,qr,f);

        up(p);
    }
    void apply(ll l,ll r,F f){
        if(l<r)apply(rt,0,n-1,l,r-1,f);
    }
    void set(ll&p,ll l,ll r,ll x,S k){
        if(!p)p=newnode(l,r);
        if(l==r){
            t[p]=k;
            lz[p]=id();
            return;
        }
        down(p,l,r);
        if(x<=mid)set(ls[p],l,mid,x,k);
        else set(rs[p],mid+1,r,x,k);
        up(p);
    }
    void set(ll x,S k){
        set(rt,0,n-1,x,k);
    }
    S prod(ll p,ll l,ll r,ll ql,ll qr){
        if(!p)return e();
        if(ql<=l&&r<=qr)return t[p];
        down(p,l,r);
        if(qr<=mid)return prod(ls[p],l,mid,ql,qr);
        if(ql>mid)return prod(rs[p],mid+1,r,ql,qr);
        return op(prod(ls[p],l,mid,ql,qr),prod(rs[p],mid+1,r,ql,qr));
    }
    S prod(ll l,ll r){
        if(l>=r)return e();
        return prod(rt,0,n-1,l,r-1);
    }
    ll merge(ll p,ll q,ll l,ll r){
        if(!p)return q;
        if(!q)return p;
        if(l==r){
            t[p]=op(t[p],t[q]);
            return p;
        }
        down(p,l,r);
        down(q,l,r);
        ls[p]=merge(ls[p],ls[q],l,mid);
        rs[p]=merge(rs[p],rs[q],mid+1,r);
        up(p);
        return p;
    }
    void merge(dynamic_lazy_segtree &b){
        assert(n==b.n);
        rt=merge(rt,b.rt,0,n-1);
        b.rt=0;
    }
    dynamic_lazy_segtree(ll _n):n(_n),rt(0){
        t.reserve(1<<23);
        lz.reserve(1<<23);
        len.reserve(1<<23);
        ls.reserve(1<<23);
        rs.reserve(1<<23);
        if(n>0)rt=newnode(0,n-1);
    }
    #undef mid
};

\textbf{Persistent Segtree}

template<class S,S(*op)(S,S),S(*e)(),S(*sub)(S,S)>
struct persistent_segtree{
    vector<S>t;
    vll ls,rs;
    ll n;
    #define mid ((l+r)/2)
    ll newnode(){
        t.push_back(e());
        ls.push_back(0);
        rs.push_back(0);
        return t.size()-1;
    }
    persistent_segtree(ll n):n(n){
        newnode();
    }
    void pushup(ll p){
        t[p]=op(t[ls[p]],t[rs[p]]);
    }
    ll build(ll l,ll r,vector<S>&a){
        ll p=newnode();
        if(l==r)return t[p]=a[l],p;
        ls[p]=build(l,mid,a);
        rs[p]=build(mid+1,r,a);
        pushup(p);
        return p;
    }
    ll build(vector<S>&a){
        return build(1,n,a);
    }
    ll apply(ll p,ll l,ll r,ll x,S v){
        ll q=newnode();
        t[q]=t[p],ls[q]=ls[p],rs[q]=rs[p];
        if(l==r)return t[q]=op(t[q],v),q;
        if(x<=mid)ls[q]=apply(ls[p],l,mid,x,v);
        else rs[q]=apply(rs[p],mid+1,r,x,v);
        pushup(q);
        return q;
    }
    ll apply(ll p,ll x,S v){
        return apply(p,1,n,x,v);
    }
    ll set(ll p,ll l,ll r,ll x,S v){
        ll q=newnode();
        t[q]=t[p],ls[q]=ls[p],rs[q]=rs[p];
        if(l==r)return t[q]=v,q;
        if(x<=mid)ls[q]=set(ls[p],l,mid,x,v);
        else rs[q]=set(rs[p],mid+1,r,x,v);
        pushup(q);
        return q;
    }
    S prod(ll p,ll l,ll r,ll ql,ll qr){
        if(!p||qr<l||r<ql)return e();
        if(ql<=l&&r<=qr)return t[p];
        return op(prod(ls[p],l,mid,ql,qr),prod(rs[p],mid+1,r,ql,qr));
    }
    S prod(ll p,ll l,ll r){return prod(p,1,n,l,r);}
    S get(ll p,ll l,ll r,ll x){
        if(!p)return e();
        if(l==r)return t[p];
        if(x<=mid)return get(ls[p],l,mid,x);
        return get(rs[p],mid+1,r,x);
    }
    S get(ll p,ll x){return get(p,1,n,x);}
    ll kth(ll u,ll v,ll l,ll r,ll k){
        if(l==r)return l;
        ll cnt=sub(t[ls[v]],t[ls[u]]);
        if(k<=cnt)return kth(ls[u],ls[v],l,mid,k);
        return kth(rs[u],rs[v],mid+1,r,k-cnt);
    }
    ll kth(ll u,ll v,ll k){
        return kth(u,v,1,n,k);
    }
};

\textbf{2D Segtree}

template<class S,S(*op)(S,S),S(*e)(),class F,S(*mp)(F,S),F(*cmp)(F,F),F(*id)()>
struct segtree_2D{
    ll n,m;
    vector<S>t;
    vector<F>lz;
    #define w (p*4)
    #define x (p*4+1)
    #define y (p*4+2)
    #define z (p*4+3)
    #define ctr ((u+d)/2)
    #define mid ((l+r)/2)
    // u:up,d:down,l:left,r:right
    // w:leftup x:rightup y:leftdown z:rightdown
    // 4-th quadrant
    void up(ll p){
        t[p]=op(op(t[w],t[x]),op(t[y],t[z]));
    }
    // leaf: l==r&&d==u
    void all(ll p,F f,bool l){
        t[p]=mp(f,t[p]);
        if(!l)lz[p]=cmp(f,lz[p]);
    }
    void down(ll p,ll u,ll d,ll l,ll r){
        if(lz[p]==id())return;
        all(w,lz[p],l==mid&&u==ctr);
        all(x,lz[p],mid+1==r&&u==ctr);
        all(y,lz[p],l==mid&&ctr+1==d);
        all(z,lz[p],mid+1==r&&ctr+1==d);
        lz[p]=id();
    }
    void build(ll p,ll u,ll d,ll l,ll r,const vector<vector<S>>&v){
        // cout<<"p="<<p<<",u="<<u<<",d="<<d<<",l="<<l<<",r="<<r<<endl;
        if(l==r&&d==u){
            t[p]=v[d][l];
            return;
        }
        if(u<=ctr&&l<=mid)build(w,u,ctr,l,mid,v);
        if(u<=ctr&&mid+1<=r)build(x,u,ctr,mid+1,r,v);
        if(ctr+1<=d&&l<=mid)build(y,ctr+1,d,l,mid,v);
        if(ctr+1<=d&&mid+1<=r)build(z,ctr+1,d,mid+1,r,v);
        up(p);
    }
    segtree_2D(const vector<vector<S>>&v):n(v.size()),m(v[0].size()){
        // cout<<"n="<<n<<",m="<<m<<endl;
        ll lim=n*m;
        // cout<<"lim="<<lim<<endl;
        t.resize(lim<<4,e());
        lz.resize(lim<<4,id());
        build(1,0,n-1,0,m-1,v);
    }
    segtree_2D(ll _n,ll _m){
        n=_n,m=_m;
        ll lim=n*m;
        t.resize(lim<<4,e());
        lz.resize(lim<<4,id());
    }
    void apply(ll p,ll u,ll d,ll l,ll r,ll qu,ll qd,ll ql,ll qr,F f){
        if(qd<u||qu>d||qr<l||ql>r)return;
        if(qu<=u&&d<=qd&&ql<=l&&r<=qr){
            all(p,f,u==d&&l==r);
            return;
        }
        down(p,u,d,l,r);
        if(qu<=ctr&&ql<=mid)apply(w,u,ctr,l,mid,qu,qd,ql,qr,f);
        if(qu<=ctr&&qr>mid)apply(x,u,ctr,mid+1,r,qu,qd,ql,qr,f);
        if(qd>ctr&&ql<=mid)apply(y,ctr+1,d,l,mid,qu,qd,ql,qr,f);
        if(qd>ctr&&qr>mid)apply(z,ctr+1,d,mid+1,r,qu,qd,ql,qr,f);
        up(p);
    }
    void apply(ll u,ll d,ll l,ll r,F f){
        return apply(1,0,n-1,0,m-1,u,d-1,l,r-1,f);
    }
    S prod(ll p,ll u,ll d,ll l,ll r,ll qu,ll qd,ll ql,ll qr){
        if(qd<u||qu>d||qr<l||ql>r)return e();
        if(qu<=u&&d<=qd&&ql<=l&&r<=qr){
            return t[p];
        }
        S res=e();
        down(p,u,d,l,r);
        if(qu<=ctr&&ql<=mid)res=op(res,prod(w,u,ctr,l,mid,qu,qd,ql,qr));
        if(qu<=ctr&&qr>mid)res=op(res,prod(x,u,ctr,mid+1,r,qu,qd,ql,qr));
        if(qd>ctr&&ql<=mid)res=op(res,prod(y,ctr+1,d,l,mid,qu,qd,ql,qr));
        if(qd>ctr&&qr>mid)res=op(res,prod(z,ctr+1,d,mid+1,r,qu,qd,ql,qr));
        return res;
    }
    S prod(ll u,ll d,ll l,ll r){
        return prod(1,0,n-1,0,m-1,u,d-1,l,r-1);
    }
    S all_prod(){
        return t[1];
    }
    #undef w
    #undef x
    #undef y
    #undef z
    #undef ctr
    #undef mid
};

\textbf{Weighted DSU}

struct weighted_dsu{
    vll fa,d;
    weighted_dsu(){}
    weighted_dsu(ll n):fa(n,-1),d(n,0){}

    ll leader(ll x){
        if(fa[x]<0)return x;
        ll p=fa[x];
        fa[x]=leader(fa[x]);
        d[x]+=d[p];
        return fa[x];
    }
    bool merge(ll x,ll y,ll k){
        ll u=leader(x),v=leader(y);
        if(u==v)return d[x]-d[y]==k;
        if(fa[u]>fa[v])swap(u,v),swap(x,y),k=-k;
        fa[u]+=fa[v],fa[v]=u;
        d[v]=d[x]-d[y]-k;
        return 1;
    }
    bool same(ll x,ll y){
        return leader(x)==leader(y);
    }
    ll diff(ll x,ll y){
        leader(x),leader(y);
        return d[x]-d[y];
    }
    ll size(ll x){
        return -fa[leader(x)];
    }
};

\textbf{Rollback DSU}

struct rollback_dsu{
    vll fa;
    vector<pair<ll,ll>> stk;
    rollback_dsu(){}
    rollback_dsu(ll n):fa(n,-1){}
    ll leader(ll x){
        while(fa[x]>=0)x=fa[x];
        return x;
    }
    bool same(ll x,ll y){
        return leader(x)==leader(y);
    }
    ll size(ll x){
        return -fa[leader(x)];
    }
    bool merge(ll x,ll y){
        x=leader(x),y=leader(y);
        if(x==y)return 0;
        if(fa[x]>fa[y])swap(x,y);
        stk.emplace_back(x,fa[x]);
        stk.emplace_back(y,fa[y]);
        fa[x]+=fa[y],fa[y]=x;
        return 1;
    }
    ll snapshot(){
        return stk.size();
    }
    void rollback(ll t){
        while(stk.size()>t){
            auto [x,v]=stk.back();
            stk.pop_back();
            fa[x]=v;
        }
    }
    void undo(){
        rollback(stk.size()-2);
    }
};

\textbf{Sparse Table}

template<class S,S(*op)(S,S)>
struct sparse_table{
    ll n,m;
    vector<vector<S>>t;
    void build(const vector<S>&v){
        n=v.size(),m=__lg(v.size())+1;
        t.resize(m,vector<S>(n));
        for(ll i=0;i<n;i++)t[0][i]=v[i];
        for(ll j=1;j<m;j++){
            for(ll i=0;i+(1<<j)<=n;i++){
                t[j][i]=op(t[j-1][i],t[j-1][i+(1<<(j-1))]);
            }
        }
    }
    sparse_table(){}
    sparse_table(const vector<S>&v){
        build(v);
    }
    S get(ll x){
        assert(0<=x&&x<n);
        return t[0][x];
    }
    S prod(ll l,ll r){// [l,r)
        assert(0<=l&&l<r&&r<=n);
        ll len=__lg(r-l);
        return op(t[len][l],t[len][r-(1<<len)]);
    }
};

\textbf{Disjoint Sparse Table}

template<class S,S(*op)(S,S)>
struct disjoint_sparse_table{
    ll n,m;
    vector<vector<S>>t;
    disjoint_sparse_table(){}
    disjoint_sparse_table(const vector<S>&v){
        build(v);
    }
    void build(const vector<S>&v){
        n=v.size();
        m=__lg(n)+2;
        t.assign(m,vector<S>(n));
        for(ll i=0;i<n;i++)t[0][i]=v[i];
        for(ll j=1;j<m;j++){
            ll len=1LL<<j;
            for(ll l=0;l<n;l+=len){
                ll mid=min(l+(len>>1),n);
                ll r=min(l+len,n);
                // [l,mid)
                if(mid>l){
                    t[j][mid-1]=v[mid-1];
                    for(ll i=mid-2;i>=l;i--){
                        t[j][i]=op(v[i],t[j][i+1]);
                    }
                }
                // [mid,r)
                if(mid<r){
                    t[j][mid]=v[mid];
                    for(ll i=mid+1;i<r;i++){
                        t[j][i]=op(t[j][i-1],v[i]);
                    }
                }
            }
        }
    }

    S get(ll x){
        assert(0<=x&&x<n);
        return t[0][x];
    }

    S prod(ll l,ll r){// [l,r)
        assert(0<=l&&l<r&&r<=n);
        if(l+1==r)return t[0][l];
        ll k=__lg(l^(r-1))+1;
        return op(t[k][l],t[k][r-1]);
    }
};

\textbf{Fraction}

struct frac{
    ll p,q;
    frac():p(0),q(1){}
    frac(ll x):p(x),q(1){}
    frac(ll p,ll q):p(p),q(q){
        norm();
    }
    void norm(){
        assert(q!=0);
        if(q<0)p=-p,q=-q;
        ll g=std::gcd(p<0?-p:p,q);
        p/=g,q/=g;
    }
    ll num()const{
        return p;
    }
    ll den()const{
        return q;
    }
    long double val()const{
        return (long double)p/q;
    }
    explicit operator double()const{
        return (double)p/q;
    }
    explicit operator long double()const{
        return val();
    }
    frac operator+()const{
        return *this;
    }
    frac operator-()const{
        return {-p,q};
    }
    frac operator+(const frac&b)const{
        ll g=std::gcd(q,b.q);
        ll x=q/g,y=b.q/g;
        return {p*y+b.p*x,x*b.q};
    }
    frac operator-(const frac&b)const{
        return *this+(-b);
    }
    frac operator*(const frac&b)const{
        ll g1=std::gcd(p<0?-p:p,b.q);
        ll g2=std::gcd(b.p<0?-b.p:b.p,q);
        return {(p/g1)*(b.p/g2),(q/g2)*(b.q/g1)};
    }
    frac operator/(const frac&b)const{
        assert(b.p!=0);
        return *this*frac(b.q,b.p);
    }
    frac& operator+=(const frac&b){
        return *this=*this+b;
    }
    frac& operator-=(const frac&b){
        return *this=*this-b;
    }
    frac& operator*=(const frac&b){
        return *this=*this*b;
    }
    frac& operator/=(const frac&b){
        return *this=*this/b;
    }
    bool operator==(const frac&b)const{
        return p==b.p&&q==b.q;
    }
    bool operator!=(const frac&b)const{
        return !(*this==b);
    }
    bool operator<(const frac&b)const{
        return (__int128)p*b.q<(__int128)b.p*q;
    }
    bool operator>(const frac&b)const{
        return b<*this;
    }
    bool operator<=(const frac&b)const{
        return !(b<*this);
    }
    bool operator>=(const frac&b)const{
        return !(*this<b);
    }
    frac& operator++(){
        p+=q;
        return *this;
    }
    frac operator++(int){
        frac res=*this;
        ++*this;
        return res;
    }
    frac& operator--(){
        p-=q;
        return *this;
    }
    frac operator--(int){
        frac res=*this;
        --*this;
        return res;
    }
    ll floor()const{
        if(p>=0)return p/q;
        return -((-p+q-1)/q);
    }
    ll ceil()const{
        return -(-*this).floor();
    }
    frac abs()const{
        return {p<0?-p:p,q};
    }
    friend std::ostream& operator<<(std::ostream&out,const frac&a){
        out<<a.p;
        if(a.q!=1)out<<'/'<<a.q;
        return out;
    }
    friend std::istream& operator>>(std::istream&in,frac&a){
        ll p,q;
        char c;
        if(!(in>>p))return in;
        if(in.peek()=='/'){
            in.get(c);
            if(!(in>>q))return in;
            if(q==0){
                in.setstate(std::ios::failbit);
                return in;
            }
            a=frac(p,q);
        }else{
            a=frac(p);
        }

        return in;
    }
};

\textbf{Linear Sieve}

struct linear_sieve{
    ll n;
    vll lp,primes;

    linear_sieve(ll n):n(n),lp(n+1,0){
        for(ll i=2;i<=n;i++){
            if(!lp[i]){
                lp[i]=i;
                primes.push_back(i);
            }
            for(auto p:primes){
                if(p>lp[i]||i*p>n)break;
                lp[i*p]=p;
            }
        }
    }
    bool is_prime(ll x)const{
        return x>=2&&lp[x]==x;
    }
    ll min_prime(ll x)const{
        return lp[x];
    }
    vll factorize(ll x)const{
        vll res;
        while(x>1){
            res.push_back(lp[x]);
            x/=lp[x];
        }
        return res;
    }
};
struct linear_sieve{
    ll n;
    vll lp,primes,phi,mu,tau,sigma,cnt,pw,p_sum;
    linear_sieve(ll n):n(n),lp(n+1,0),
    phi(n+1),mu(n+1),tau(n+1),
    sigma(n+1),cnt(n+1),pw(n+1),p_sum(n+1){
        if(n>=1){
            phi[1]=mu[1]=tau[1]=sigma[1]=1;
            pw[1]=1;
            p_sum[1]=1;
        }
        for(ll i=2;i<=n;i++){
            if(!lp[i]){
                lp[i]=i;
                primes.push_back(i);
                phi[i]=i-1;
                mu[i]=-1;
                tau[i]=2;
                sigma[i]=i+1;
                cnt[i]=1;
                pw[i]=i;
                p_sum[i]=i+1;
            }
            for(auto p:primes){
                if(p>lp[i]||i*p>n)break;
                ll x=i*p;
                lp[x]=p;
                if(p==lp[i]){
                    cnt[x]=cnt[i]+1;
                    pw[x]=pw[i]*p;
                    p_sum[x]=p_sum[i]+pw[x];

                    phi[x]=phi[i]*p;
                    mu[x]=0;
                    tau[x]=tau[i]/(cnt[i]+1)*(cnt[x]+1);
                    sigma[x]=sigma[i]/p_sum[i]*p_sum[x];

                    break;
                }
                cnt[x]=1;
                pw[x]=p;
                p_sum[x]=p+1;
                phi[x]=phi[i]*(p-1);
                mu[x]=-mu[i];
                tau[x]=tau[i]*2;
                sigma[x]=sigma[i]*(p+1);
            }
        }
    }
    bool is_prime(ll x)const{
        return x>=2&&lp[x]==x;
    }
    ll min_prime(ll x)const{
        return lp[x];
    }
    vll factorize(ll x)const{
        vll res;
        while(x>1){
            res.push_back(lp[x]);
            x/=lp[x];
        }
        return res;
    }
};

\textbf{Linear Basis}

struct linear_basis{
    static constexpr ll B=60;
    vll d;
    ll size=0,built=0;
    linear_basis():d(B+1,0){}

    void insert(ll x){
        for(ll i=B;i>=0;i--){
            if(!(x>>i&1))continue;
            if(!d[i]){
                d[i]=x;
                size++;
                return;
            }
            x^=d[i];
        }
    }
    bool contains(ll x)const{
        for(ll i=B;i>=0;i--){
            if(!(x>>i&1))continue;
            if(!d[i])return 0;
            x^=d[i];
        }
        return x==0;
    }
    ll max_xor(ll x=0)const{
        for(ll i=B;i>=0;i--){
            x=max(x,x^d[i]);
        }
        return x;
    }
    ll min_xor()const{
        for(ll i=0;i<=B;i++){
            if(d[i])return d[i];
        }
        return 0;
    }
    void merge(const linear_basis&b){
        for(ll i=0;i<=B;i++){
            if(b.d[i])insert(b.d[i]);
        }
    }
    void rebuild(){
        for(ll i=B;i>=0;i--){
            if(!d[i])continue;
            for(ll j=i-1;j>=0;j--){
                if(d[i]>>j&1)d[i]^=d[j];
            }
        }
        built=1;
    }
    ll kth(ll k)const{
        assert(built);
        if(size<B+1){
            if(k>=(1LL<<size))return -1;
        }
        else{
            if(k<0)return -1;
        }
        ll res=0;
        for(ll i=0;i<=B;i++){
            if(d[i]){
                if(k&1)res^=d[i];
                k>>=1;
            }
        }
        return res;
    }
};

\textbf{Path Decomposition}

template<class S,S(*op)(S,S),S(*e)(),class F,S(*mapping)(F,S),F(*composition)(F,F),F(*id)()>
struct path_decomposition{
    graph g;
    ll n,m,tim=0,rt;
    vll dep,sz,f,son,dfn,top;
    lazy_segtree<S,op,e,F,mapping,composition,id>T;
    // 1-based
    void all_resize(ll siz){
        g.resize(siz);
        dep.resize(siz),sz.resize(siz),f.resize(siz);
        son.resize(siz),dfn.resize(siz),top.resize(siz);
    }
    path_decomposition(ll _n):n(_n),m(0),rt(-1){
        T.resize(n+1),all_resize(n+1);
    }
    path_decomposition(const vector<S>&v):n(v.size()),m(0),rt(-1){
        T.resize(v),all_resize(n+1);
    }
    void setroot(ll u){
        rt=u;
    }
    void addedge(ll u,ll v){
        assert(1<=u&&u<=n);
        assert(1<=v&&v<=n);
        m++;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    void dfs1(ll u,ll fa){
        sz[u]=1,dep[u]=dep[fa]+1,f[u]=fa;
        ll mx=0;
        for(auto v:g[u]){
            if(v==fa)continue;
            dfs1(v,u);
            sz[u]+=sz[v];
            if(sz[v]>mx){
                mx=sz[v];
                son[u]=v;
            }
        }
    }

    void dfs2(ll u,ll topf){
        top[u]=topf,dfn[u]=++tim;
        if(son[u])dfs2(son[u],topf);
        for(auto v:g[u]){
            if(v==son[u]||v==f[u])continue;
            dfs2(v,v);
        }
    }

    void decomposition(){
        assert(rt!=-1);
        dfs1(rt,0);
        dfs2(rt,rt);
    }

    #define X (top[x])
    #define Y (top[y])

    void apply(ll x,ll y,F f){
        while(X!=Y){
            if(dep[X]>dep[Y])swap(x,y);
            T.apply(dfn[Y],dfn[y]+1,f);
            y=f[Y];
        }
        if(dep[x]>dep[y])swap(x,y);
        T.apply(dfn[x],dfn[y]+1,f);
    }

    void apply(ll x,F f){
        T.apply(dfn[x],dfn[x]+sz[x],f);
    }

    S prod(ll x,ll y){
        S sum=e();
        while(X!=Y){
            if(dep[X]>dep[Y])swap(x,y);
            sum=op(sum,T.prod(dfn[Y],dfn[y]+1));
            y=f[Y];
        }
        if(dep[x]>dep[y])swap(x,y);
        sum=op(sum,T.prod(dfn[x],dfn[y]+1));
        return sum;
    }

    S prod(ll x){
        return T.prod(dfn[x],dfn[x]+sz[x]);
    }

};

\textbf{Power}

template<class S,S(*op)(S,S),S(*e)()>
struct power{
    static S calc(S x,ll n){
        S r=e();
        for(;n;n>>=1,x=op(x,x))
            if(n&1)r=op(r,x);
        return r;
    }
};