当前位置:网站首页>区区区间---线段树lazy标记板子题
区区区间---线段树lazy标记板子题
2022-07-29 02:17:00 【WAWA源】

#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';
}
}
边栏推荐
猜你喜欢

新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版

第六天笔记

物联网组件

This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.

Understand the evolution of redis architecture in one article

MySQL basic operation and comprehensive instance project based on MySQL basic operation

JMeter's BeanShell generates MD5 encrypted data and writes it to the database

云开发口袋工具箱微信小程序源码

Ten methods to prevent blackmail software from attacking data

一文搞懂 Redis 架构演化之路
随机推荐
0728~面试题梳理
What are the TCP retransmission mechanisms?
全新UI四方聚合支付系统源码/新增USDT提现/最新更新安全升级修复XSS漏洞补单漏洞
Ten methods to prevent blackmail software from attacking data
Shell 脚本 快速入门 -01
多年前的回忆
Shell script quick start-01
Never pass a request to an asynchronous thread. There is a hole
Brief answer of Engineering Economics
Why is redis fast? Message queue, single thread
Implement encapsulated public method global call in laravel framework
6-21 vulnerability exploitation MySQL weak password cracking
QT屏幕自适应自动布局,拖动窗口自动变大变小(一)
idea替换所有文件中的内容
图书管理系统
QT qstringlist usage
多重继承与派生类成员标识
白马过隙的时光
Read the recent trends of okaleido tiger and tap the value and potential behind it
【无标题】