当前位置:网站首页>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;
}
边栏推荐
- QTcpServer. QTcpSocket. Differences between qudpsockets
- Codeforces Round #798 (Div. 2)ABCD
- 报告录屏+PPT 傅云飞-喜马拉雅山脉南坡云降水特征研究
- Understanding RPC and rest
- 统计特殊子序列数目(0,1,2)DP
- Some experience in database table structure design
- Electrolytic capacitor, tantalum capacitor, ordinary capacitor
- Simple query cost estimation [Gauss is not a mathematician this time]
- 2022 tailings recurrent training question bank and simulated examination
- 2022 Gansu Province safety officer C certificate work certificate title and online simulation examination
猜你喜欢

Folder data synchronization tool sync folders Pro

Navicat connection MySQL in Pagoda

Nim game ladder Nim game and SG function application (set game)
![[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

电赛校赛经验-程控风力摆

Redis related

There is no suspense about the first one in the overtime table of the Internet company!

Nim游戏阶梯 Nim游戏和SG函数应用(集合游戏)

flutter简单优秀的开源dialog使用free_dialog

关于 SAP Spartacus CmsService.getComponentData 可能的优化思路
随机推荐
网传互联网公司加班表,排名第一的没有悬念!
Finally, the monthly income is 20000!!
宝塔中查看mysql默认密码
DNS protocol analysis
Read pgstat [this time Gauss is not a mathematician]
Necessary for Architects: system capacity status checklist
数据库学习笔记(第十五章)
状态压缩DP例题(旅行商问题和填矩形问题)
Environ. Sci. Technol.(IF=9.028) | 城市绿化对大气环境的影响
Introduction to recursive idea and implementation, eliminating recursion
View the default MySQL password in the pagoda
MySQL transaction isolation level and mvcc
【20220526】UE5.0.2 release d11782b
Count the number of special subsequences (0, 1, 2) DP
What is 400g Ethernet?
Actual combat simulation │ real time error alarm of enterprise wechat robot
Redis相关
Introduction to binary tree
[cloud enjoying freshness] community weekly · vol.66- Huawei partners and Developers Conference 2022 wonderful agenda announcement
欧拉函数和线性筛求欧拉函数