线段树区间更新,区间求和
#include <iostream>
#include <cstdio>
#define maxn 100005
#define INF 1e9
using namespace std;
typedef long long ll;
ll sum[maxn<<2], add[maxn<<2];
void Build(int n, int l, int r){
add[n] = 0;
if(l == r){
scanf("%lld ", sum+n);
return ;
}
int mid = (l + r) >> 1;
Build(n<<1, l, mid);
Build(n<<1|1, mid+1, r);
sum[n] = sum[n<<1] + sum[n<<1|1];
}
void Pushdown(int n, int m){
if(add[n]){
sum[n<<1] += (m - (m >> 1)) * add[n];
sum[n<<1|1] += (m >> 1) * add[n];
add[n<<1] += add[n];
add[n<<1|1] += add[n];
add[n] = 0;
}
}
void Update(int n, int L, int R, int l, int r, int d){
if(l == L && R == r){
add[n] += d;
sum[n] += (r - l + 1) * d;
return ;
}
Pushdown(n, R - L + 1);
int mid = (L + R) >> 1;
if(r <= mid)
Update(n<<1, L, mid, l, r, d);
else if(l > mid)
Update(n<<1|1, mid+1, R, l, r, d);
else{
Update(n<<1, L, mid, l, mid, d);
Update(n<<1|1, mid+1, R, mid+1, r, d);
}
sum[n] = sum[n<<1] + sum[n<<1|1];
}
void Query(int n, int L, int R, int l, int r, ll &s){
if(L == l && R == r){
s += sum[n];
return ;
}
Pushdown(n, R - L + 1);
int mid = (L + R) >> 1;
if(r <= mid)
Query(n<<1, L, mid, l, r, s);
else if(l > mid)
Query(n<<1|1, mid+1, R, l, r, s);
else{
Query(n<<1, L, mid, l, mid, s);
Query(n<<1|1, mid+1, R, mid+1, r, s);
}
}
int main(){
// freopen("in.txt", "r", stdin);
int n, q;
while(scanf("%d%d ", &n, &q) == 2){
char ch;
int a, b, c;
Build(1, 1, n);
for(int i = 0; i < q; i++){
scanf("%c", &ch);
if(ch == 'Q'){
ll s = 0;
scanf("%d%d ", &a, &b);
Query(1, 1, n, a, b, s);
printf("%lld\n", s);
}
else{
scanf("%d%d%d ", &a, &b, &c);
Update(1, 1, n, a, b, c);
}
}
}
return 0;
}