Laugh_at_the_sky @ 2024-08-16 14:05:30
#include <map>
#include <set>
#include <list>
#include <regex>
#include <queue>
#include <limits>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int n, m;
int a[N];
struct Node {
int l, r;
int sum;
int tag;
};
Node operator + (const Node& a, const Node& b) {
Node x;
x.l = a.l;
x.r = b.r;
x.sum = a.sum + b.sum;
return x;
}
Node t[N << 1 << 1];
void build(int l, int r, int rt) {
if (l == r) {
t[rt].l = t[rt].r = l;
t[rt].sum = a[l];
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
t[rt] = t[rt << 1] + t[rt << 1 | 1];
}
void color(int rt, int x) {
t[rt].sum += (t[rt].r - t[rt].l + 1) * x;
t[rt].tag += x;
}
void color_son(int rt) {
if (t[rt].tag) {
color(rt << 1, t[rt].tag);
color(rt << 1 | 1, t[rt].tag);
t[rt].tag = 0;
}
}
Node query(int l, int r, int rt, int nl, int nr) {
if (l >= nl && r <= nr)
return t[rt];
color_son(rt);
int m = (l + r) >> 1;
if (nl <= m) {
if (m < nr)
return query(l, m, rt << 1, nl, nr) + query(m + 1, r, rt << 1 | 1, nl, nr);
else
return query(l, m, rt << 1, nl, nr);
}
else
return query(m + 1, r, rt << 1 | 1, nl, nr);
}
void update(int l, int r, int rt, int nl, int nr, int x) {
if (l >= nl && r <= nr) {
color(rt, x);
return;
}
color_son(rt);
int m = (l + r) >> 1;
if (nl <= m) update(l, m, rt << 1, nl, nr, x);
if (nr > m) update(m + 1, r, rt << 1 | 1, nl, nr, x);
t[rt] = t[rt << 1] + t[rt << 1 | 1];
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, n, 1);
while (m--) {
int op;
cin >> op;
int x, y;
cin >> x >> y;
if (op == 1) {
int k;
cin >> k;
update(1, n, 1, x, y, k);
}
if (op == 2)
cout << query(1, n, 1, x, y).sum << "\n";
}
return 0;
}
by Laugh_at_the_sky @ 2024-08-16 15:11:29
已 AC,本帖结。