Retime @ 2023-10-10 17:32:37
#include<bits/stdc++.h>
#define N 9999999
using namespace std;
long long f[N],sum[N];
/*快读*/inline void read(long long &a)
{
int s=0,w=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';ch=getchar();
}
a=s*w;
}
/*单点修改*/void change(int l,int r,int k)
{
if(l==r)
{
read(f[k]);
return ;
}
int mid=(l+r)>>1;
change(l,mid,k*2);
change(mid+1,r,k*2+1);
f[k]=f[k*2]+f[k*2+1];
}
/*打标记*/void add(int l,int r,int v,int k)
{
f[k]=v;
sum[k]+=(r-l+1)*v;
return ;
}
/*标记下传*/void down(int l,int r,int k,int mid,int v)
{
if(f[k]==0)return ;
add(l,mid,v,k*2);
add(mid+1,r,v,k*2+1);
f[k]=0;
}
/*区间修改*/void xg(int x,int y,int l,int r,int k,int v)
{
if(x>=l&&r>=y)return add(l,r,v,k);
int mid=(l+r)>>1;
down(l,r,k,mid,v);
if(x<=mid)xg(x,y,l,mid,k*2,v);
if(mid<y)xg(x,y,mid+1,r,k*2+1,v);
f[k]=f[k*2]+f[k*2+1];
}
/*区间和*/int rr(int x,int y,int l,int r,int k)
{
if(x>=l&&r>=y)return sum[k];
int mid=(l+r)>>1,res=0;
if(x<=mid)res+=rr(x,y,l,mid,k*2);
else res+=rr(x,y,mid+1,r,k*2+1);
return res;
}
int main()
{
long long n,m;
read(n);read(m);
change(1,n,1);
for(int i=1;i<=m;i++)
{
long long a,b,c,d;
read(a);
if(a==1)
{
read(a);read(b);read(c);
xg(b,c,1,n,1,a);
}
else
{
read(a);read(b);
cout<<rr(a,b,1,n,1)<<endl;
}
}
}
Thanks♪(・ω・)ノ
by wl19lookdsb @ 2023-10-10 18:25:53
@aAsdfghjkl 这边建议重构,错的实在是有点多,找个题解对一下吧。
by wl19lookdsb @ 2023-10-10 18:27:55
#include<bits/stdc++.h>
#define N 400005
#define int long long
using namespace std;
long long f[N],sum[N],a1[N];
/*快读*/inline void read(long long &a) {
int s=0,w=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if(ch=='-')w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
s=s*10+ch-'0';
ch=getchar();
}
a=s*w;
}
/*单点修改*/void change(int l,int r,int k) {
if(l==r) {
sum[k]=a1[l];
return ;
}
int mid=(l+r)>>1;
change(l,mid,k*2);
change(mid+1,r,k*2+1);
sum[k]=sum[k*2]+sum[k*2+1];
}
/*打标记*/void add(int l,int r,long long v,int k) {
f[k]+=v;
sum[k]+=(r-l+1)*v;
//return ;
}
/*标记下传*/void down(int l,int r,int k,int mid,int v) {
if(f[k]==0)return ;
add(l,mid,f[k],k*2);
add(mid+1,r,f[k],k*2+1);
f[k]=0;
}
/*区间修改*/void xg(int x,int y,int l,int r,int k,int v) {
if(x<=l&&r<=y) {
add(l,r,v,k);
return;
}
int mid=(l+r)>>1;
down(l,r,k,mid,v);
if(x<=mid)xg(x,y,l,mid,k*2,v);
if(mid<y)xg(x,y,mid+1,r,k*2+1,v);
sum[k]=sum[k*2]+sum[k*2+1];
}
/*区间和*/int rr(int x,int y,int l,int r,int k) {
if(x<=l&&r<=y)return sum[k];
int mid=(l+r)>>1,res=0;
down(l,r,k,mid,f[k]);
if(x<=mid)res+=rr(x,y,l,mid,k*2);
if(mid<y)res+=rr(x,y,mid+1,r,k*2+1);
return res;
}
signed main() {
long long n,m;
read(n);
read(m);
for(int i=1; i<=n; i++)scanf("%lld",&a1[i]);
change(1,n,1);
for(int i=1; i<=m; i++) {
long long a,b,c,d;
read(a);
if(a==1) {
read(d);
read(b);
read(c);
xg(d,b,1,n,1,c);
} else {
read(c);
read(b);
cout<<rr(c,b,1,n,1)<<endl;
}
}
return 0;
}
by Retime @ 2023-10-11 16:40:56
@wl19lookdsb 谢谢了