OJ题号:洛谷2023、BZOJ1798

思路:

参见[洛谷3373]【模板】线段树 2

 #include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100001
#define root 1
#define _left <<1
#define _right <<1|1
int n,m;
ll mod;
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=((x+(x<<))<<)+(ch^'');
return x;
}
inline ll getll() {
char ch;
while(!isdigit(ch=getchar()));
ll x=ch^'';
while(isdigit(ch=getchar())) x=((x+(x<<))<<)+(ch^'');
return x;
}
struct SegmentTree {
ll val[maxn<<],add[maxn<<],mul[maxn<<];
void push_up(const int p) {
val[p]=(val[p _left]+val[p _right])%mod;
}
void build(const int p,const int b,const int e) {
mul[p]=;
add[p]=;
if(b==e) {
val[p]=getll()%mod;
return;
}
int mid=(b+e)>>;
build(p _left,b,mid);
build(p _right,mid+,e);
push_up(p);
}
int length(const int b,const int e) {
return e-b+;
}
void push_down(const int p,const int b,const int e) {
if(mul[p]!=) {
val[p _left]=val[p _left]*mul[p]%mod;
val[p _right]=val[p _right]*mul[p]%mod;
mul[p _left]=mul[p _left]*mul[p]%mod;
mul[p _right]=mul[p _right]*mul[p]%mod;
add[p _left]=add[p _left]*mul[p]%mod;
add[p _right]=add[p _right]*mul[p]%mod;
mul[p]=;
}
if(add[p]) {
int mid=(b+e)>>;
val[p _left]=(val[p _left]+add[p]*length(b,mid))%mod;
val[p _right]=(val[p _right]+add[p]*length(mid+,e))%mod;
add[p _left]=(add[p _left]+add[p])%mod;
add[p _right]=(add[p _right]+add[p])%mod;
add[p]=;
}
}
void modify_mul(const int p,const int b,const int e,const int l,const int r,const ll x) {
if((b==l)&&(e==r)) {
val[p]=val[p]*x%mod;
mul[p]=mul[p]*x%mod;
add[p]=add[p]*x%mod;
return;
}
push_down(p,b,e);
int mid=(b+e)>>;
if(l<=mid) modify_mul(p _left,b,mid,l,min(mid,r),x);
if(r>mid) modify_mul(p _right,mid+,e,max(mid+,l),r,x);
push_up(p);
}
void modify_add(const int p,const int b,const int e,const int l,const int r,const ll x) {
if((b==l)&&(e==r)) {
val[p]=(val[p]+x*length(b,e))%mod;
add[p]=(add[p]+x)%mod;
return;
}
push_down(p,b,e);
int mid=(b+e)>>;
if(l<=mid) modify_add(p _left,b,mid,l,min(mid,r),x);
if(r>mid) modify_add(p _right,mid+,e,max(mid+,l),r,x);
push_up(p);
}
ll query(const int p,const int b,const int e,const int l,const int r) {
if((b==l)&&(e==r)) {
return val[p];
}
int mid=(b+e)>>;
ll ans=;
push_down(p,b,e);
if(l<=mid) ans=(ans+query(p _left,b,mid,l,min(mid,r)))%mod;
if(r>mid) ans=(ans+query(p _right,mid+,e,max(mid+,l),r))%mod;
return ans;
}
};
SegmentTree tree;
int main() {
n=getint();
mod=getll();
tree.build(root,,n);
m=getint();
while(m--) {
int op=getint(),x=getint(),y=getint();
if(op==) {
ll k=getll()%mod;
tree.modify_mul(root,,n,x,y,k);
continue;
}
if(op==) {
ll k=getll()%mod;
tree.modify_add(root,,n,x,y,k);
continue;
}
printf("%lld\n",tree.query(root,,n,x,y));
}
return ;
}

最新文章

  1. 恶心的hadoop集群
  2. python——第一天
  3. NOI题库 09:图像旋转翻转变换
  4. [HTML]DIV+CSS 文字垂直居中
  5. JavaScript之canvas
  6. CodeForces 682C Alyona and the Tree (树+dfs)
  7. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器
  8. Applet签名
  9. 【Luogu1919】 A*B Problem升级版(FFT)
  10. POJ-1751 Highways---确定部分边的MST
  11. Skin Microstructure Deformation with Displacement Map Convolution项目小结
  12. TypeError: value.getTime is not a function (elementUI报错转载 )
  13. opengl库学习
  14. 新建maven遇到的错误
  15. 在 ASP.NET MVC 中使用异步控制器
  16. 【转载并整理】mysql分页方法
  17. 【python】globle的使用
  18. CentOS6.5(1)----设置静态IP并禁用IPV6
  19. 加号选择器(ul&gt;li + li)
  20. 动态绑数据(GridView控件Header和ItemTemplate)

热门文章

  1. 配置webpack loader vue 报错:Module build failed: TypeError: this._init is not a function
  2. java数组元素的复制
  3. Python sendmail
  4. EF Core Fluent API
  5. TFS 生成任务报错:目录不是空的
  6. 452. Minimum Number of Arrows to Burst Balloons
  7. SpringBank 开发日志 使用maven构建dubbo服务的可执行jar包
  8. [转]解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
  9. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-1目录
  10. Python学习(二十七)—— Django和pymysql搭建学员管理系统