|
- #include <algorithm>
- #include <iostream>
- #include <cmath>
- #include <cstring>
- #include <map>
- #include <string>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- const int N = 2e5 + 10;
- typedef long long LL;
- LL sum[N * 4]= {0}, tag[N * 4]= {0},a[N];
- void build(int l, int r, int o)
- {
- if(l == r)
- {
- sum[o] = a[l];
- tag[o] = 0;
- return ;
- }
- int mid = (l + r) >> 1;
- build(l, mid, o * 2);
- build(mid + 1, r, o << 1 | 1);
- sum[o] = sum[o << 1] + sum[o << 1 | 1];
- tag[o] = 0;
- return ;
- }
- void update(int L, int R, LL add, int l, int r, int o)
- {
- if(L <= l && r <= R)
- {
- sum[o] += (r - l + 1) * add;
- tag[o] += add;
- return ;
- }
- int mid = (l + r) >> 1;
- if(tag[o])
- {
- sum[o << 1] += tag[o] * (mid - l + 1);
- tag[o << 1] += tag[o];
- sum[o << 1|1] += tag[o] * (r - mid);
- tag[o << 1|1] += tag[o];
- tag[o] = 0;
- }
- if(L <= mid) update(L, R, add, l, mid, o << 1);
- if(R > mid) update(L, R, add, mid + 1, r, o << 1 | 1);
- sum[o] = sum[o << 1] + sum[o << 1 | 1];
- }
- LL query(int L, int R, int l, int r, int o)
- {
- if(L <= l && r <= R)
- {
- return sum[o];
- }
- int mid = (l + r) >> 1;
- if(tag[o])
- {
- sum[o << 1] += tag[o] * (mid - l + 1);
- tag[o << 1] += tag[o];
- sum[o << 1|1] += tag[o] * (r - mid);
- tag[o << 1|1] += tag[o];
- tag[o] = 0;
- }
- LL ans = 0;
- if(L <= mid) ans += query(L, R, l, mid, o << 1);
- if(R > mid) ans += query(L, R, mid + 1, r, o << 1 | 1);
- return ans;
- }
- int main()
- {
- int n, m;
- scanf("%d %d", &n, &m);
- for(int i=1; i<=n; i++)
- scanf("%lld", &a[i]);
- build(1, n, 1);
- while(m --)
- {
- int op;
- scanf("%d", &op);
- if(op == 1)
- {
- int l, r;
- LL x;
- scanf("%d %d %lld", &l, &r, &x);
- update(l, r, x, 1, n, 1);
- }
- else
- {
- int l, r;
- scanf("%d %d", &l, &r);
- printf("%lld\n", query(l, r, 1, n, 1));
- }
- }
- }
复制代码 |
|