#include<bits/stdc++.h>
#define N 100100
using namespace std;
long long s[4*N],lazy[4*N],lazy2[4*N];
int n,m,M;
void built(int k,int l,int r){
    lazy[k]=1;
    lazy2[k]=0;
    if(l==r){
        cin>>s[k];
        return;
    }
    int mid=(l+r)/2;
    built(2*k,l,mid);
    built(2*k+1,mid+1,r);
    s[k]=s[2*k]+s[2*k+1];
}
void pushdown(int k,int l,int r){
    if(lazy[k]!=1){
        s[2*k]=(s[2*k]*lazy[k])%M;
        lazy[2*k]=(lazy[2*k]*lazy[k])%M;
        lazy2[2*k]=(lazy2[2*k]*lazy[k])%M;
        s[2*k+1]=(s[2*k+1]*lazy[k])%M;
        lazy[2*k+1]=(lazy[2*k+1]*lazy[k])%M;
        lazy2[2*k+1]=(lazy2[2*k+1]*lazy[k])%M;
        lazy[k]=1;
    }   
    int mid=(l+r)/2;
    if(lazy2[k]!=0){
        s[2*k]=(s[2*k]+(mid-l+1)*lazy2[k])%M;
        lazy2[2*k]=(lazy2[2*k]+lazy2[k])%M;
        s[2*k+1]=(s[2*k+1]+(r-mid)*lazy2[k])%M;
        lazy2[2*k+1]=(lazy2[2*k+1]+lazy2[k])%M;
        lazy2[k]=0;
    }
    return;
} 
void update(int k,int l,int r,int x,int y,int t1,int t2){
    if(l>y||r<x) return;
    if(x<=l&&r<=y){
        if(t1>=0){
            s[k]=((s[k]%M)+(r-l+1)*(t1%M))%M;
            lazy2[k]+=t1;
        } 
        else{
            s[k]=((s[k]%M)*(t2%M))%M;
            lazy[k]=(lazy[k]*t2)%M;
            lazy2[k]=(lazy2[k]*t2)%M;
        }
        return; 
    }
    pushdown(k,l,r);
    int mid=(l+r)/2;
    update(2*k,l,mid,x,y,t1,t2);
    update(2*k+1,mid+1,r,x,y,t1,t2);
    s[k]=((s[2*k]%M)+(s[2*k+1]%M))%M;
} 
long long ask(int k,int l,int r,int x,int y){
    if(y<l||x>r) return 0;
    if(x<=l&&r<=y) return s[k];
    pushdown(k,l,r);
    int mid=(l+r)/2;
    return (ask(2*k,l,mid,x,y)+ask(2*k+1,mid+1,r,x,y))%M;
}
int main(){
    cin>>n>>M;
    built(1,1,n);
    int k,t,g;
    long long c;
    cin>>m;
    while(m--){
        cin>>k;
        if(k==1){
            cin>>t>>g>>c;
            update(1,1,n,t,g,-1,c); 
        }
        if(k==2){
            cin>>t>>g>>c;
            update(1,1,n,t,g,c,1);
        }
        if(k==3){
            cin>>t>>g;
            cout<<ask(1,1,n,t,g)<<endl;
        }
    }
    return 0;
}

0 条评论

目前还没有评论...