区间更新模板:
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static int n, q;
static long [] sum;
static long [] mark;
static void pushdown(int rt, int l, int r){
int m = (l + r) >> 1;
mark[rt << 1] += mark[rt];
sum[rt << 1] += mark[rt] * (m-l+1);
mark[rt << 1 | 1] += mark[rt];
sum[rt << 1 | 1] += mark[rt] * (r-m);
mark[rt] = 0;
}
static void pushup(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
static void update(int rt, int l, int r, int i, int j, int val){
if(l >= i && r <= j){
sum[rt] += (r-l+1)*val;//记得是区间更新,所以要把下面一个区间的都加上
mark[rt] += val;
return;
}
if(mark[rt] != 0) pushdown(rt,l,r);
int m = (l+r) >> 1;
if(i <= m) update(rt<<1, l, m, i, j, val);
if(j > m) update(rt<<1|1, m+1, r, i, j, val);
pushup(rt);
}
static long query(int rt, int l, int r, int i, int j){
if(l >= i && r <= j){
return sum[rt];
}
if(mark[rt] != 0) pushdown(rt,l,r);
int m = (l + r) >> 1;
long ans = 0;
if(i <= m) ans += query(rt<<1, l, m, i, j);
if(j > m) ans += query(rt<<1|1, m+1, r, i, j);
return ans;
}
public static void main(String[] args) {
n = sc.nextInt(); q = sc.nextInt();
sum = new long[4*n+1];
mark = new long[4*n+1];
for(int i = 1; i <= n; i++){
update(1,1,n,i,i,sc.nextInt());
}
String op;
for(int i = 0; i < q; i++){
op = sc.next();
if(op.equals("Q")){
System.out.println(query(1, 1, n, sc.nextInt(), sc.nextInt()));
} else{
update(1,1,n,sc.nextInt(),sc.nextInt(),sc.nextInt());
}
}
}
}