A Simple Problem with Integers

Time Limit: 5000MS Memory Limit: 131072K
Case Time Limit: 2000MS

Description

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.

Input

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.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

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 4

Sample Output

4
55
9
15


n个数,q个操作
Q a b 查询a-b的和
C a b c a-b都加c


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
ll num;
ll flag;
}tre[5000000 + 5];
void build(int t, int l, int r)
{
tre[t].flag = 0;
if (l == r)
{
scanf("%lld", &tre[t].num);
return;
}
int mid = l + r >> 1;
build(t << 1, l, mid);
build(t << 1 | 1, mid + 1, r);
tre[t].num = tre[t << 1].num + tre[t << 1 | 1].num;
}
ll ans;
int getlen(int l, int x, int r, int y)
{
return min(r, y) - max(l, x) + 1;
}
void update(int t, int l, int r, int x, int y, int k)
{
if (l >= x&&r <= y)
{
tre[t].flag += k;
return;
}
tre[t].num += (getlen(l, x, r, y)*k);
int mid = l + r >> 1;
if (y <= mid)
update(t << 1, l, mid, x, y, k);
else if (x > mid)
update(t << 1 | 1, mid + 1, r, x, y, k);
else
{
update(t << 1, l, mid, x, y, k);
update(t << 1 | 1, mid + 1, r, x, y, k);
}
}
void query(int t, int l, int r, int x, int y, ll f)
{
if (l >= x&&r <= y)
{
ans += tre[t].num;
ans += (f+tre[t].flag)*getlen(l,x,r,y);
return;
}
int mid = l + r >> 1;
if (y <= mid)
query(t << 1, l, mid, x, y, f + tre[t].flag);
else if (x > mid)
query(t << 1 | 1, mid + 1, r, x, y, f + tre[t].flag);
else
{
query(t << 1, l, mid, x, y, f + tre[t].flag);
query(t << 1 | 1, mid + 1, r, x, y, f + tre[t].flag);
}
}
int main()
{
int n, q;
scanf("%d%d", &n, &q);
build(1, 1, n);
while (q--)
{
char s[5];
int a, b, c;
scanf("%s%d%d", s, &a, &b);
if (s[0] == 'Q')
{
ans = 0;
query(1, 1, n, a, b, 0);
printf("%lld\n", ans);
}
else
{
scanf("%d", &c);
update(1, 1, n, a, b, c);
}
}
}