|
- #include <cstdio>
- long long a[1000001],sum[4000001],tag[4000001];
- void build(long long place,long long left,long long right)
- {
- if(left==right)
- {
- sum[place]=a[left];
- return;
- }
- long long mid=(left+right)>>1;
- build(place<<1,left,mid);
- build(place<<1|1,mid+1,right);
- sum[place]=sum[place<<1]+sum[place<<1|1];
- }
- inline void find(long long left,long long right,long long place,long long x)
- {
- tag[place]+=x;
- sum[place]+=x*(right-left+1);
- }
- inline void putdown(long long left,long long right,long long place)
- {
- long long mid=(left+right)>>1;
- find(left,mid,place<<1,tag[place]);
- find(mid+1,right,place<<1|1,tag[place]);
- tag[place]=0;
- }
- inline void change(long long start,long long end,long long left,long long right,long long place,long long x)
- {
- if(start<=left && right<=end)
- {
- tag[place]+=x;
- sum[place]+=x*(right-left+1);
- return;
- }
- putdown(left,right,place);
- long long mid=(left+right)>>1;
- if(start<=mid)
- change(start,end,left,mid,place<<1,x);
- if(end>mid)
- change(start,end,mid+1,right,place<<1|1,x);
- sum[place]=sum[place<<1|1]+sum[place<<1];
- }
- long long query(long long start,long long end,long long left,long long right,long long place)
- {
- long long ans=0;
- if(start<=left && right<=end)
- return ans=sum[place];
- long long mid=(left+right)>>1;
- putdown(left,right,place);
- if(start<=mid)
- ans+=query(start,end,left,mid,place<<1);
- if(end>mid)
- ans+=query(start,end,mid+1,right,place<<1|1);
- return ans;
- }
- int main()
- {
- long long n,m,p,q,x;
- int k;
- scanf("%lld%lld",&n,&m);
- for(int i=1;i<=n;i++)
- scanf("%lld",&a[i]);
- build(1,1,n);
- for(int i=1;i<=m;i++)
- {
- scanf("%d",&k);
- if(k==1)
- {
- scanf("%lld%lld%lld",&p,&q,&x);
- change(p,q,1,n,1,x);
- }
- if(k==2)
- {
- scanf("%lld%lld",&p,&q);
- printf("%lld\n",query(p,q,1,n,1));
- }
- }
- return 0;
- }
复制代码 |
|