ruanwentao666 @ 2023-11-05 10:42:02
本蒟蒻样例没过:
输出为:
0
2
8
#include<bits/stdc++.h>
using namespace std;
int n, m, selcet, x, y, k, t[400005], a[100005], addv[400005], sumv[400005], _sum;
void Judge2(int o, int l, int r) {
if (r > l) {
sumv[o] = sumv[o * 2] + sumv[o * 2 + 1];
}
sumv[o] += addv[o] * (r - l + 1);
}
void build_tree(int o, int l, int r) {
if (l == r) {
t[o] = a[l];
return;
}
int mid = (l + r) / 2;
build_tree(o * 2, l, mid);
build_tree(o * 2 + 1, mid + 1, r);
sumv[o] = sumv[o * 2] + sumv[o * 2 + 1];
}
void Judge1(int o, int l, int r, int x, int y) {
if (x <= l && r <= y) {
addv[o] += k;
}
else {
int mid = (l + r) / 2;
if (x <= mid) {
Judge1(o * 2, l, mid, x, y);
}
if (y > mid) {
Judge1(o * 2 + 1, mid + 1, r, x, y);
}
}
Judge2(o, l, r);
}
void find(int o, int l, int r, int x, int y, int add) {
if (x <= l && r <= y) {
_sum += sumv[o] + add * (r - l + 1);
}
else {
int mid = (l + r) / 2;
if (x <= mid) {
find(o * 2, l, mid, x, y, add + addv[o]);
}
if (y > mid) {
find(o * 2 + 1, mid + 1, r, x, y, add + addv[o]);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1, v; i <= n; i++) {
cin >> a[i];
}
build_tree(1, 1, n);
for (int i = 1; i <= m; i++) {
cin >> selcet >> x >> y;
if (selcet == 1) {
cin >> k;
Judge1(1, 1, n, x, y);
}
else {
_sum = 0;
find(1, 1, n, x, y, 0);
cout << _sum << "\n";
}
}
return 0;
}
by 大眼仔Happy @ 2023-11-05 10:50:58
@ruanwentao666 怎么感觉有点像标记永久化,但是又有点不像。。。
by 大眼仔Happy @ 2023-11-05 10:53:11
@ruanwentao666 您的 t 数组除了建树的时候后面就没用过了。。。
by yydfj @ 2023-11-10 19:52:15
@ruanwentao666 你的懒标记去哪了?