当前位置:网站首页>纪念一下第一次写的线段树了喽(对应洛谷3372)
纪念一下第一次写的线段树了喽(对应洛谷3372)
2022-07-28 20:40:00 【好好学吧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");
} 边栏推荐
- SQL注入 Less38(堆叠注入)
- Typeof principle
- 学习 Kotlin - 扩展函数
- HCIP(11)
- 科大讯飞笔试
- 【NLP】生成词云
- array_diff_assoc 元素是数组时不比较数组值的办法
- What is a prime factor? In number theory, a prime factor (prime factor or prime factor) refers to a prime number that can divide a given positive integer
- Apifox: satisfy all your fantasies about API
- LCR测试仪最为主要的功能和用途都是什么
猜你喜欢
随机推荐
Overall introduction of Ruiji takeout project
typeof原理
SQL injection less42 (post stack injection)
HCIP(15)
Lvs+keepalived high availability deployment practical application
Byte side: can TCP and UDP use the same port?
get和post的区别
从 Web3到Web2.5,是倒退还是另辟蹊径?
Learning notes and summary of C language programming specification
ECMASript 5/6 笔记
Openresty request authentication
The difference between get and post
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
2021年数学建模B组代码
HCIP(9)
[machine learning] naive Bayesian classification of text -- Classification of people's names and countries
AimBetter洞察您的数据库,DPM 和 APM 解决方案
Brief introduction to PCB materials
TensorFlow Serving 高性能的机器学习模型服务系统
Clearing of applet component timer

![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)







