码迷,mamicode.com
首页 > 其他好文 > 详细

【luogu P2023 [AHOI2009]维护序列】 题解

时间:2018-04-09 23:02:44      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:blank   ref   ring   lan   text   push   long   res   \n   

题目链接:https://www.luogu.org/problemnew/show/P2023

把P3373改一改直接粘过来就A

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #define lson left, mid, rt<<1
  6 #define rson mid+1, right, rt<<1|1
  7 #define ll long long
  8 using namespace std;
  9 const int maxn = 100010;
 10 ll n, m, addlazy[maxn<<2], mullazy[maxn<<2], ans[maxn<<2], mod, x,y,k;
 11 void PushUP(ll rt)
 12 {
 13     ans[rt] = (ans[rt<<1] + ans[rt<<1|1])%mod;
 14 }
 15 void build(ll left, ll right, ll rt)
 16 {
 17     addlazy[rt] = 0;
 18     mullazy[rt] = 1;
 19     if(right == left)
 20     {
 21         scanf("%d",&ans[rt]);
 22         return ;
 23     }
 24     ll mid = (left+right) >> 1;
 25     build(lson);
 26     build(rson);
 27     PushUP(rt);
 28 }
 29 void PushDOWN(ll rt, ll mid, ll left, ll right)
 30 {
 31         mullazy[rt<<1]=(mullazy[rt<<1]*mullazy[rt])%mod;
 32         mullazy[rt<<1|1]=(mullazy[rt<<1|1]*mullazy[rt])%mod;
 33         addlazy[rt<<1]=(addlazy[rt<<1]*mullazy[rt])%mod;
 34         addlazy[rt<<1|1]=(addlazy[rt<<1|1]*mullazy[rt])%mod;
 35         ans[rt<<1]=(ans[rt<<1]*mullazy[rt])%mod;
 36         ans[rt<<1|1]=(ans[rt<<1|1]*mullazy[rt])%mod;
 37         mullazy[rt]=1;
 38         addlazy[rt<<1]=(addlazy[rt<<1]+addlazy[rt])%mod;
 39         addlazy[rt<<1|1]=(addlazy[rt<<1|1]+addlazy[rt])%mod;
 40         ans[rt<<1]=(ans[rt<<1]+(mid-left+1)*addlazy[rt])%mod;
 41         ans[rt<<1|1]=(ans[rt<<1|1]+(right-mid)*addlazy[rt])%mod;
 42         addlazy[rt]=0;
 43 }
 44 void mulupdate(ll l, ll r, ll add, ll left, ll right, ll rt)
 45 {
 46     if(l<=left&&r>=right)
 47     {
 48         addlazy[rt] = (addlazy[rt]*add)%mod;
 49         mullazy[rt] = (mullazy[rt]*add)%mod;
 50         ans[rt] = (add*ans[rt])%mod;
 51         return;
 52     }    
 53     ll mid = (left+right)>>1;
 54     if(mullazy[rt]!=1||addlazy[rt]>=1) PushDOWN(rt,mid,left,right);
 55     if(l<=mid) mulupdate(l,r,add,lson);
 56     if(r>mid)  mulupdate(l,r,add,rson);
 57     PushUP(rt);
 58 }
 59 void addupdate(ll l, ll r, ll add, ll left, ll right, ll rt)
 60 {
 61     if(l<=left&&r>=right)
 62     {
 63         addlazy[rt]= (addlazy[rt]+add)%mod;
 64         ans[rt] = (ans[rt] + add*(right-left+1))%mod;
 65         return;
 66     }    
 67     ll mid = (left+right)>>1;
 68     if(mullazy[rt]!=1||addlazy[rt]>=1) PushDOWN(rt,mid,left,right);
 69     if(l<=mid) addupdate(l,r,add,lson);
 70     if(r>mid)  addupdate(l,r,add,rson);
 71     PushUP(rt);
 72 }
 73 ll query(ll l, ll r, ll left, ll right, ll rt)
 74 {
 75     ll res = 0;
 76     if(l <= left&&r >= right) return ans[rt]%mod;
 77     ll mid = (left+right) >> 1;
 78     if(mullazy[rt]!=1||addlazy[rt]>=1) PushDOWN(rt, mid, left, right);
 79     if(l <= mid) res = (res+query(l,r,lson))%mod;
 80     if(r > mid) res = (res+query(l,r,rson))%mod;
 81     return res%mod;
 82 }
 83 int main()
 84 {
 85     scanf("%d%d",&n,&mod);
 86     build(1,n,1);
 87     scanf("%d",&m);
 88     while(m--)
 89     {
 90         int p;
 91         scanf("%d",&p);
 92         if(p == 1)
 93         {
 94             scanf("%d%d%d",&x,&y,&k);
 95             mulupdate(x,y,k,1,n,1);
 96         }
 97         if(p == 2)
 98         {
 99             scanf("%d%d%d",&x,&y,&k);
100             addupdate(x,y,k,1,n,1);
101         }
102         if(p == 3)
103         {
104             scanf("%d%d",&x,&y);
105             printf("%d\n",query(x,y,1,n,1));
106         }
107     }
108     return 0;
109 }

 

【luogu P2023 [AHOI2009]维护序列】 题解

标签:blank   ref   ring   lan   text   push   long   res   \n   

原文地址:https://www.cnblogs.com/MisakaAzusa/p/8763141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!