/
5954641_AC_4032MS_9316K.cc
executable file
·92 lines (84 loc) · 2.19 KB
/
5954641_AC_4032MS_9316K.cc
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
90
91
92
#include <iostream>
using namespace std;
const int MAXN = 100001;
class Node {
public:
int left, right, num;
long long add, sum;
};
Node nodes[5*MAXN];
int N, Q, integers[MAXN];
void build( int pn, int a, int b )
{
nodes[pn].left = a;
nodes[pn].right = b;
nodes[pn].num = (b-a+1);
nodes[pn].add = 0;
if ( a == b ) {
nodes[pn].sum = integers[a];
} else {
int mid = (a + b) >> 1;
int plc = pn << 1;
int prc = plc + 1;
build( plc, a, mid );
build( prc, mid+1, b );
nodes[pn].sum = nodes[plc].sum + nodes[prc].sum;
}
}
void insert( int pn, int a, int b, long long c )
{
if ( a > b ) return ;
int plc = pn << 1;
int prc = plc + 1;
if ( nodes[pn].left == a && nodes[pn].right == b ) {
nodes[pn].add += c;
return ;
}
int mid = (nodes[pn].left + nodes[pn].right) >> 1;
nodes[plc].add += nodes[pn].add;
nodes[prc].add += nodes[pn].add;
insert( plc, a, min(b, mid), c );
insert( prc, max(mid+1, a), b, c );
nodes[pn].sum = (nodes[plc].sum + nodes[plc].add * nodes[plc].num)
+ (nodes[prc].sum + nodes[prc].add * nodes[prc].num);
nodes[pn].add = 0;
}
void query( int pn, int a, int b, long long *pc )
{
if ( a > b ) return ;
if ( nodes[pn].left == a && nodes[pn].right == b ) {
*pc += (nodes[pn].sum + nodes[pn].add * nodes[pn].num);
return ;
}
int mid = (nodes[pn].left + nodes[pn].right) >> 1;
int plc = pn << 1;
int prc = plc + 1;
nodes[plc].add += nodes[pn].add;
nodes[prc].add += nodes[pn].add;
nodes[pn].add = 0;
query( plc, a, min(b, mid), pc );
query( prc, max(mid+1, a), b, pc );
nodes[pn].sum = (nodes[plc].sum + nodes[plc].add * nodes[plc].num)
+ (nodes[prc].sum + nodes[prc].add * nodes[prc].num);
}
int main()
{
cin >> N >> Q;
for ( int n = 1; n <= N; ++n ) cin >> integers[n];
char op;
int a, b, root = 1;
long long c;
build( root, 1, N );
while ( cin >> op ) {
if ( op == 'C' ) {
cin >> a >> b >> c;
insert( root, a, b, c );
} else {
cin >> a >> b;
c = 0;
query( root, a, b, &c );
cout << c << endl;
}
}
return 0;
}