WILDFIRE233 @ 2023-10-09 22:50:23
求调!谢谢!
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, m;
vector<ll> arr, t, lz;
inline void create(int idx, int l, int r) {
if (l == r) {
t[idx] = arr[l];
return;
}
int m = l + r >> 1;
create(idx * 2, l, m), create(idx * 2 + 1, m + 1, r);
t[idx] = t[idx * 2] + t[idx * 2 + 1];
}
inline void pushDown(int idx, int l, int r) {
int m = l + r >> 1;
t[idx * 2] += lz[idx] * (m - l + 1), t[idx * 2 + 1] += lz[idx * 2 + 1] * (r - m);
lz[idx * 2] += lz[idx], lz[idx * 2 + 1] += lz[idx];
lz[idx] = 0;
}
inline void update(int idx, int ql, int qr, int l, int r, ll c) {
if (l >= ql && r <= qr) {
t[idx] += c * (r - l + 1);
lz[idx] += c;
return;
}
int m = l + r >> 1;
if (lz[idx] && l != r) pushDown(idx, l, r);
if (ql <= m) update(idx * 2, ql, qr, l, m, c);
if (qr > m) update(idx * 2 + 1, ql, qr, m + 1, r, c);
t[idx] = t[idx * 2] + t[idx * 2 + 1];
}
inline long long getSum(int idx, int ql, int qr, int l, int r) {
if (l >= ql && r <= qr) return t[idx];
int m = l + r >> 1;
if (lz[idx]) pushDown(idx, l, r);
ll sum = 0;
if (ql <= m) sum += getSum(idx * 2, ql, qr, l, m);
if (qr > m) sum += getSum(idx * 2 + 1, ql, qr, m + 1, r);
return sum;
}
int main() {
scanf("%d %d", &n, &m);
arr.resize(n + 1), t.resize(4 * n), lz.resize(4 * n);
for (int i = 1; i <= n; i++)
scanf("%lld", &arr[i]);
create(1, 1, n);
for (int i = 0; i < m; i++) {
int op;
scanf("%d", &op);
if (op == 1) {
int l, r;
ll c;
scanf("%d %d %lld", &l, &r, &c);
update(1, l, r, 1, n, c);
} else {
int l, r;
scanf("%d %d", &l, &r);
printf("%lld\n", getSum(1, l, r, 1, n));
}
}
return 0;
}
by TLE_AK @ 2023-10-09 23:03:57
@WILDFIRE233 t[idx * 2] += lz[idx] * (m - l + 1), t[idx * 2 + 1] += lz[idx * 2 + 1] * (r - m);
这里的+=
改为=
,因为你要用tag将原来的值覆盖
by WILDFIRE233 @ 2023-10-09 23:12:20
@TLE_AK 事实上,问题确实出在这一行,但是其实是 lz[idx]
写成了 lz[idx*2+1]
(aa太粗心力刚刚才发现(悲