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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include<iostream> #include<cstdio> # define ll long long int # define maxn (ll)(1e6+5) # define lc (x << 1) # define rc (x << 1 | 1) using namespace std; ll a[maxn];
struct Segment_Tree{ long long sum; long long lazy; }; Segment_Tree tree[maxn<<2];
inline void push_up(ll x){ tree[x].sum = tree[lc].sum + tree[rc].sum; return; }
void build(ll x, ll l, ll r){ if(l == r){ tree[x].sum = a[l]; return; } int mid = (l + r) >> 1; build(lc, l, mid); build(rc, mid + 1, r); push_up(x); return; }
inline void update_point(ll x, ll l, ll r, ll i, ll k){ if(l == r){ tree[x].sum += k; return; } ll mid = (l + r) >> 1; if(i <= mid){ update_point(lc, l, mid, i, k); }else{ update_point(rc, mid + 1, r, i, k); } push_up(x); }
inline void free(ll x, ll l, ll r, ll k){ tree[x].lazy += k; tree[x].sum += k * (r - l + 1); return; }
inline void push_down(ll x, ll l, ll r){ ll mid = (l + r) >> 1; free(lc, l, mid, tree[x].lazy); free(rc, mid + 1, r, tree[x].lazy); tree[x].lazy = 0; return; }
inline void update(ll x, ll l, ll r, ll q_l, ll q_r, ll k){ if(q_l <= l && q_r >= r){ free(x, l, r, k); return; } push_down(x, l, r); ll mid = (l + r) >> 1; if(q_l <= mid){ update(lc, l, mid, q_l, q_r, k); } if(q_r > mid){ update(rc, mid + 1, r, q_l, q_r, k); } push_up(x); return; }
inline ll query(ll x, ll l, ll r, ll q_l, ll q_r){ ll res = 0; if(q_l <= l && q_r >= r){ return tree[x].sum; } ll mid = (l + r) >> 1; push_down(x, l, r); if(q_l <= mid){ res += query(lc, l, mid, q_l, q_r); } if(q_r > mid){ res += query(rc, mid + 1, r, q_l, q_r); } return res; }
int main() { int n, q, i, cmd, ql, qr, k; scanf("%d%d", &n, &q); for(i = 1 ; i <= n ; i++) scanf("%lld", &a[i]); build(1, 1, n); while(q--){ scanf("%d", &cmd); if(cmd == 1){ scanf("%d%d%d", &ql, &qr, &k); update(1, 1, n, ql, qr, k); }else{ scanf("%d%d", &ql, &qr); printf("%lld\n", query(1, 1, n, ql, qr)); } } return 0; }
|