当前位置:网站首页>Zone --- line segment tree lazy marking board sub problem
Zone --- line segment tree lazy marking board sub problem
2022-07-29 02:48:00 【Wawa source】

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
#define int long long
int n,m;
int w[N];
struct node
{
int l,r;
int sum,tag;
}tr[N*4];
int cal(int a1,int len)
{
int an=a1+len-1;
return (a1+an)*len/2;
}
void pushup(int u)
{
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void pushdown(int u)
{
if(tr[u].tag)
{
tr[u<<1].tag=tr[u].tag;
tr[u<<1|1].tag=tr[u].tag+tr[u<<1].r-tr[u<<1].l+1;
tr[u<<1].sum=cal(tr[u<<1].tag,tr[u<<1].r-tr[u<<1].l+1);
tr[u<<1|1].sum=cal(tr[u<<1|1].tag,tr[u<<1|1].r-tr[u<<1|1].l+1);
tr[u].tag=0;
}
}
void build(int u,int l,int r)
{
if(l==r)tr[u]={
l,r,w[l],0};
else
{
tr[u]={
l,r};
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 v)
{
if(tr[u].l>=l&&tr[u].r<=r)
{
tr[u].tag=tr[u].l-l+v;
tr[u].sum=cal(tr[u].tag,tr[u].r-tr[u].l+1);
return ;
}
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid)modify(u<<1,l,r,v);
if(r>mid)modify(u<<1|1,l,r,v);
pushup(u);
}
int query(int u,int l,int r)
{
if(tr[u].l>=l&&tr[u].r<=r)return tr[u].sum;
int res=0;
int mid=tr[u].l+tr[u].r>>1;
pushdown(u);
if(l<=mid)res+=query(u<<1,l,r);
if(r>mid)res+=query(u<<1|1,l,r);
return res;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>w[i];
build(1,1,n);
int op,l,r;
while(m--)
{
cin>>op>>l>>r;
if(op==1)
{
int k;cin>>k;
modify(1,l,r,k);
}
else cout<<query(1,l,r)<<'\n';
}
}
边栏推荐
- Driverless obstacle avoidance technology
- QT screen adaptive automatic layout, drag the window to automatically grow larger and smaller (I)
- New UI Sifang aggregate payment system source code / new usdt withdrawal / latest update security upgrade to fix XSS vulnerability patch vulnerability
- 双for循环
- STM32C8T6编码器电机测速与arduino光电模块测速
- Memories of many years ago
- ECCV 2022 | airdet: a small sample target detection method without fine tuning
- 云开发口袋工具箱微信小程序源码
- 别人的快乐
- 优炫软件任命黄志军为公司总经理
猜你喜欢

Asemi rectifier bridge s25vb100, s25vb100 parameters, s25vb100 application

优炫软件任命黄志军为公司总经理

第五天实验

New UI Sifang aggregate payment system source code / new usdt withdrawal / latest update security upgrade to fix XSS vulnerability patch vulnerability

HTB-Blocky

Shell script quick start-01

Multiple inheritance and derived class member identification

idea配置web容器与war打包

自动分账系统哪家好?

Rocbos open source micro community light forum source code
随机推荐
idea配置web容器与war打包
Where, having, group by, order by, is null, not in, subquery, delete, date function
golang 协程的实现原理
C language: hollow square pattern
MPEG音频编码三十年
Library management system
Mqtt routine
云开发口袋工具箱微信小程序源码
C language: Little Lele and Euclid
Why is redis fast? Message queue, single thread
Happy childhood
Youxuan software appoints Huang Zhijun as the general manager of the company
多重继承与派生类成员标识
(job) C language: Simulation Implementation of ATOI and strncpy, strncat, strncmp
Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
really time ntp服务启动命令
h. 264 code stream explanation
云开发打工人必备上班摸鱼划水微信小程序源码
这个博主,qt归类比较全,有空去学习总结,记录一下。
Driverless obstacle avoidance technology