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
| #include <iostream> #define maxn 100010 #define ll long long using namespace std;
int n, m, a[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { ll v, tv; ll add, tadd, t; } T[maxn * 4]; inline void maintain(int i) { T[i].v = T[lc].v + T[rc].v; T[i].tv = T[lc].tv + T[rc].tv; }
void build(int i, int l, int r) { if (l == r) return T[i] = { a[l], a[l], 0, 0, 0 }, void(); int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); maintain(i); }
inline void Update(int i, int l, int r, ll add, ll tadd, ll t) { T[i].tv += t * T[i].v + tadd * (r - l + 1); T[i].v += (r - l + 1) * add; T[i].tadd += t * T[i].add + tadd; T[i].t += t; T[i].add += add; }
inline void pushdown(int i, int l, int r) { ll &add = T[i].add, &tadd = T[i].tadd, &t = T[i].t, m = l + r >> 1; Update(lc, l, m, add, tadd, t); Update(rc, m + 1, r, add, tadd, t); add = tadd = t = 0; }
void update(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; if (L <= l && r <= R) return Update(i, l, r, v, 0, 0); int m = l + r >> 1; pushdown(i, l, r); update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); maintain(i); }
ll query(int i, int l, int r, int L, int R) { if (l > R || r < L) return 0; if (L <= l && r <= R) return T[i].tv; int m = l + r >> 1; pushdown(i, l, r); return query(lc, l, m, L, R) + query(rc, m + 1, r, L, R); }
inline void solve_1() { int x, y, z; cin >> x >> y >> z; update(1, 1, n, x, y, z); }
inline void solve_2() { int x, y; cin >> x >> y; cout << query(1, 1, n, x, y) << "\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; build(1, 1, n); for (int i = 1; i <= m; ++i) { int opt; cin >> opt; if (opt == 1) solve_1(); else solve_2(); Update(1, 1, n, 0, 0, 1); } return 0; }
|