Guizy @ 2023-10-03 19:11:14
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[400001],x,y,k,cnt;
int tree[400001],flag,flag2;
int lzy_tag[400001],n,m;
void build(int l,int r,int id){
if(l==r){
tree[id]=a[l];
return ;
}
else{
int mid=(l+r)>>1;
build(l,mid,id*2);
build(mid+1,r,id*2+1);
tree[id]=tree[id*2]+tree[id*2+1];
return ;
}
return ;
}
void push_lzyt(int id,int len){
int k=lzy_tag[id];
lzy_tag[id]=0;
lzy_tag[id*2]+=k;
lzy_tag[id*2+1]+=k;
tree[id*2]+=k*(len-len/2);
tree[id*2+1]+=k*(len/2);
}
void upd(int l,int r,int id){
if(l==r&&l>=x&&r<=y){
tree[id]+=k;
}
else{
if(l>y||r<x){
return;
}
if(l>=x&&r<=y){
tree[id]+=k*(r-l+1);
if(l!=r) lzy_tag[id]=k;
}
else{
int mid=(l+r)>>1;
push_lzyt(id,r-l+1);
upd(l,mid,id*2);
upd(mid+1,r,id*2+1);
tree[id]=tree[id*2]+tree[id*2+1];
}
}
}
int ask(int l,int r,int id){
if(l>y||r<x){
return 0;
}
else if(l>=x&&r<=y){
return tree[id];
}
else{
push_lzyt(id,r-l+1);
int mid=(l+r)>>1;
return ask(l,mid,id*2)+ask(mid+1,r,id*2+1);
}
}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",a+i);
}
build(1,n,1);
while(m--){
scanf("%lld%lld%lld",&flag,&x,&y);
if(flag==1){
scanf("%lld",&k);
upd(1,n,1);
}
else{
flag2=0;
printf("%lld\n",ask(1,n,1));
}
}
return 0;
}
by Eltaos_xingyu @ 2023-10-03 19:37:53
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[400001],x,y,k,cnt;
int tree[400001],flag,flag2;
int lzy_tag[400001],n,m;
void build(int l,int r,int id){
if(l==r){
tree[id]=a[l];
return ;
}
else{
int mid=(l+r)>>1;
build(l,mid,id*2);
build(mid+1,r,id*2+1);
tree[id]=tree[id*2]+tree[id*2+1];
return ;
}
return ;
}
void push_lzyt(int id,int len){
int k=lzy_tag[id];
lzy_tag[id]=0;
lzy_tag[id*2]+=k;
lzy_tag[id*2+1]+=k;
tree[id*2]+=k*(len-len/2);
tree[id*2+1]+=k*(len/2);
}
void upd(int l,int r,int id){
if(l==r&&l>=x&&r<=y){
tree[id]+=k;
}
else{
if(l>y||r<x){
return;
}
if(l>=x&&r<=y){
tree[id]+=k*(r-l+1);
if(l!=r) lzy_tag[id]+=k;//这里
}
else{
int mid=(l+r)>>1;
push_lzyt(id,r-l+1);
upd(l,mid,id*2);
upd(mid+1,r,id*2+1);
tree[id]=tree[id*2]+tree[id*2+1];
}
}
}
int ask(int l,int r,int id){
if(l>y||r<x){
return 0;
}
else if(l>=x&&r<=y){
return tree[id];
}
else{
push_lzyt(id,r-l+1);
int mid=(l+r)>>1;
return ask(l,mid,id*2)+ask(mid+1,r,id*2+1);
}
}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",a+i);
}
build(1,n,1);
while(m--){
scanf("%lld%lld%lld",&flag,&x,&y);
if(flag==1){
scanf("%lld",&k);
upd(1,n,1);
}
else{
flag2=0;
printf("%lld\n",ask(1,n,1));
}
}
return 0;
}
@Guizy
by Eltaos_xingyu @ 2023-10-03 19:39:16
考虑这个是加法,不能直接赋值啊
by Guizy @ 2023-10-03 19:44:12
好的,谢谢!