当前位置:网站首页>Remember the first line segment tree (corresponding to Luogu 3372)
Remember the first line segment tree (corresponding to Luogu 3372)
2022-07-28 22:26:00 【Study hard 867】
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct tree{
ll data;
ll l;
ll r;
ll lazy=0;
};
tree a[400040];
ll b[100010];
ll wh[400040];
void pushdown(ll i){
if(a[i].lazy!=0){
if(a[i].l!=a[i].r){
ll ch=i<<1;
a[ch].lazy+=a[i].lazy;
a[ch+1].lazy+=a[i].lazy;
a[i].data+=(a[i].r-a[i].l+1)*a[i].lazy;
}
else a[i].data+=a[i].lazy;
a[i].lazy=0;
}
}
void update(ll i){
ll ch=i<<1;
pushdown(ch);
pushdown(ch+1);
a[i].data=a[ch].data+a[ch+1].data;
return ;
}
void build(ll l,ll r,ll i){
a[i].l=l;
a[i].r=r;
if(l==r){
a[i].data=b[l];
return ;
}
ll ch=i<<1;
ll mid=(l+r)>>1;
build(l,mid,ch);
build(mid+1,r,ch+1);
if(l!=r)update(i);
}
void change(ll l,ll r,ll i,ll add){
pushdown(i);
if(a[i].l>=l&&a[i].r<=r){
a[i].lazy+=add;
return ;
}
ll mid=(a[i].l+a[i].r)>>1;
ll ch=i<<1;
if(r<=mid){
change(l,r,ch,add);
}
else if(l>mid){
change(l,r,ch+1,add);
}
else{
change(l,mid,ch,add);
change(mid+1,r,ch+1,add);
}
update(i);
}
ll getsum(ll l,ll r,ll i){
pushdown(i);
if(a[i].l>=l&&a[i].r<=r){
return a[i].data;
}
ll mid=(a[i].l+a[i].r)>>1;
ll ch=i<<1;
if(r<=mid){
return getsum(l,r,ch);
}
else if(l>mid){
return getsum(l,r,ch+1);
}
else return getsum(l,mid,ch)+getsum(mid+1,r,ch+1);
}
int main(){
ll n,m;
scanf("%lld%lld",&n,&m);
ll i;
for(i=1;i<=n;i++){
scanf("%lld",&b[i]);
}
build(1,n,1);
while(m--){
ll way;
scanf("%d",&way);
switch(way){
case 1:{
ll l,r,k;
scanf("%lld%lld%lld",&l,&r,&k);
change(l,r,1,k);
break;
}
case 2:{
ll x,y;
scanf("%lld%lld",&x,&y);
cout<<getsum(x,y,1)<<endl;
break;
}
}
}
system("pause");
} 边栏推荐
猜你喜欢
随机推荐
How to realize dynamic route switching and route caching in vuejs
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
Lvs+keepalived high availability deployment practical application
Bugku,Web:都过滤了
Ordinary practice of JS DOM programming
What testing services do third-party software testing institutions provide? Charging standard of software test report
[CS231N]Lecture_2:Image Classification pipelin
40. Combined sum II
Excel-VBA 快速上手(十三、日期的常见用法)
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
静态路由和缺省路由实验
Sword finger offer II 058. schedule (medium design segment tree treemap ordered set)
Brief introduction to PCB materials
The person in charge of Tencent cloud database borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
Ukrainian officials: half of Ukrainian agricultural products are exported through the Danube port
vuejs中如何实现动态路由切换及路由的缓存
Clearing of applet component timer
[leetcode] maximum depth of binary tree
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation





![[CS231N]Lecture_2:Image Classification pipelin](/img/4f/de56b071560ada746c587a9dbc5f02.jpg)



