博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【luogu P2023 [AHOI2009]维护序列】 题解
阅读量:5249 次
发布时间:2019-06-14

本文共 3426 字,大约阅读时间需要 11 分钟。

题目链接:

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

1 #include 
2 #include
3 #include
4 #include
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 }

 

转载于:https://www.cnblogs.com/MisakaAzusa/p/8763141.html

你可能感兴趣的文章
bzoj 3996 最小割
查看>>
每天的记录1-padStart(); padEnd();Object.values();Object.entries()
查看>>
Golang从零开始(二):命名规范、变量和常量
查看>>
WeGeek直播课:从0到1快速开发电商小程序(文字版)
查看>>
iOS 2019最新Jenkins集成gitblit实现自动打包攻略
查看>>
Python 操作 MySQL 的5种方式
查看>>
ffiddler抓取手机(app)https包
查看>>
【Python】 SQLAlchemy的初步使用
查看>>
javaweb本地启动很快,服务器上面启动特别慢
查看>>
一张图看明白部标808协议
查看>>
n-Queens(n皇后)问题的简单回溯
查看>>
Python爬虫实战之抓取淘宝MM照片
查看>>
python爬虫学习之用Python抢火车票的简单小程序
查看>>
python爬虫学习教程,用python爬取新浪微博数据
查看>>
python爬虫学习之淘宝模拟登录
查看>>
python爬虫学习之爬取超清唯美壁纸
查看>>
python爬虫学习之爬取169图片网站
查看>>
python爬虫学习之大批量抓取京东商品id和标签
查看>>
零基础如何学好python之Python代码规范,简明概述!
查看>>
零基础如何学好python?Python代码规范之注释
查看>>