当前位置:网站首页>Interval modification multiplication and addition (a good example of understanding lazy tags)
Interval modification multiplication and addition (a good example of understanding lazy tags)
2022-06-13 11:03:00 【I can screw the bottle cap when I am born again】

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
int n,m,p;
int a[N];
struct Node{
int l,r;
int sum,add,mul;
}tr[N*4];
void pushup(int u)
{
tr[u].sum = (tr[u<<1].sum+tr[u<<1|1].sum)%p;
}
void eval(Node &t,int add,int mul)
{
t.sum = ((LL)t.sum*mul+(LL)(t.r-t.l+1)*add)%p;
t.mul = (LL) t.mul*mul%p;
t.add = ((LL)t.add*mul+add)%p;
}
void pushdown (int u)
{
eval(tr[u<<1],tr[u].add,tr[u].mul);
eval(tr[u<<1|1],tr[u].add,tr[u].mul);
tr[u].add=0,tr[u].mul = 1;
}
void build(int u,int l,int r)
{
if (l==r) tr[u] = {
l,r,a[r],0,1};
else
{
tr[u] = {
l,r,0,0,1};
int mid = l+r>>1;
build (u<<1,l,mid); build(u<<1|1,mid+1,r);
pushup(u);
}
}
void modify(int u,int l,int r,int add,int mul)
{
if (tr[u].l>=l&&tr[u].r<=r) eval(tr[u],add,mul);
else
{
pushdown(u);
int mid = tr[u].l+tr[u].r>>1;
if (l<=mid) modify(u<<1,l,r,add,mul);
if (r>mid) modify(u<<1|1,l,r,add,mul);
pushup(u);
}
}
int query(int u,int l,int r)
{
if (tr[u].l>=l&&tr[u].r<=r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l+tr[u].r>>1;
int sum=0;
if (l<=mid) sum=query(u<<1,l,r)%p;
if (r>mid) sum=(sum+query(u<<1|1,l,r))%p;
return sum;
}
int main()
{
scanf("%d%d", &n, &p);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
build(1,1,n);
scanf("%d", &m);
while (m -- )
{
int t,l,r,d;
scanf("%d%d%d",&t,&l,&r);
if (t==1)
{
scanf("%d", &d);
modify(1,l,r,0,d);
}
else if (t==2)
{
scanf("%d", &d);
modify(1,l,r,d,1);
}
else
printf ("%d\n",query(1,l,r));
}
return 0;
}
边栏推荐
- 避免让转型企业走入歧途,是时候重新理解下湖仓一体了!| Q推荐
- Understanding RPC and rest
- 宝塔中查看mysql默认密码
- 高斯消元求n元方程组
- Vivo large scale kubernetes cluster automation operation and maintenance practice
- 乘法逆元作用
- What is 400g Ethernet?
- Pagoda access changed from IP to domain name
- Do you agree that the salary of hardware engineers is falsely high?
- Talk about MySQL indexing mechanism
猜你喜欢

欧拉函数和线性筛求欧拉函数

Easyclick run code snippet out null

Idea remote debugging jar submitted by spark submit

基于Vue+Nest.js+MySQL的跨平台开源社区运营管理系统

Advanced technology management - what management tools can managers use
![[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code](/img/e3/a299393e865104d96341ce3d93ac6b.png)
[elm classification] data classification based on particle swarm optimization convolution neural network CNN combined with limit learning machine elm with matlab code

数位DP例题

ST表学习

Database system concept (Chapter 17)

数据库系统概念(第十七章)
随机推荐
Four methods of finding combinatorial numbers
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
Understanding RPC and rest
[dynamic planning] beginner level
Talk about MySQL indexing mechanism
Vivo large scale kubernetes cluster automation operation and maintenance practice
DNS protocol analysis
EasyClick 运行代码片段出Null
第七章 文件管理作业
微众银行OSPO建设之路:如何通过OSPO的建设推动企业开源?
Determine the maximum match between bipartite graph and bipartite graph
21世纪以来的历次“粮食危机”,发生了什么?
2022 tailings recurrent training question bank and simulated examination
Brief description of redo logs and undo logs in MySQL
C# 文件打包下载
基于Vue+Nest.js+MySQL的跨平台开源社区运营管理系统
终于,月入 20000 !!
Ue5 random point in bounding boxf from stream
COM的模式变化引起的IPdu Handling【接收截止日期监控】
Vivo large scale kubernetes cluster automation operation and maintenance practice