#include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <vector> #include <iostream> #define lson rt<<1 #define rson rt<<1|1 using namespace std; typedef long long ll; const int maxn=2e5+10; //区间查询 区间更新线段树 ll tree[maxn<<2],lazy[maxn<<2]; void pushdown(int rt,int l,int r){ lazy[lson]=(lazy[lson]+lazy[rt]); lazy[rson]=(lazy[rson]+lazy[rt]); int mid=(l+r)/2; tree[lson]=tree[lson]+lazy[rt]*(mid-l+1); tree[rson]=tree[rson]+lazy[rt]*(r-mid); lazy[rt]=0; } void pushup(int rt) { tree[rt]=(tree[lson]+tree[rson]); } int wt[maxn]; void build(int rt,int l,int r) { if(l==r) { tree[rt]=wt[l]; return; } int mid=(l+r)/2; build(lson,l,mid); build(rson,mid+1,r); pushup(rt); } void update(int x,int L,int R,int l,int r,int rt) { pushdown(rt, l, r); if(L<=l&&r<=R) { lazy[rt]=x; tree[rt]=tree[rt]+x*(r-l+1); return; } int mid=(l+r)/2; if(mid>=L) update(x, L, R, l,mid,lson); if(mid<R) update(x, L, R, mid+1,r,rson); pushup(rt); } ll query(int L,int R,int l,int r,int rt) { pushdown(rt,l,r); if(L<=l&&r<=R) { return tree[rt]; } int mid=(r+l)/2; ll ans=0; if(mid>=L) ans=(ans+query(L, R, l,mid,lson)); if(mid<R) ans=(ans+query(L, R, mid+1,r,rson)); return ans; } int main() { int n,q; cin>>n>>q; for(int i=1;i<=n;i++) scanf("%d",&wt[i]); build(1, 1, n); for(int i=0;i<q;i++) { char c; getchar(); scanf("%c",&c); if(c=='C') { int a,b,x; scanf("%d%d%d",&a,&b,&x); update(x, a, b, 1, n, 1); } else { int a,b; scanf("%d%d",&a,&b); cout<<query(a, b, 1, n, 1)<<endl; } } }
来源:https://www.cnblogs.com/King-of-Dark/p/12272923.html