K_J_M @ 2024-07-21 19:07:04
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
const int M = sqrt(1e5+10);
int n,m,op,x,y,k,len,num;
int a[N],belong[N];
struct node{
int l,r,sum,add;
}b[M];
void build(){
num=sqrt(n);
len=sqrt(n);
for(int i=1;i<=num;i++){
b[i].l=(i-1)*len+1,b[i].r=i*len;
}
if(len*num<n){
num++;
b[num].l=b[num-1].r+1;
b[num].r=n;
}
for(int i=1;i<=num;i++){
for(int j=b[i].l;j<=b[i].r;j++){
b[i].sum+=a[j];
belong[j]=i;
}
}
for(int i=1;i<=num;i++) b[i].add=0;
}
void update(int x,int y,int k){
int p=belong[x],q=belong[y];
if(p==q){
for(int i=x;i<=y;i++) a[i]+=k,b[p].sum+=k;
}else{
for(int i=x;i<=b[p].r;i++) a[i]+=k,b[p].sum+=k;
for(int i=p+1;i<q;i++) b[i].sum+=len*k,b[i].add+=k;
for(int i=b[q].l;i<=y;i++) a[i]+=k,b[q].sum+=k;
}
}
int getsum(int x,int y){
int p=belong[x],q=belong[y],ans=0;
if(p==q){
for(int i=x;i<=y;i++) ans+=a[i]+b[p].add;
return ans;
}
for(int i=x;i<=b[p].r;i++) ans+=a[i]+b[p].add;
for(int i=p+1;i<q;i++) ans+=b[i].sum;
for(int i=b[q].l;i<=y;i++) ans+=a[i]+b[q].add;
return ans;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
build();
for(int i=1;i<=m;i++){
cin>>op>>x>>y;
if(op==1){
cin>>k;
update(x,y,k);
}else{
cout<<getsum(x,y)<<endl;
}
}
return 0;
}
by K_J_M @ 2024-07-21 19:17:57
好吧,是氧气中毒
by K_J_M @ 2024-07-21 19:18:23
再问一句,为什么开O2会WA啊
by xuhaotian @ 2024-07-21 19:21:16
@K_J_M M开小了
by K_J_M @ 2024-07-21 19:34:22
@xuhaotian thx