ln001 @ 2023-11-23 16:59:16
// Problem: P3372 【模板】线段树 1
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3372
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, INF = 0x3f3f3f3f3f3f3f3f;
#define mid(l, r) ((l + r) >> 1)
#define ls(p) (p << 1)
#define rs(p) (p << 1 | 1)
long long n, m, a[N];
struct TREE
{
long long p;
long long l, r;
long long a, t;
} t[N * 4];
void push_up(long long x)
{
t[x].a = t[ls(x)].a + t[rs(x)].a;
}
void add(long long x, long long y)
{
t[x].a += (t[x].r - t[x].l + 1) * y;
t[x].t += y;
}
void push_down(long long x)
{
if (!t[x].t)
return;
add(ls(x), t[x].t);
add(rs(x), t[x].t);
t[x].t = 0;
return;
}
void add(long long p, long long L, long long R, long long x)
{
if (t[p].l > R || t[p].r < L)
return;
if (t[p].l >= L && t[p].r <= R)
return add(p, x);
push_down(p);
add(ls(p), L, R, x);
add(rs(p), L, R, x);
push_up(p);
}
long long ask(long long p, long long L, long long R)
{
if (t[p].l > R || t[p].r < L)
return 0;
if (t[p].l >= L && t[p].r <= R)
return t[p].a;
push_down(p);
return ask(ls(p), L, R) + ask(rs(p), L, R);
}
void build(long long p, long long x, long long y)
{
if (x == y)
return t[p].a = a[x], void();
build(ls(p), x, mid(x, y));
build(rs(p), 1 + mid(x, y), y);
push_up(p);
}
signed main()
{
cin >> n >> m;
for (long long i = 1; i <= n; i++)
{
cin >> a[i];
}
build(1, 1, n);
while (m--)
{
long long op, x, y;
cin >> op;
if (op == 2)
{
cin >> x >> y;
cout << ask(1, x, y) << endl;
}
else
{
long long z;
cin >> x >> y >> z;
add(1, x, y, z);
}
}
return 0;
}
by ln001 @ 2023-11-23 17:09:37
build建错了 此贴解