边角暴力,大块整体,好写的暴力数据结构。
区间查询,单点修改。(树状数组的活)
//Stay foolish,stay hungry,stay young,stay simple
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
#define R register
using namespace std;
const int MAXN=500005;
int sum[MAXN],r[MAXN],l[MAXN];
int a[MAXN],belong[MAXN],block;
int n,m,num;
inline int read_d(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c)) f=c=='-'?-1:1;
while(isdigit(c)){
ret*=10;ret+=c-'0';c=getchar();
}
return ret*f;
}
inline void write(int x)
{
if(x<0) x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void build(){
block=sqrt(n);
num=n/block;
if(n%block) num++;
for(R int i=1;i<=num;i++){
l[i]=(i-1)*block+1;
r[i]=i*block;
}
for(R int i=1;i<=n;i++)
belong[i]=(i-1)/block+1;
r[num]=n;
for(R int i=1;i<=num;i++)
for(int j=l[i];j<=r[i];j++)
sum[i]+=a[j];
}
inline void updata(int now,int w){
a[now]+=w;
sum[belong[now]]+=w;
}
inline int query(int x,int y){
int ret=0;
if(belong[x]==belong[y]){
for(R int i=x;i<=y;i++) ret+=a[i];
return ret;
}
for(R int i=x;i<=r[belong[x]];i++) ret+=a[i];
for(R int i=belong[x]+1;i<=belong[y]-1;i++)
ret+=sum[i];
for(R int i=l[belong[y]];i<=y;i++) ret+=a[i];
return ret;
}
int main(){
n=read_d();
m=read_d();
for(R int i=1;i<=n;i++)
a[i]=read_d();
build();
for(R int i=1;i<=m;i++){
int x,y,q;
q=read_d();
x=read_d();
y=read_d();
if(q==1) updata(x,y);
else write(query(x,y)),putchar('\n');
}
return 0;
}
区间修改,区间查询。线段树的活,跑得挺快是真的。
//Stay foolish,stay hungry,stay young,stay simple
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAXN=500005;
ll sum[MAXN],r[MAXN],l[MAXN],inc[MAXN];
ll a[MAXN],belong[MAXN],block;
ll n,m,num;
inline ll read_d(){
ll ret=0,f=1;char c;
while(c=getchar(),!isdigit(c)) f=c=='-'?-1:1;
while(isdigit(c)){
ret*=10;ret+=c-'0';c=getchar();
}
return ret*f;
}
inline void write(ll x)
{
if(x<0) x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void build(){
block=sqrt(n);
num=n/block;
if(n%block) num++;
for(int i=1;i<=num;i++){
l[i]=(i-1)*block+1;
r[i]=i*block;
}
for(int i=1;i<=n;i++)
belong[i]=(i-1)/block+1;
r[num]=n;
for(int i=1;i<=num;i++)
for(int j=l[i];j<=r[i];j++)
sum[i]+=a[j];
}
//inline void updata(int now,int w){
// a[now]+=w;
// sum[belong[now]]+=w;
//}
inline void updata(ll x,ll y,ll w){
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++) a[i]+=w;
sum[belong[x]]+=w*(y-x+1);
return ;
}
for(int i=x;i<=r[belong[x]];i++)
a[i]+=w,sum[belong[i]]+=w;
for(int i=belong[x]+1;i<=belong[y]-1;i++)
inc[i]+=w;
for(int i=l[belong[y]];i<=y;i++)
a[i]+=w,sum[belong[i]]+=w;
// sum[belong[y]]+=w*(y-l[belong[y]]+1);
}
inline ll query(int x,int y){
ll ret=0;
if(belong[x]==belong[y]){
for(int i=x;i<=y;i++) ret+=a[i]+inc[belong[i]];
return ret;
}
for(int i=x;i<=r[belong[x]];i++) ret+=a[i]+inc[belong[i]];
for(int i=belong[x]+1;i<=belong[y]-1;i++)
ret+=sum[i]+inc[i]*(r[i]-l[i]+1);
for(int i=l[belong[y]];i<=y;i++) ret+=a[i]+inc[belong[i]];
// if(belong[x]==belong[y]){
// ret-=a[x]+2*inc[belong[x]]+a[y];
// }
return ret;
}
int main(){
n=read_d();
m=read_d();
for(int i=1;i<=n;i++)
a[i]=read_d();
build();
for(int i=1;i<=m;i++){
ll q,x,y,z;
q=read_d();
x=read_d();
y=read_d();
if(q==1) z=read_d(),updata(x,y,z);
else write(query(x,y)),putchar('\n');
}
return 0;
}