线段树模板
建树 push_up
更新 如果该点lazy标记push_down 更新完push_up
查询 如果lazy标记push_down
//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define sc(a) scanf("%d",&a)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 1000000007;
const int base=131;
ll n, m, x, y, k, a[N];
ll tree[N],lazy[N];
void push_up(ll rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void push_down(ll rt,ll len)
{
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
tree[rt<<1] += (len - (len>>1)) * lazy[rt];
tree[rt<<1|1] += (len>>1) * lazy[rt];
lazy[rt] = 0;
}
void build(ll rt,ll l,ll r)
{
if(l==r)
{
//show2("l",l)
scanf("%lld",&a[l]);
//cin>>a[l];
tree[rt]=a[l];
return ;
}
ll m= l+r >> 1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
push_up(rt);
}
void update(ll L,ll R,ll x,ll rt,ll l,ll r)
{
if(L<=l&&R>=r)
{
tree[rt]+=(r-l+1)*x;
lazy[rt]+=x;
return ;
}
if(lazy[rt]) push_down(rt,r-l+1);
ll m=l+r >>1;
if(L<=m) update(L,R,x,rt<<1,l,m);
if(R>m) update(L,R,x,rt<<1|1,m+1,r);
push_up(rt);
}
ll query(ll L,ll R,ll rt,ll l,ll r)
{
if(L<=l&&r<=R) return tree[rt];
ll m=l+r >> 1;
ll ans=0;
if(lazy[rt]) push_down(rt, r-l+1);
if(L<=m) ans+=query(L,R,rt<<1,l,m);
if(R>m) ans+=query(L,R,rt<<1|1,m+1,r);
return ans;
}
int main()
{
scanf("%lld %lld",&n,&m);
build(1,1,n);
//for(int i=1;i<=n;i++) scanf("&I64d",a[i])
while(m--)
{
char ch;
cin>>ch;
if(ch=='Q')
{
ll L,R;
scanf("%lld %lld",&L,&R);
//cin>>L>>R;
printf("%lld\n",query(L,R,1,1,n));
}
else
{
//cin>>x>>y>>k;
scanf("%lld %lld %lld",&x,&y,&k);
update(x,y,k,1,1,n);
}
}
}