ssss
作者:互联网
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAX 100005 4 struct Node 5 { 6 int l,r; 7 long long lazyadd,lazymul,val; 8 }node[MAX<<2]; 9 long long d[MAX],p; 10 inline void pushup(int rt) 11 { 12 node[rt].val=(node[rt<<1].val+node[rt<<1|1].val)%p; 13 } 14 inline void construct(int rt,int l,int r) 15 { 16 node[rt].l=l; 17 node[rt].r=r; 18 node[rt].lazyadd=node[rt].val=0; 19 node[rt].lazymul=1; 20 if(l==r) 21 { 22 node[rt].val=d[l]; 23 return; 24 } 25 int mid=(l+r)>>1; 26 construct(rt<<1,l,mid); 27 construct(rt<<1|1,mid+1,r); 28 pushup(rt); 29 } 30 inline void pushdown(int rt) 31 { 32 if(node[rt].lazymul!=1) 33 { 34 node[rt<<1].val=(node[rt<<1].val%p)*(node[rt].lazymul%p)%p; 35 node[rt<<1|1].val=(node[rt<<1|1].val%p)*(node[rt].lazymul%p)%p; 36 node[rt<<1].lazymul=(node[rt<<1].lazymul%p)*(node[rt].lazymul%p)%p; 37 node[rt<<1|1].lazymul=(node[rt<<1|1].lazymul%p)*(node[rt].lazymul%p)%p; 38 node[rt<<1].lazyadd=(node[rt<<1].lazyadd)*(node[rt].lazymul)%p; 39 node[rt<<1|1].lazyadd=(node[rt<<1|1].lazyadd)*(node[rt].lazymul)%p; 40 node[rt].lazymul=1; 41 } 42 if(node[rt].lazyadd) 43 { 44 node[rt<<1].val+=(node[rt<<1].r-node[rt<<1].l+1)*node[rt].lazyadd; 45 node[rt<<1|1].val+=(node[rt<<1|1].r-node[rt<<1|1].l+1)*node[rt].lazyadd; 46 node[rt<<1].lazyadd+=node[rt].lazyadd; 47 node[rt<<1|1].lazyadd+=node[rt].lazyadd; 48 node[rt].lazyadd=0; 49 } 50 } 51 inline void modify(int rt,int fr,int to,long long v) 52 { 53 if(fr<=node[rt].l&&to>=node[rt].r) 54 { 55 node[rt].lazyadd+=v; 56 node[rt].val+=v*(node[rt].r-node[rt].l+1); 57 return; 58 } 59 int mid=(node[rt].l+node[rt].r)>>1; 60 pushdown(rt); 61 if(fr<=mid) modify(rt<<1,fr,to,v); 62 if(to>mid) modify(rt<<1|1,fr,to,v); 63 pushup(rt); 64 } 65 inline void modify2(int rt,int fr,int to,long long v) 66 { 67 if(fr<=node[rt].l&&to>=node[rt].r) 68 { 69 node[rt].lazymul=(node[rt].lazymul*v)%p; 70 node[rt].lazyadd=(node[rt].lazyadd*v)%p; 71 node[rt].val=(v*node[rt].val)%p; 72 return; 73 } 74 int mid=(node[rt].l+node[rt].r)>>1; 75 pushdown(rt); 76 if(fr<=mid) modify2(rt<<1,fr,to,v); 77 if(to>mid) modify2(rt<<1|1,fr,to,v); 78 pushup(rt); 79 } 80 inline long long query(int rt,int fr,int to,int l,int r) 81 { 82 if(fr<=l&&to>=r) 83 return node[rt].val%p; 84 pushdown(rt); 85 int mid=(l+r)>>1; 86 long long ans=0; 87 if(fr<=mid) ans+=query(rt<<1,fr,to,l,mid); 88 if(to>mid) ans+=query(rt<<1|1,fr,to,mid+1,r); 89 return ans%p; 90 } 91 int main() 92 { 93 //freopen("in.txt","r",stdin); 94 //freopen("testout.txt","w",stdout); 95 long long n,m,a,b,c,k; 96 scanf("%lld%lld%lld",&n,&m,&p); 97 for(register int i=1;i<=n;++i) 98 scanf("%lld",&d[i]),d[i]%=p; 99 construct(1,1,n); 100 for(register int i=1;i<=m;++i) 101 { 102 scanf("%lld%lld%lld",&a,&b,&c); 103 if(a==2) 104 { 105 scanf("%lld",&k); 106 modify(1,b,c,k%p); 107 } 108 else if(a==1) 109 { 110 scanf("%lld",&k); 111 modify2(1,b,c,k%p); 112 } 113 else printf("%lld\n",query(1,b,c,1,n)); 114 } 115 return 0; 116 }
标签:rt,node,val,int,long,ssss,lazyadd 来源: https://www.cnblogs.com/juruo-1237898456ye/p/11025415.html