AssiduousCoding @ 2024-05-27 21:19:31
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, m;
int w[N];
struct Node
{
int l, r;
LL sum, add;
} tr[N << 2];
void pushup(int u)
{
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void pushdown(int u)
{
Node &t = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if (t.add)
{
left.add += t.add; left.sum += (LL)(left.r - left.l + 1) * t.add;
right.add += t.add; right.sum += (LL)(right.r - right.l + 1) * t.add;
t.add = 0;
}
}
void build(int u, int l, int r)
{
if (l == r)
{
tr[u] = {l, r, w[r], 0};
return ;
}
tr[u] = {l, r};
int mid = (l + r) >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void modify(int u, int l, int r, LL v)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].sum += (LL)(tr[u].r - tr[u].r + 1) * v;
tr[u].add += v;
return ;
}
int mid = (tr[u].l + tr[u].r) >> 1;
pushdown(u);
if (l <= mid) modify(u << 1, l, r, v);
if (r > mid) modify(u << 1 | 1, l, r, v);
pushup(u);
}
LL query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
int mid = (tr[u].l + tr[u].r) >> 1;
LL res = 0LL;
pushdown(u);
if (l <= mid) res += query(u << 1, l, r);
if (r > mid) res += query(u << 1 | 1, l, r);
pushup(u);
return res;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
build(1, 1, n);
int type, l, r, k;
while (m -- )
{
scanf("%d%d%d", &type, &l, &r);
if (type == 1)
{
scanf("%d", &k);
modify(1, l, r, k);
}
else printf("%lld\n", query(1, l, r));
}
return 0;
}
by AssiduousCoding @ 2024-05-27 21:21:15
只得了10pts,跪求路过大佬指出错误!!!
by lc666616 @ 2024-05-29 12:05:36
tr[u].sum += (LL)(tr[u].r - tr[u].r + 1) * v;
这里应该是r-l+1吧
by AssiduousCoding @ 2024-07-08 13:57:39
@lc666616