struct Sgt{
int lazyTag;
int val;
}t[maxn];
void pushUp(int x, int l, int r){
t[x].val = t[x].lazyTag * (r - l + 1) + t[x * 2].val + t[x * 2 + 1].val;
}
void pushDown(int x, int l, int r){
int mid = l + r >> 1;
t[x * 2].lazyTag += t[x].lazyTag;
t[x * 2 + 1].lazyTag += t[x].lazyTag;
t[x * 2].val += t[x].lazyTag * (mid - l + 1);
t[x * 2 + 1].val += t[x].lazyTag * (r - mid);
t[x].lazyTag = 0;
}
void Build(int x, int l, int r){
if (l == r){
t[x].val = 0;
t[x].lazyTag = 0;
return ;
}
t[x].lazyTag = 0;
int mid = l + r >> 1;
Build(x * 2, l, mid);
Build(x * 2 + 1, mid + 1, r);
pushUp(x, l, r);
}
void update(int x, int l, int r, int L, int R, int k){
if (L <= l && r <= R){
t[x].lazyTag += k;
t[x].val += k * (r - l + 1);
return ;
}
int mid = l + r >> 1;
if (L <= mid) update(x * 2, l, mid, L, R, k);
if (mid < R) update(x * 2 + 1, mid + 1, r, L, R, k);
pushUp(x, l, r);
}
int query(int x, int l, int r, int L, int R){
if (L <= l && r <= R){
return t[x].val;
}
pushDown(x, l, r);
int mid = l + r >> 1;
int res = 0;
if (L <= mid) res += query(x * 2, l, mid, L, R);
if (mid < R) res += query(x * 2 + 1, mid + 1, r, L, R);
return res;
}
标签:val,int,代码,Sgt,mid,lazyTag,void,模板
From: https://www.cnblogs.com/yh2021shx/p/17469860.html