[P2023][AHOI2009]维护序列
来源:互联网 发布:知乎机构号注册流程 编辑:程序博客网 时间:2024/05/17 03:08
原题链接
线段树
加乘混合修改
主要是lazy的pushdown
代码意义明确就不解释了
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<queue>#include<vector>#include<climits>#include<string>#include<cstdlib>#include<ctime>#define MOD 1000000007#define LL long longusing namespace std;struct nico{ LL lef,rig,len,lazy1,lazy2,sum;}point[400005];LL n,P;LL a[100005];void build(LL l,LL r,LL p){ LL mid; point[p].lef=l; point[p].rig=r; point[p].len=r-l+1; point[p].lazy2=1; if(l==r) { point[p].sum=a[l]%P; return; } mid=(l+r)>>1; build(l,mid,p<<1); build(mid+1,r,p<<1|1); point[p].sum=(point[p<<1].sum+point[p<<1|1].sum)%P;}void pushdown(LL p){ point[p<<1].sum=((point[p<<1].sum*point[p].lazy2)%P+(point[p].lazy1*point[p<<1].len)%P)%P; point[p<<1|1].sum=((point[p<<1|1].sum*point[p].lazy2)%P+(point[p].lazy1*point[p<<1|1].len)%P)%P; point[p<<1].lazy1=(point[p<<1].lazy1*point[p].lazy2+point[p].lazy1)%P; point[p<<1|1].lazy1=(point[p<<1|1].lazy1*point[p].lazy2+point[p].lazy1)%P; point[p<<1].lazy2=(point[p<<1].lazy2*point[p].lazy2)%P; point[p<<1|1].lazy2=(point[p<<1|1].lazy2*point[p].lazy2)%P; point[p].lazy1=0; point[p].lazy2=1;}void add(LL l,LL r,LL x,LL p){ LL mid; if(l==point[p].lef&&r==point[p].rig) { point[p].lazy1=(point[p].lazy1+x)%P; point[p].sum=(point[p].sum+((x%P)*(point[p].len%P))%P)%P; return; } pushdown(p); mid=(point[p].lef+point[p].rig)>>1; if(l>mid) add(l,r,x,p<<1|1); else if(r<=mid) add(l,r,x,p<<1); else if(l>=point[p].lef&&r<=point[p].rig) {add(l,mid,x,p<<1);add(mid+1,r,x,p<<1|1);} point[p].sum=(point[p<<1].sum+point[p<<1|1].sum)%P; }void change(LL l,LL r,LL x,LL p){ LL mid; if(l==point[p].lef&&r==point[p].rig) { point[p].lazy1=(point[p].lazy1*x)%P; point[p].lazy2=(point[p].lazy2*x)%P; point[p].sum=((point[p].sum%P)*(x%P))%P; return; } pushdown(p); mid=(point[p].lef+point[p].rig)>>1; if(l>mid) change(l,r,x,p<<1|1); else if(r<=mid) change(l,r,x,p<<1); else if(l>=point[p].lef&&r<=point[p].rig) {change(l,mid,x,p<<1);change(mid+1,r,x,p<<1|1);} point[p].sum=(point[p<<1].sum+point[p<<1|1].sum)%P; }LL ask(LL l,LL r,LL p){ LL mid; if(l==point[p].lef&&r==point[p].rig) return point[p].sum%P; pushdown(p); mid=(point[p].lef+point[p].rig)>>1; if(l>mid) return ask(l,r,p<<1|1); else if(r<=mid) return ask(l,r,p<<1); else if(l>=point[p].lef&&r<=point[p].rig) return (ask(l,mid,p<<1)%P)+(ask(mid+1,r,p<<1|1)%P); }LL m,i,f,t,g,c,ans;int main(){ scanf("%lld%lld",&n,&P); for(i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,n,1); scanf("%lld",&m); for(i=1;i<=m;i++) { scanf("%lld",&f); if(f==1) { scanf("%lld%lld%lld",&t,&g,&c); change(t,g,c,1); } if(f==2) { scanf("%lld%lld%lld",&t,&g,&c); add(t,g,c,1); } if(f==3) { scanf("%lld%lld",&t,&g); ans=ask(t,g,1)%P; printf("%lld\n",ans); } } return 0;}
阅读全文
0 0
- [P2023][AHOI2009]维护序列
- 洛谷P2023 [AHOI2009]维护序列
- [洛谷luogu] P2023 [AHOI2009]维护序列
- 【bzoj1798】【洛谷P2023】【AHOI2009】维护序列
- BZOJ1798 洛谷P2023 [AHOI2009]维护序列
- P2023 [AHOI2009]维护序列 --线段树
- 洛谷P2023 [AHOI2009]维护序列 (BZOJ1798)
- 洛谷P2023 && bzoj1798 [AHOI2009]维护序列
- 洛谷 P2023 [AHOI2009]维护序列(bzoj P1798 [Ahoi2009]Seq 维护序列seq)
- bzoj1798【ahoi2009】维护序列
- 【AHOI2009】bzoj1798 维护序列
- 【BZOJ1798】【AHOI2009】维护序列
- [AHOI2009]维护序列
- 【bzoj 1798】[AHOI2009]维护序列
- [Ahoi2009]Seq 维护序列seq
- [Ahoi2009]Seq 维护序列seq
- jzoj P1663【AHOI2009】维护序列
- [BZOJ3146][AHOI2009]维护序列seq
- 数据中心里的那些XDC们
- 输入n,求斐波那契数列第n项。斐波那契数列的第0项为0,第一项为1,之后的项依次为前两项之和。
- 金融企业软件测试中心筹备书
- 位运算基本概念及简单运用
- 面向5G的数据中心建、维、营
- [P2023][AHOI2009]维护序列
- jackson 完美用法
- JAVA项目连接到JDBC
- 欢迎使用CSDN-markdown编辑器
- dataFilter
- RxJava
- Problem
- Java并发编程实战(学习笔记五 第六章 任务执行)
- Django 的 URL name