RTX7070Ti @ 2024-07-29 09:15:49
#include <iostream>
#define int long long
using namespace std;
const int N = (int)2e5 + 10;
int n, m, a[N];
struct segtree{
int l, r, num, lazy;
}tr[4*N];
void pushup(int i){
tr[i].num = tr[i << 1].num + tr[i << 1 | 1].num;
}
void build(int i, int l, int r){
tr[i].l = l;
tr[i].r = r;
tr[i].lazy = 0;
if (l == r){
tr[i].num = a[i];
return;
}
int mid = (l + r) >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
pushup(i);
}
void f(int i, int l, int r, int k){
tr[i].lazy += k;
tr[i].num += k * (r - l + 1);
}
void pushdown(int i, int l, int r){
if (tr[i].lazy){
int mid = (l + r) >> 1;
f(i << 1, l, mid, tr[i].lazy);
f(i << 1 | 1, mid + 1, r, tr[i].lazy);
tr[i].lazy = 0;
}
}
void modify(int i, int l, int r, int ql, int qr, int k){
if (ql <= l && qr >= r){
f(i, l, r, k);
return;
}
pushdown(i, l, r);
int mid = (l + r) >> 1;
if (ql <= mid) modify(i << 1, l, mid, ql, qr, k);
if (qr > mid) modify(i << 1 | 1, mid + 1, r, ql, qr, k);
pushup(i);
}
int query(int i, int l, int r, int ql, int qr){
int ret = 0;
if (ql <= l && qr >= r){
return tr[i].num;
}
int mid = (l + r) >> 1;
pushdown(i, l, r);
if (ql <= mid) ret += query(i << 1, l, mid, ql, qr);
if (qr > mid) ret += query(i << 1 | 1, mid + 1, r, ql, qr);
return ret;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int op, x, y, k;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
build (1, 1, n);
for (int i = 1; i <= m; i++){
cin >> op;
if (op == 1){
cin >> x >> y >> k;
modify(1, 1, n, x, y, k);
}else{
cin >> x >> y;
cout << query(1, 1, n, x, y) << endl;
}
}
return 0;
}
我的输出:
3
5
11
样例输出:
11
8
20
调了好久都没发现问题在哪……
by 水星湖 @ 2024-07-29 09:30:40
@RTX7070Ti tr[i].num = a[l];
by RTX7070Ti @ 2024-07-29 10:15:19
@水星湖 AC了,感谢