-
Notifications
You must be signed in to change notification settings - Fork 0
/
3468.cpp
103 lines (86 loc) · 2 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
#include<cstdio>
#include<cstring>
#define maxn 100005
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
long long sum[maxn << 2], val[maxn];
int mark[maxn << 2];
void pushup(int rt){
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void pushdown(int rt, int c){
if(mark[rt]){
mark[rt << 1] += mark[rt];
mark[rt << 1 | 1] += mark[rt];
sum[rt << 1] += mark[rt] * (c - (c >> 1));
sum[rt << 1 | 1] += mark[rt] * (c >> 1);
mark[rt] = 0;
}
}
void build(int l, int r, int rt){
int m;
if(l == r){
sum[rt] = val[l];
return;
}
m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L, int R, int l, int r, int rt, int c){
int m;
if(l >= L && r <= R){
mark[rt] = c;
sum[rt] += (r - l + 1) * c;
return;
}
pushdown(rt, r - l + 1);
m = (l + r) >> 1;
if(m >= L)
update(L, R, lson, c);
if(m < R)
update(L, R, rson, c);
//修改操作要对祖先节点也做相应的修改
pushup(rt);
}
long long query(int L, int R, int l, int r, int rt){
int m;
long long ret;
if(l >= L && r <= R){
return sum[rt];
}
//在需要查询孩子的时候才执行延迟标识操作
pushdown(rt, r - l + 1);
m = (l + r) >> 1;
ret = 0;
if(m >= L)
ret += query(L, R, lson);
if(m < R)
ret += query(L, R, rson);
return ret;
}
int main(){
int N, Q;
int i;
char op[1];
int from, to, c;
freopen("input.txt", "r", stdin);
memset(mark, 0, sizeof(mark));
scanf("%d%d", &N, &Q);
for(i = 1; i <= N; i++){
scanf("%lld", &val[i]);
}
build(1, N, 1);
for(i = 0; i < Q; i++){
scanf("%s", op);
if(op[0] == 'C'){
scanf("%d%d%d", &from, &to, &c);
update(from, to, 1, N, 1, c);
}else{
scanf("%d%d", &from, &to);
printf("%lld\n", query(from, to, 1, N, 1));
}
}
return 0;
}