W_C_B_H @ 2024-07-25 10:53:55
rt,评测记录
代码:
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define SIZE 400005
typedef long long ll;
#define int ll
struct node
{
int l,r;
ll val,tag;
node(int al=0,int ar=0,ll av=0,ll at=0)
{
l=al,r=ar,val=av,tag=at;
}
}seg[SIZE]; // segment tree 线段树
int n,m;
ll a[MAXN];
bool son[MAXN];
ll build(int p,int x,int y)
{
if(x==y)
{
seg[p]=node(x,y,a[x],0);
son[p]=0;
//cout<<"build("<<p<<","<<x<<","<<y<<")="<<a[x]<<"\n";
return a[x];
}
int mid=x+((y-x)>>1);
ll ret = build(p*2,x,mid) + build(p*2+1,mid+1,y);
seg[p]=node(x,y,ret,0);
son[p]=1;
//cout<<"build("<<p<<","<<x<<","<<y<<")="<<ret<<"\n";
return ret;
}
ll sum(int p,int x,int y)
{
if(seg[p].tag)
{
if(son[p])
{
seg[p*2].tag+=seg[p].tag;
seg[p*2+1].tag+=seg[p].tag;
}
seg[p].val+=(seg[p].r-seg[p].l+1)*seg[p].tag;
seg[p].tag=0;
}
int sl=seg[p].l, sr=seg[p].r;
if(sl==x && sr==y)
{
//cout<<"sum("<<p<<","<<x<<","<<y<<")="<<seg[p].val<<"\n";
return seg[p].val;
}
int mid=sl+((sr-sl)>>1);
ll p1=0,p2=0;
if(x<=mid && son[p])
{
p1=sum(p*2,x,min(mid,y));
}
if(y>mid && son[p])
{
p2=sum(p*2+1,max(mid+1,x),y);
}
//cout<<"sum("<<p<<","<<x<<","<<y<<")="<<p1+p2<<"\n";
return p1+p2;
}
void add(int p,int x,int y,int d)
{
//cout<<"add("<<p<<","<<x<<","<<y<<","<<d<<")\n";
int sl=seg[p].l, sr=seg[p].r;
if(sl>=x && sr<=y)
{
seg[p].tag+=d;
return;
}
seg[p].val+=(min(y,sr)-max(x,sl)+1)*d;
int mid = (sl + sr) >> 1;
if(son[p])
{
if(mid>=x)
{
add(p*2,x,y,d);
}
if(mid<=y)
{
add(p*2+1,x,y,d);
}
}
}
signed main()
{
//cout<<"Hello world!\n";
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
while(m--)
{
char op;
int x,y;
cin>>op>>x>>y;
if(op=='1')
{
int d;
cin>>d;
add(1,x,y,d);
}
else
{
cout<<sum(1,x,y)<<"\n";
}
}
return 0;
}
by bamboo12345 @ 2024-07-25 11:07:34
@White_Cat_Black_Hat son 数组4倍
by W_C_B_H @ 2024-07-25 11:10:08
@bamboo1030 感谢