rt,样例已过,build()
检查过没有问题。
using namespace std;
# define int long long
const int MAXN = 1e5 + 10;
int n,q,a[MAXN];
struct node{ int l,r,sum,lazy; }tree[MAXN << 2];
void _BuTr(int root,int l,int r){
tree[root].l = l,tree[root].r = r;
if (l == r){
tree[root].sum = a[l];
return;
}int mid = (l + r) >> 1;
_BuTr(root << 1,l,mid); _BuTr((root << 1) + 1,mid + 1,r);
tree[root].sum = tree[root << 1].sum + tree[(root << 1) + 1].sum;
}void _AdSe(int root,int l,int r,int k){
if (l <= tree[root].l && r >= tree[root].r){
tree[root].lazy = k,tree[root].sum += (tree[root].r - tree[root].l + 1) * k;
return;
}int mid = (tree[root].l + tree[root].r) >> 1;
if (tree[root].lazy && tree[root].l != tree[root].r){
tree[root << 1].sum += tree[root].lazy * (mid - tree[root].l + 1),tree[(root << 1) + 1].sum += tree[root].lazy * (tree[root].r - mid);
tree[root << 1].lazy += tree[root].lazy,tree[(root << 1) + 1].lazy += tree[root].lazy;
tree[root].lazy = 0;
}if (l <= mid) _AdSe(root << 1,l,r,k);
if (r > mid) _AdSe((root << 1) + 1,l,r,k);
tree[root].sum = tree[root << 1].sum + tree[(root << 1) + 1].sum;
}int _QuSe(int root,int l,int r){
if (l <= tree[root].l && r >= tree[root].r) return tree[root].sum;
int mid = (tree[root].l + tree[root].r) >> 1,ans = 0;
if (tree[root].lazy/* && tree[root].l != tree[root].r*/){
tree[root << 1].sum += tree[root].lazy * (mid - tree[root].l + 1),tree[(root << 1) + 1].sum += tree[root].lazy * (tree[root].r - mid);
tree[root << 1].lazy += tree[root].lazy,tree[(root << 1) + 1].lazy += tree[root].lazy;
tree[root].lazy = 0;
}if (l <= mid) ans += _QuSe(root << 1,l,r);
if (r > mid) ans += _QuSe((root << 1) + 1,l,r);
return ans;
}void build_tree(){
_BuTr(1,1,n);
}void add_section(int l,int r,int k){
_AdSe(1,l,r,k);
}int query_section(int l,int r){
return _QuSe(1,l,r);
}signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1;i <= n;i++) cin >> a[i];
build_tree();
while (q--){
int op,x,y; cin >> op >> x >> y;
if (op == 1){
int k; cin >> k;
add_section(x,y,k);
}else{
cout << query_section(x,y) << endl;
}
}return 0;
}