Ted_LightningTechG_ @ 2024-01-08 22:14:33
#include<bits/stdc++.h>
#define ll long long
const int MAX = 100000;
int n, m;
ll a[MAX+5], z;
int opt, x, y;
struct Segment_Tree{
struct Tree{
int l, r;
ll sum, lzy_tag;
}tree[MAX << 3];
inline void push_down(int k) {
tree[k << 1].lzy_tag += tree[k].lzy_tag, tree[k << 1 | 1].lzy_tag += tree[k].lzy_tag;
tree[k << 1].sum += tree[k].lzy_tag * (tree[k << 1].r - tree[k << 1].l + 1);
tree[k << 1 | 1].sum += tree[k].lzy_tag * (tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1);
tree[k].lzy_tag = 0;
}
inline void build(int k, int l, int r) {
tree[k].l = l, tree[k].r = r;
if(l == r) {
tree[k].sum = a[l];
return ;
}
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum;
}
inline void update(int k, int l, int r, ll num) {
if(tree[k].l >= l && tree[k].r <= r) {
tree[k].sum += num * (tree[k].r - tree[k].l + 1);
tree[k].lzy_tag += num;
return;
}
push_down(k);
if(l <= tree[k << 1].r) update(k << 1, l, r, num);
if(r >= tree[k << 1 | 1].l) update(k << 1 | 1, l, r, num);
tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum;
}
inline ll query(int k, int l, int r) {
if(tree[k].l >= l && tree[k].r <= r) return tree[k].sum;
push_down(k);
int sum = 0;
if(l <= tree[k << 1].r) sum += query(k << 1, l, r);
if(r >= tree[k << 1 | 1].l) sum += query(k << 1 | 1, l, r);
return sum;
}
}S_Tree;
int main() {
std::cin >> n >> m;
for(int i = 1; i <= n; i ++) std::cin >> a[i];
S_Tree.build(1, 1, n);
while(m --> 0) {
std::cin >> opt >> x >> y;
if(opt == 1) {
std::cin >> z;
S_Tree.update(1, x, y, z);
}
else {
std::cout << S_Tree.query(1, x, y) <<'\n';
}
}
return 0;
}
好心人调下程序吧<T_T>orz
by Ted_LightningTechG_ @ 2024-01-08 22:44:08
我叉,query里边的sum longlong没开,过了