ragwort @ 2023-02-17 18:55:44
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+10;
int a[N],n,m;
int add[N*4];
int t[N*4];
inline void pushdown(int k,int l,int r,int mid){
if(!add[k]) return;
t[k*2] += (mid-l+1)*add[k];
add[k*2] += add[k];
t[k*2+1] += (r-mid)*add[k];
add[k*2+1] += add[k];
add[k] = 0;
}
inline void build(int k,int l,int r){
if(l == r){
t[k] = a[l];
return ;
}
int mid = l + r >> 1;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
t[k] = t[k*2]+t[k*2+1];
}
inline void update(int k,int l,int r,int x,int y,int v){
if(l == r){
t[k] += (r-l+1)*v;
add[k] += v;
return ;
}
int mid = l + r >> 1;
pushdown(k,l,r,mid);
if(x <= mid) update(k*2,l,mid,x,y,v);
if(y > mid) update(k*2+1,mid+1,r,x,y,v);
t[k] = t[k*2] + t[k*2+1];
}
inline int query(int k,int l,int r,int x,int y){
if(x <= l && y >= r) return t[k];
int mid = l+r>>1,res=0;
pushdown(k,l,r,mid);
if(x <= mid) res = query(k*2,l,mid,x,y);
if(y > mid) res += query(k*2+1,mid+1,r,x,y);
return res;
}
signed main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
build(1,1,n);
int x,y,z;
while(m--){
cin >> x;
if(x == 2){
cin >> x >> y;
cout << query(1,1,n,x,y) << endl;
}else{
cin >> x >> y >> z;
update(1,1,n,x,y,z);
}
}
return 0;
}
by Claire0918 @ 2023-02-17 18:56:51
@wind_kaka 用scanf
和printf
。
by ragwort @ 2023-02-17 18:58:10
@Claire0918 ok
by L7_56 @ 2023-02-17 19:00:25
update里面的终止条件应该是 x <= l && r <= y
by zyl19416 @ 2023-02-17 19:01:11
update写错了吧
by GARYVVN @ 2023-02-17 19:02:16
把long long 改成int试试?
by Light_az @ 2023-02-17 19:04:11
@wind_kaka 学了一周怎么还没会
by ragwort @ 2023-02-17 19:51:22
@lianzhuo 不是,这周我只学了一个晚上,平时写作业/dk
by ragwort @ 2023-02-17 19:51:39
@zyl19416 @L7_56 谢谢,wssb
by Light_az @ 2023-02-17 19:52:13
@wind_kaka 但是你的线段树笔记写了好久啊
by ragwort @ 2023-02-17 19:53:04
@lianzhuo 早写好了(