跳到正文

剪贴板

剪贴板 ouxv1oo8

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define dep(i,r,l) for(int i=(r);i>=(l);i--)
#define ls (u<<1)
#define rs (u<<1|1)
#define fi first
#define se second
using namespace std;
const int N=1e5+15,mod=1e9+7;const ll inf=2e9;const long double eps=1e-9;
namespace I_O {
#define IOSIZE 100000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    inline bool read(double &x){x=0.0;int w = 1;double y=0.1;char ch=getchar(); for(;!isdigit(ch) and (ch != EOF);ch=getchar()) if(ch=='-') w=-1;if (ch == EOF) return false; for(;isdigit(ch);ch=getchar()) x=x*10+(ch^48);if(ch!='.'){x*=w;return true;} for(ch=getchar();isdigit(ch);ch=getchar(),y/=10) x+=y*(ch-48);x*=w;return true;}
    template<typename T> inline void put(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) put(x / 10); putchar(x % 10 + 48); }
    inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
    inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
    inline void put(char x) { putchar(x); }
    inline void put(char *x) { while (*x) putchar(*x++); }
    inline void put(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
    inline void put(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
    inline void put(bool b) { putchar(b+48); }
    template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> inline void put(T a, T1... other) { put(a), put(other...); }
    struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return put(b), io; }
#define cout io
#define cin io
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
// #define int long long
inline int Max(const int &a,const int &b){return a>b?a:b;}
inline int Min(const int &a,const int &b){return a<b?a:b;}
} using namespace I_O;

int n,m;
int a[N];bool p[N];
struct modint{
    int val;
    static int norm(const int& x){return x<0?x+mod:x;}
    modint():val(0){}
    modint(const int& m):val(norm(m)){}
    modint operator = (const int& o){return val=o,*this;}
    modint operator-()const{return modint(norm(-val));}
    bool operator==(const modint&o){return val==o.val;}
    bool operator<(const modint&o){return val<o.val;}
    modint&operator+=(const modint&o){return val=(1ll*val+o.val)%mod,*this;}
    modint&operator-=(const modint&o){return val=norm(1ll*val-o.val),*this;}
    modint&operator*=(const modint&o){return val=1ll*val*o.val%mod,*this;}
    modint operator-(const modint&o)const{return modint(*this)-=o;}
    modint operator+(const modint&o)const{return modint(*this)+=o;}
    modint operator*(const modint&o)const{return modint(*this)*=o;}
    friend std::ostream&operator<<(std::ostream& os, const modint&a){return os<<a.val;}
    friend std::istream&operator>>(std::istream& is,modint&a){long long v;return is>>v,a.val=norm(v%mod),is;}
};
inline modint qpow(modint a,int b){
    modint ans(1);
    while(b){
        if(b&1) ans=ans*a;
        a=a*a;b>>=1;
    }
    return ans;
}
struct tree{
    int l,r;bool p;
    int llen,rlen,len,mx;
    modint L,v,sum,mul,lm,rm,fs;
    int fp;
    vector<int> q,cnt;

    void up(const tree &t1,const tree &t2){
        sum=t1.sum+t2.sum;
        mul=t1.mul*t2.mul;
        L=t1.L,p=t2.p;
        llen=t1.llen;
        lm=t1.lm;
        rlen=t2.rlen;
        rm=t2.rm;
        rep(i,0,mx) cnt[i]=0;
        q.clear();
        int LL=0,RR=0,add=0;
        if(t1.p){
            v=t1.v+t2.v-t1.rm-t2.lm+t1.rm*t2.lm;
            if(t1.len==t1.llen){
                llen=t1.len+t2.llen;
                lm=t1.lm*t2.lm;
            }
            if(t2.len==t2.rlen){
                rlen=t1.rlen+t2.len;
                rm=t1.rm*t2.rm;
            }
            LL=t1.rlen,RR=t2.llen,add=LL+RR;
            if(LL&&LL<=mx) cnt[LL]--,LL=0;
            if(RR&&RR<=mx) cnt[RR]--,RR=0;
            if(add){
                if(add<=mx) cnt[add]++;
                else q.emplace_back(add);
            }
        }else v=t1.v+t2.v;
        rep(i,1,t1.mx) cnt[i]+=t1.cnt[i];
        rep(i,1,t2.mx) cnt[i]+=t2.cnt[i];
        for(int i=0;i<t1.q.size();i++){
            int x=t1.q[i];
            if(x==LL) LL=0;
            else if(x==RR) RR=0;
            else if(x<=mx) cnt[x]++;
            else q.emplace_back(x);
        }
        for(int i=0;i<t2.q.size();i++){
            int x=t2.q[i];
            if(x==LL) LL=0;
            else if(x==RR) RR=0;
            else if(x<=mx) cnt[x]++;
            else q.emplace_back(x);
        }
    }

