ZSB00000 @ 2024-10-02 12:20:32
#include<bits/stdc++.h>
#define up(i) sum[i]=sum[(i)<<1]+sum[(i)<<1|1];
using namespace std;
int sum[(int)4e6 + 5];
int add[(int)4e6 + 5];
int a[(int)1e6 + 5];
void lazy(int v, int n, int i) {
add[i] += n;
sum[i] += v * n;
}
void down(int i, int ln, int rn) {
if (add[i] != 0) {
lazy(add[i], ln, i << 1);
lazy(add[i], rn, i << 1 | 1);
add[i] = 0;
}
}
void add_(int jobl, int jobr, int jobv, int l, int r, int i) {
if (jobl <= l && r <= jobr) {
lazy(jobv, r - l + 1, i);
} else {
int mid = (l + r) >> 1;
down(i, mid - l + 1, r - mid);
if (jobl <= mid) {
add_(jobl, jobr, jobv, l, mid, i << 1);
}
if (jobr > mid) {
add_(jobl, jobr, jobv, mid + 1, r, i << 1 | 1);
}
up(i);
}
}
void build(int l, int r, int i) {
if (l == r) {
sum[i] = a[l];
} else {
int mid = (l + r) >> 1;
build(l, mid, i << 1), build(mid + 1, r, i << 1 | 1);
up(i);
}
add[i] = 0;
return;
}
long long query(int jobl, int jobr, int l, int r, int i) {
if (jobl <= l && jobr >= r) {
return sum[i];
}
long long ans = 0;
int mid = (l + r) >> 1;
down(i, mid - l + 1, r - mid);
if (jobl <= mid) {
ans += query(jobl, jobr, l, mid, i << 1);
}
if (jobr > mid) {
ans += query(jobl, jobr, mid + 1, r, i << 1 | 1);
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a[i] = x;
}
build(1, n, 1);
for (int i = 1; i <= m; i++) {
int c;
cin >> c;
switch (c) {
case 1:
int x, y, k;
cin >> x >> y >> k;
add_(x, y, k, 1, n, 1);
break;
case 2:
int aa, b;
cin >> aa >> b;
cout << query(aa, b, 1, n, 1) << endl;
break;
default:
break;
}
}
}
全WA,求助qwq
by eason13950875500 @ 2024-10-02 12:39:01
我会
关我告诉你