#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<math.h> #include<vector> #include<stack> #include<string> #include<stdio.h> using namespace std; typedef long long LL; const int MAXN = 1e5 + 10; LL a[MAXN],st[MAXN << 2]; LL add[MAXN << 2]; void pushup(int o) { st[o] = st[o << 1] + st[o << 1 | 1]; } void build(int o,int l,int r) { if (l == r) st[o] = a[l]; else { int m = (l + r) >> 1; build(o << 1, l, m); build(o << 1 | 1, m + 1, r); pushup(o); } } void pushdown(int o,int l,int r) { if(add[o]) { add[o << 1] += add[o]; add[o << 1 | 1] += add[o]; int m = (l + r) >> 1; st[o << 1] += add[o] * (m - l + 1); st[o << 1 | 1] += add[o] * (r - m); add[o] = 0; } } LL query(int o,int l,int r,int ql,int qr) { if(ql <= l && r <= qr) return st[o]; pushdown(o,l,r); int m = (l + r) >> 1; LL ans = 0; if(ql <= m) ans += query(o << 1, l, m, ql, qr); if(qr >= m + 1) ans += query(o << 1 | 1, m + 1, r, ql, qr); return ans; } void update(int o,int l,int r,int ql,int qr,LL addv) { if(ql <= l && r <= qr) { add[o] += addv; st[o] += addv * (r - l + 1); return; } pushdown(o,l,r); int m = (l + r) >> 1; if(ql <= m) update(o << 1,l,m,ql,qr,addv); if(qr >= m + 1) update(o << 1 | 1,m + 1,r,ql,qr,addv); pushup(o); } int main() { int n,q; scanf("%d %d",&n,&q); for (int i = 1; i <= n ; i++) scanf("%lld",&a[i]); build(1,1,n); char s[10]; int x,y; LL add; while(q--) { scanf("%s",s); if(s[0] == ‘Q‘) { scanf("%d %d",&x,&y); LL ans = query(1,1,n,x,y); printf("%lld\n",ans); } else { scanf("%d %d %lld",&x,&y,&add); update(1,1,n,x,y,add); } } }
原文地址:https://www.cnblogs.com/smallhester/p/11267225.html
时间: 2024-10-07 08:20:55