做这种题目呢,我们只需要分清楚每种操作的优先级就行了。在这里,我们规定优先级:区间赋值>区间乘>区间加。
也没啥好说而呀,我要咋办= =。
记得 query 和 update 时要 标记下饭(放就行了。
放代码吧。
代码如下
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define N 100005
using namespace std;
int sum[N * 4], add[N * 4], col[N * 4], mul[N * 4];
void pushup(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt, int x){
if(col[rt] != -1){
add[rt<<1] = add[rt<<1|1] = 0;
mul[rt<<1] = mul[rt<<1|1] = 1;
sum[rt<<1] = col[rt] * (x - x / 2);
sum[rt<<1|1] = col[rt] * (x / 2);
col[rt<<1] = col[rt<<1|1] = col[rt];
col[rt] = -1;
}
if(mul[rt] != 1){
int c = mul[rt];
mul[rt<<1] *= c;
mul[rt<<1|1] *= c;
sum[rt<<1] *= c;
sum[rt<<1|1] *= c;
add[rt<<1] *= c;
add[rt<<1|1] *= c;
mul[rt] = 1;
}
if(add[rt]){
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += add[rt] * (x - x / 2);
sum[rt<<1|1] += add[rt] * (x / 2);
add[rt] = 0;
}
}
void build(int l, int r, int rt){
if(l == r){
scanf("%d", &sum[rt]);
return;
}
int m = l + r >> 1;
build(lson);
build(rson);
pushup(rt);
}
void update(int l, int r, int rt, int a, int b, int c, int o){
if(l >= a && r <= b){
if(o == 1){//set
add[rt] = 0;
mul[rt] = 1;
sum[rt] = c * (r - l + 1);
col[rt] = c;
}
else if(o == 2){//cheng
mul[rt] *= c;
sum[rt] *= c;
add[rt] *= c;
}
else{//jia
add[rt] += c;
sum[rt] += c * (r - l + 1);
}
return;
}
pushdown(rt, r-l+1);
int m = l + r >> 1;
if(a <= m) update(lson, a, b, c, o);
if(b > m) update(rson, a, b, c, o);
pushup(rt);
}
int query(int l, int r, int rt, int a, int b){
if(l >= a && r <= b) return sum[rt];
pushdown(rt, r-l+1);
int m = l + r >> 1, ans = 0;
if(a <= m) ans += query(lson, a, b);
if(b > m) ans += query(rson, a, b);
return ans;
}
int main(){
int i, j, n, m, o, a, b, c;
scanf("%d%d", &n, &m);
for(i = 1; i <= n * 4; i++){
mul[i] = 1;
col[i] = -1;
}
build(1, n, 1);
while(m--){
scanf("%d%d%d", &o, &a, &b);
if(o == 4) printf("%d\n", query(1, n, 1, a, b));
else{
scanf("%d", &c);
update(1, n, 1, a, b, c, o);
}
}
return 0;
}