const int MAX_LEN = ;
int seg_tree[MAX_LEN << 2];
int Lazy[MAX_LEN << 2];
int arr[MAX_LEN];
//从下往上更新 节点
void push_up (int root) {
seg_tree[root] = seg_tree[root << 1] + seg_tree[root << 1 | 1];
}
//从上向下更新,左右孩子
void push_down (int root, int L, int R) {
if(Lazy[root]){
Lazy[root << 1] += Lazy [root];
Lazy[root << 1 | 1] += Lazy[root];
int mid = (L + R) >> 1;
seg_tree[root << 1] += Lazy[root] * (mid - L + 1);
seg_tree[root << 1 | 1] += Lazy[root] * (R - mid);
Lazy[root] = 0;
}
}
//建树
void build (int root, int L, int R) {
Lazy[root] = 0;
if(L == R) {
seg_tree[root] = arr[L];
return ;
}
int mid = (L + R) >> 1;
build(root << 1, L, mid);
build(root << 1 | 1, mid + 1, R);
push_up(root);
}
//区间查询
int query (int root, int L, int R, int LL, int RR) {
if (LL <= L && R <= RR) return seg_tree[root];
push_down(root, L, R); //每次访问都去检查Lazy 标记
int Ans = 0;
int mid = (L + R) >> 1;
if(LL <= mid) Ans += query(root << 1, L, mid, LL, RR);
if(RR > mid) Ans += query(root << 1 | 1, mid + 1, R, LL, RR);
return Ans;
}
//区间修改
void update_Interval(int root, int L, int R, int LL, int RR, int val){
if (LL <= L && R <= RR) {
Lazy[root] += val;
seg_tree[root] += val * (R - L + 1);
return ;
}
push_down(root, L, R);
int mid = (L + R) >> 1;
if (LL <= mid) update_Interval(root << 1, L, mid, LL, RR, val);
if (RR > mid) update_Interval(root << 1 | 1, mid + 1, R, LL , RR, val);
push_up(root);
}
//单点修改
void update(int root, int L, int R, int pos, int val) {
if(L == R){
seg_tree[root] += val;
return ;
}
int mid = (L + R) >> 1;
if(pos <= mid) update(root << 1, L, mid, pos, val);
else update(root << 1 | 1, mid + 1, R, pos, val);
push_up(root);
}
区间直接变为某值:
void push_down (int root, int L, int R) {
if(Lazy[root]){
Lazy[root << 1] = Lazy [root];
Lazy[root << 1 | 1] = Lazy[root];
int mid = (L + R) >> 1;
seg_tree[root << 1] = Lazy[root] * (mid - L + 1);
seg_tree[root << 1 | 1] = Lazy[root] * (R - mid);
Lazy[root] = 0;
}
}
void update_Interval(int root, int L, int R, int LL, int RR, int val){
if (LL <= L && R <= RR) {
Lazy[root] = val;
seg_tree[root] = val * (R - L + 1);
return ;
}
push_down(root, L, R);
int mid = (L + R) >> 1;
if (LL <= mid) update_Interval(root << 1, L, mid, LL, RR, val);
if (RR > mid) update_Interval(root << 1 | 1, mid + 1, R, LL , RR, val);
push_up(root);
}