【题目链接】

点击打开链接

【算法】

线段树

注意标记下传

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 500000 typedef long long LL; struct SegTreeNode {
LL l,r,sum,lazya,lazyb;
}tree[MAXN+]; LL i,N,P,Q,l,r,x,opt;
LL a[MAXN+]; template <typename T> void read(T &x) {
LL f=; char c = getchar(); x=;
for (; !isdigit(c); c = getchar()) { if (c=='-') f=-; }
for (; isdigit(c); c = getchar()) x=x*+c-'';
x*=f;
} inline void write(LL x) {
if (x < ) {
putchar('-');
x = -x;
}
if (x / > ) write(x/);
putchar(x%+'');
} inline void pushup(LL index) {
tree[index].sum = (tree[index*].sum + tree[index*+].sum) % P;
} inline void pushdown(LL index) {
tree[index*].sum = (tree[index*].sum * tree[index].lazya + tree[index].lazyb * (tree[index*].r - tree[index*].l + )) % P;
tree[index*+].sum = (tree[index*+].sum * tree[index].lazya + tree[index].lazyb * (tree[index*+].r - tree[index*+].l + )) % P;
tree[index*].lazya = (tree[index*].lazya * tree[index].lazya) % P;
tree[index*+].lazya = (tree[index*+].lazya * tree[index].lazya) % P;
tree[index*].lazyb = (tree[index*].lazyb * tree[index].lazya + tree[index].lazyb) % P;
tree[index*+].lazyb = (tree[index*+].lazyb * tree[index].lazya + tree[index].lazyb) % P;
tree[index].lazya = ; tree[index].lazyb = ;
} inline void build(LL index,LL l,LL r) {
LL mid;
tree[index].l = l;
tree[index].r = r;
tree[index].lazya = ;
if (l == r) tree[index].sum = a[l] % P;
else {
mid = (l + r) >> ;
build(index*,l,mid);
build(index*+,mid+,r);
pushup(index);
}
} inline void changea(int index,int l,int r,int x) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) {
tree[index].sum = tree[index].sum * x % P;
tree[index].lazya = (tree[index].lazya * x) % P;
tree[index].lazyb = (tree[index].lazyb * x) % P;
}else {
if (tree[index].l == tree[index].r) return;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) changea(index*,l,r,x);
else if (l >= mid + ) changea(index*+,l,r,x);
else {
changea(index*,l,mid,x);
changea(index*+,mid+,r,x);
}
pushup(index);
}
} inline void changeb(LL index,LL l,LL r,LL x) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) {
tree[index].sum = (tree[index].sum + x * (tree[index].r - tree[index].l + )) % P;
tree[index].lazyb = (tree[index].lazyb + x) % P;
}else {
if (tree[index].l == tree[index].r) return;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) changeb(index*,l,r,x);
else if (l >= mid + ) changeb(index*+,l,r,x);
else {
if (tree[index].l == tree[index].r) return;
changeb(index*,l,mid,x);
changeb(index*+,mid+,r,x);
}
pushup(index);
}
} inline LL query(LL index,LL l,LL r) {
LL mid;
pushdown(index);
if ((tree[index].l == l) && (tree[index].r == r)) return tree[index].sum;
else {
if (tree[index].l == tree[index].r) return ;
mid = (tree[index].l + tree[index].r) >> ;
if (r <= mid) return query(index*,l,r);
else if (l >= mid + ) return query(index*+,l,r);
else return (query(index*,l,mid) + query(index*+,mid+,r)) % P;
}
} int main() { read(N); read(P); for (i = ; i <= N; i++) read(a[i]); build(,,N); read(Q); while (Q--) {
read(opt);
if (opt == ) {
read(l); read(r); read(x);
changea(,l,r,x);
}else if (opt == ) {
read(l); read(r); read(x);
changeb(,l,r,x);
}else {
read(l); read(r);
write(query(,l,r));
puts("");
}
} return ; }

最新文章

  1. CodeIgniter 发送邮件
  2. ajax请求超时时间
  3. listview定位到上次显示的位置
  4. Android从相册中获取图片以及路径
  5. VisualStudio中的代码段
  6. jq向webApi提交post json数据
  7. memcached常用命令
  8. Day 2 Python数值计算
  9. python自动化开发-[第二十五天]-scrapy进阶与flask使用
  10. 20180309 - C# demo - 1
  11. Deque 双端队列 Stack 堆栈
  12. P1375 小猫(二飞的小憨猫)
  13. DVWA安装——一个菜鸟的入门教程
  14. Backpropagation In Convolutional Neural Networks
  15. php使用ffmpeg向视频中添加文字字幕
  16. UVA - 11995 - I Can Guess the Data Structure! STL 模拟
  17. WIN7下为Editplus添加右键打开
  18. UIScrollView中的手势
  19. 【机器学习笔记】自组织映射网络(SOM)
  20. Javascript备忘录-枚举一个对象的所有属

热门文章

  1. 2017 ACM/ICPC Asia Regional Urumuqi Online 记录
  2. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)
  3. 洛谷 P3865 【模板】ST表
  4. IntelliJ IDEA设置properties文件显示中文
  5. 11.Java web&mdash;servlet
  6. [转] sql 删除表数据的drop、truncate和delete用法
  7. 关于PDF的读取与绘制
  8. ScSPM
  9. 基于 orange(nginx+openresty) + docker 实现微服务 网关功能
  10. Java并发编程(三)volatile域