P3372 【模板】线段树 1
P3372 【模板】线段树 1
题目简述
对于一段数列,有如下两种操作
1 x y k
:将区间 内每个数加上 。2 x y
:输出区间 内每个数的和。
思路
线段树裸树
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+5;
ll a[N],n,m;
ll tr[N],tag[N];
void in(ll &x){
x=0;
int f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
x*=f;
}
ll ls(ll root){
return root<<1;
}
ll rs(ll root){
return root<<1|1;
}
void push_up(ll root){
tr[root]=tr[ls(root)]+tr[rs(root)];
}
void work(ll l,ll r,ll root,ll k){
tag[root]+=k;
tr[root]+=(r-l+1)*k;
}
void push_down(ll l,ll r,ll root){
ll mid=(l+r)>>1;
work(l,mid,ls(root),tag[root]);
work(mid+1,r,rs(root),tag[root]);
tag[root]=0;
}
void build(ll root,ll l,ll r){
ll mid=(l+r)>>1;
if(l==r){
tr[root]=a[l];
return ;
}
build(ls(root),l,mid);
build(rs(root),mid+1,r);
push_up(root);
return ;
}
void update(ll nl,ll nr,ll l,ll r,ll root,ll num){
if(l>=nl&&r<=nr){
tr[root]+=(r-l+1)*num;
tag[root]+=num;
return ;
}
//cout<<l<<' '<<r<<endl;
push_down(l,r,root);
ll mid=(l+r)>>1;
if(mid>=nl)update(nl,nr,l,mid,ls(root),num);
if(mid<nr)update(nl,nr,mid+1,r,rs(root),num);
push_up(root);
return ;
}
ll query(ll nl,ll nr,ll l,ll r,ll root){
ll ans=0;
if(l>=nl&&r<=nr)return tr[root];
ll mid=(l+r)>>1;
push_down(l,r,root);
if(mid>=nl)ans+=query(nl,nr,l,mid,ls(root));
if(mid<nr)ans+=query(nl,nr,mid+1,r,rs(root));
return ans;
}
int main(){
freopen("3372.in","r",stdin);
freopen("3372.out","w",stdout);
in(n);in(m);
for(int i=1;i<=n;i++)in(a[i]);
build (1,1,n);
for(int i=1;i<=m;i++){
ll op;
in(op);
switch(op){
case 1:{
ll x,y,k;
in(x);in(y);in(k);
update(x,y,1,n,1,k);
break;
}
case 2:{
ll x,y;
in(x);in(y);
cout<<query(x,y,1,n,1)<<endl;
break;
}
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】