hh__hh @ 2023-10-06 21:35:17
#include<bits/stdc++.h>
#define lc cur << 1
#define rc lc | 1
#define int long long
using namespace std;
const int N = 1e5 + 20;
int w[N];
struct edge {
int l,r,sum,add;
} a[N];
void build(int cur,int l,int r)
{
a[cur].l = l;
a[cur].r = r;
if (l == r) {
a[cur].sum = w[l];
return;
}
int mid = l + r >> 1;
build(lc,l,mid);
build(rc,mid + 1,r);
a[cur].sum = a[lc].sum + a[rc].sum;
}
void pushdown(int cur)
{
a[lc].sum += (a[lc].r - a[lc].l + 1) * a[cur].add;
a[lc].add += a[cur].add;
a[rc].sum += (a[rc].r - a[rc].l + 1) * a[cur].add;
a[rc].add += a[cur].add;
a[cur].add = 0;
}
void updata(int cur,int x,int y,int k)
{
if (a[cur].l <= x && a[cur].r <= y) {
a[cur].add += k;
a[cur].sum += (a[cur].r - a[cur].l + 1) * k;
return;
}
if (a[cur].add) pushdown(cur);
int mid = a[cur].l + a[cur].r >> 1;
if (x <= mid) updata(lc,x,y,k);
if (y > mid) updata(rc,x,y,k);
a[cur].sum = a[lc].sum + a[rc].sum;
}
int query(int cur,int x,int y)
{
if (x <= a[cur].l && a[cur].r <= y) return a[cur].sum;
if (a[cur].add) pushdown(cur);
int sum = 0;
int mid = a[cur].l + a[cur].r >> 1;
if (x <= mid) sum += query(lc,x,y);
if (y > mid) sum += query(rc,x,y);
return sum;
}
signed main()
{
int n,m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[N];
build(1,1,n);
int op,x,y,k;
for (int i = 1; i <= m; i++) {
cin >> op;
if (op == 1) {
cin >> x >> y >> k;
updata(1,x,y,k);
}
else {
cin >> x >> y;
cout << query(1,x,y) << endl;
}
}
return 0;
}
求调
(悬赏关注)
by NO_OI_NO_LIFE @ 2023-10-06 22:16:21
@hh__hh 不谢