-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3468.cpp
123 lines (116 loc) · 2.94 KB
/
3468.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//+base
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SIZE(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
//+segtree
#define segtree_lson l,m,rt<<1
#define segtree_rson m+1,r,rt<<1|1
// int T.l, T.r; T.up(const T& a, const T& b); T.down(T& a, T& b); T.update(UT v); T.build(); T.merge(const T& t);
template<typename T, typename UT=int>
struct SEGTREE {
vector<T> tree;
SEGTREE(int N): tree(N*4) { build(1, N); }
void build(int l, int r, int rt=1){
auto& t = tree[rt];
t.l = l;
t.r = r;
if(l == r) {
t.build();
return;
}
int m = (t.l + t.r)/2;
build(segtree_lson);
build(segtree_rson);
t.up(tree[rt<<1], tree[rt<<1|1]);
}
void update(UT v, int l, int r, int rt=1){ // range[1..N]
auto& t = tree[rt];
if(t.l == l && t.r == r) { t.update(v); return; }
if(t.l == t.r)return;
t.down(tree[rt<<1], tree[rt<<1|1]);
int m = (t.l + t.r)/2;
if(r <= m) update(v, l, r, rt<<1);
else if(l > m) update(v, l, r, rt<<1|1);
else {
update(v, segtree_lson);
update(v, segtree_rson);
}
t.up(tree[rt<<1], tree[rt<<1|1]);
}
void run(T& ret, int l, int r, int rt=1){ // range[1..N]
auto& t = tree[rt];
if(l == t.l && r == t.r){
ret = t;
return;
}
t.down(tree[rt<<1], tree[rt<<1|1]);
int m = (t.l + t.r)/2;
if(r <= m) run(ret, l, r, rt<<1);
else if(l > m) run(ret, l, r, rt<<1|1);
else {
T b;
run(ret, segtree_lson);
run(b, segtree_rson);
ret.merge(b);
}
}
};
//+main
struct ST {
int l, r;
ll sum, add;
ST() : sum(0), add(0) {}
void build(){
cin >> sum;
}
void update(int v){
add += v;
sum += v * (r - l + 1);
}
void up(const ST& a, const ST& b){
sum = a.sum + b.sum;
}
void down(ST& a, ST& b){
if(!add)return;
int m = r - l + 1;
a.add += add;
b.add += add;
a.sum += add * (m - (m>>1));
b.sum += add * (m>>1);
add = 0;
}
void merge(const ST& a){
sum += a.sum;
}
};
int main(){
int N, M;
while( cin >> N >> M ) {
SEGTREE<ST> st(N);
while(M--){
char ch[2];
cin >> ch;
int a, b, c;
if(ch[0] == 'Q'){
cin >> a >> b;
ST ret;
st.run(ret, a, b);
cout << ret.sum << endl;
} else {
cin >> a >> b >> c;
st.update(c, a, b);
}
}
}
return 0;
}