    void push_x(const modint &x){
        fs=x;
        v=0,sum=x*len,mul=qpow(x,len),L=x,lm=qpow(x,llen),rm=qpow(x,rlen);
        modint pw=1;
        rep(i,1,mx) pw*=x,v+=pw*cnt[i];
        for(int i=0;i<q.size();i++) v+=qpow(x,q[i]);
    }

    void push_p(const bool &x){
        p=fp=x;
        rep(i,0,mx) cnt[i]=0;
        q.clear();
        if(x){
            if(len>mx) q.emplace_back(len);
            else cnt[len]++;
            v=lm=rm=mul;llen=rlen=len;
        }else{
            v=sum,lm=L,rm=1;
            cnt[1]=len;
            llen=1,rlen=0;
        }
    }
}t[N<<2],res;

void down(int u){
    if(t[u].fs.val!=-1) t[ls].push_x(t[u].fs),t[rs].push_x(t[u].fs),t[u].fs=-1;
    if(t[u].fp!=-1) t[ls].push_p(t[u].fp),t[rs].push_p(t[u].fp),t[u].fp=-1;
}

void build(int u,int l,int r){
    t[u].l=l,t[u].r=r,t[u].fs=t[u].fp=-1;
    if(l==r){
        t[u].p=p[l],t[u].L=a[l];
        t[u].sum=t[u].mul=t[u].v=t[u].lm=a[l];
        t[u].mx=t[u].len=t[u].llen=1;
        if(p[l]) t[u].rlen=1,t[u].rm=a[l];
        else t[u].rlen=0,t[u].rm=1;
        t[u].cnt.resize(2);t[u].cnt[0]=0,t[u].cnt[1]=1;
        return ;
    }
    t[u].len=r-l+1;
    t[u].mx=sqrt(r-l+1);
    t[u].cnt.resize(t[u].mx+1);
    int mid=(l+r)>>1;
    build(ls,l,mid),build(rs,mid+1,r);
    t[u].up(t[ls],t[rs]);
}

void update(int u,const int l,const int r,const int &x){
    if(l<=t[u].l&&t[u].r<=r) return t[u].push_x(x);
    down(u);
    int mid=(t[u].l+t[u].r)>>1;
    if(l<=mid) update(ls,l,r,x);
    if(r>mid) update(rs,l,r,x);
    t[u].up(t[ls],t[rs]);
}

void update_(int u,const int l,const int r,const int &x){
    if(l<=t[u].l&&t[u].r<=r) return t[u].push_p(x);
    down(u);
    int mid=(t[u].l+t[u].r)>>1;
    if(l<=mid) update_(ls,l,r,x);
    if(r>mid) update_(rs,l,r,x);
    t[u].up(t[ls],t[rs]);
}

tree query(int u,const int l,const int r){
    if(l<=t[u].l&&t[u].r<=r) return t[u];
    down(u);
    int mid=(t[u].l+t[u].r)>>1;
    if(r<=mid) return query(ls,l,r);
    if(l>mid) return query(rs,l,r);
    tree t1=query(ls,l,r),t2=query(rs,l,r);
    res.len=t1.len+t2.len;
    res.p=t2.p;
    if(t1.p){
        res.v=t1.v+t2.v-t1.rm-t2.lm+t1.rm*t2.lm;
        if(t1.len==t1.llen){
            res.llen=t1.len+t2.llen;
            res.lm=t1.lm*t2.lm;
        }else{
            res.llen=t1.llen;
            res.lm=t1.lm;
        }
        if(t2.len==t2.rlen){
            res.rlen=t1.rlen+t2.len;
            res.rm=t1.rm*t2.rm;
        }else{
            res.rlen=t2.rlen;
            res.rm=t2.rm;
        }
    }else{
        res.v=t1.v+t2.v;
        res.llen=t1.llen;
        res.lm=t1.lm;
        res.rlen=t2.rlen;
        res.rm=t2.rm;
    }
    return res;
}

signed main(){
    cin>>n>>m;
    int opt,l,r;ll v;
    rep(i,1,n) cin>>v,a[i]=v%mod;
    rep(i,1,n-1) cin>>p[i];
    build(1,1,n);
    while(m--){
        cin>>opt>>l>>r;
        if(opt==1){
            cin>>v;v%=mod;
            update(1,l,r,v);
        }else if(opt==2){
            cin>>v;
            update_(1,l,r,v);
        }else cout<<query(1,l,r).v.val<<'\n';
    }
    return 0;
}