当前位置:网站首页>区间修改乘和加(理解懒标记的好例题)
区间修改乘和加(理解懒标记的好例题)
2022-06-13 10:57:00 【重生之我会拧瓶盖】

#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;
}
边栏推荐
- Implementation of singleton mode
- Modification of string class object
- The first laravel workflow engine released the official version of v1.0
- WinForm resolves frequent refresh of black screen
- There is no suspense about the first one in the overtime table of the Internet company!
- Test cases that testers must master
- Folder data synchronization tool sync folders Pro
- DNS协议分析
- vivo大规模 Kubernetes 集群自动化运维实践
- Do you agree that the salary of hardware engineers is falsely high?
猜你喜欢

第七章 文件管理作业

spark源码(一)spark-submit如何将jar以及配置参数提交给spark服务器

Multithreading starts from the lockless queue of UE4 (thread safe)

Go zero microservice Practice Series (III. API definition and table structure design)

Use of servers

Private computing fat core concepts and stand-alone deployment

【20220526】UE5.0.2 release d11782b

Vivo large scale kubernetes cluster automation operation and maintenance practice

deepin系统中Qt5.12无法输入中文(无法切换中文输入法)解决办法

Install Kubernetes 1.24
随机推荐
宝塔访问从IP改为域名
Pagoda add a website: PHP project
Install Kubernetes 1.24
《气候韧性和可持续性》| 新研究表明超级飓风未来几年会对南亚产生更大破坏
Easyclick run code snippet out null
Vivo large scale kubernetes cluster automation operation and maintenance practice
SSM整合初步 所得细节
宝塔添加一个网站:PHP项目
上周热点回顾(6.6-6.12)
go-zero微服务实战系列(三、API定义和表结构设计)
Database learning notes (Chapter 15)
电赛校赛经验-程控风力摆
阿里一季度员工减少4000人;程序员写脚本抢挂疫苗号,牟利40万被刑拘;搜狐遭遇史诗级邮件诈骗,张朝阳回应 | Q资讯
Idea remote debugging jar submitted by spark submit
数据库学习笔记(第十五章)
Simple query cost estimation [Gauss is not a mathematician this time]
C file package and download
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation
作为一个测试人员,这些基础知识必不可少
第七章 文件管理作业