You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
You need to answer all Q commands in order. One answer in a line.
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4Sample Output
4 55 9 15Hint
using namespace std;
const int eps = 1e6+5;
const int maxn = 0x3f3f3f3f;
#define ll long long
#define lson k<<1
#define rson k<<1|1
int n, m;
struct node
{
int l, r;
ll lazy, sum;
}t[eps<<2];
void pushup(int k){
t[k].sum = t[lson].sum + t[rson].sum;
}
void pushdown(int k){
// 本题就是 坑在了这里 !!!!
t[lson].sum += 1ll*(t[lson].r - t[lson].l + 1)*t[k].lazy;
t[rson].sum += 1ll*(t[rson].r - t[rson].l + 1)*t[k].lazy;
t[lson].lazy += t[k].lazy;
t[rson].lazy += t[k].lazy;
t[k].lazy = 0;
}
void build(int l, int r, int k){
t[k].l = l;
t[k].r = r;
t[k].lazy = 0;
if (l == r){
scanf("%lld", &t[k].sum);
return;
}
int m = (t[k].l + t[k].r) >> 1;
build(l, m, lson);
build(m+1, r, rson);
pushup(k);
}
ll query(ll l, ll r, int k){
if (l <= t[k].l && t[k].r <= r){
return t[k].sum;
}
if (t[k].lazy != 0) pushdown(k);
int m = (t[k].l + t[k].r) >> 1;
ll ans = 0;
if (l <= m) ans += query(l, r, lson);
if (r > m) ans += query(l, r, rson);
pushup(k);
return ans;
}
void update(ll l, ll r, ll c, int k){
if (l <= t[k].l && t[k].r <= r){
t[k].sum += 1ll*(t[k].r - t[k].l + 1)*c;
t[k].lazy += 1ll*c;
return;
}
if (t[k].lazy != 0) pushdown(k);
int m = (t[k].l + t[k].r) >> 1;
if (l <= m) update(l, r, c, lson);
if (r > m) update(l, r, c, rson);
pushup(k);
}
int main() {
cin >> n >> m;
char s[5];
ll a, b, c;
build(1, n, 1);
for(int i = 1; i <= m; i++){
scanf("%s", s);
if (s[0] == 'Q'){
scanf("%lld%lld", &a, &b);
printf("%lld\n", query(a, b, 1));
}
else {
scanf("%lld%lld%lld", &a, &b, &c);
update(a, b, c, 1);
}
}
return 0;
}