线段树维护区间Sum Max Min

#include <bits/stdc++.h>		//线段树维护区间和
#define ll long long
#define inf 0x3f3f3f3f 
using namespace std;
const int maxn = 1e5+7;
int a[10000];
struct node{
	int l,r;
	ll add;
	ll sum;
	ll maxx;
	ll minn;
}tree[maxn<<2];
void pushup(int index){		//更新值
	tree[index].sum = tree[index<<1].sum+tree[index<<1|1].sum;  
    tree[index].maxx = max(tree[index<<1].maxx,tree[index<<1|1].maxx);  
    tree[index].minn = min(tree[index<<1].minn,tree[index<<1|1].minn);  
}
void pushdown(int index){	//下推标记
	if(tree[index].add){
 		tree[index<<1].sum += (tree[index<<1].r-tree[index<<1].l+1)*tree[index].add;  
        tree[index<<1|1].sum +=(tree[index<<1|1].r-tree[index<<1|1].l+1)*tree[index].add;  
        
        tree[index<<1].maxx += tree[index].add;  
        tree[index<<1|1].maxx += tree[index].add;  
        
        tree[index<<1].minn += tree[index].add;  
        tree[index<<1|1].minn += tree[index].add;  
        
        tree[index<<1].add += tree[index].add;  
        tree[index<<1|1].add += tree[index].add;  
        
        tree[index].add = 0; 
	}
}
void build(int l, int r, int index){		//建树	数组a的l,r区间建树
	tree[index].l = l;
	tree[index].r = r;
	tree[index].add = 0;
	if(l == r){
		scanf("%lld",&tree[index].sum);
		tree[index].maxx = tree[index].minn = tree[index].sum;
		// tree[index].sum = tree[index].maxx = tree[index].minn = a[l];
		return;
	}
	int mid = (l+r)>>1;
	build(l,mid,index<<1);
	build(mid+1,r,index<<1|1);
	pushup(index);
}
void update(int l,int r,int index, ll w){	//修改a[L]的值 更新sum
	if(l <= tree[index].l && r >= tree[index].r){	
        tree[index].sum += (tree[index].r-tree[index].l+1)*w;  
        tree[index].maxx += w;  
        tree[index].minn += w;  
        tree[index].add += w;
		return;
	}
	pushdown(index);
	int mid = (tree[index].l + tree[index].r)>>1;
	if(l <= mid) update(l,r,index<<1,w);
	if(r > mid) update(l,r,index<<1|1,w);
	pushup(index);
}
ll query(int l,int r,int index){	//查询
	if(l <= tree[index].l && r >= tree[index].r){
		return tree[index].sum;
		// return tree[index].maxx;
		// return tree[index].minn;
	}
	pushdown(index);
	int mid = (tree[index].l + tree[index].r)>>1;
	ll sum = 0;
	ll maxx = 0;
	ll minn = inf;
 	if(l <= mid){
 		sum += query(l,r,index<<1);
 		maxx = max(maxx,query(l,r,index<<1));
 		minn = min(minn,query(l,r,index<<1));
 	}
	if(r > mid){
		sum += query(l,r,index<<1|1);
 		maxx = max(maxx,query(l,r,index<<1|1));
 		minn = min(minn,query(l,r,index<<1|1));
	}
	return sum;
	// return maxx;
	// return minn;
}
int main(){
	int n,m,q,x,y;
	ll z; 
    while(~scanf("%d %d", &n, &m)){  
        build(1,n,1);  
        while(m--){  
            scanf("%d",&q);  
            if(q == 1){  
                cout<<"query from x to y"<<endl;  
                scanf("%d %d",&x,&y);  
                cout<<query(x,y,1)<<endl;  
            }  
            else{  
                cout<<"add z from x to y:"<<endl;  
                scanf("%d %d %lld", &x, &y, &z);  
                update(x,y,1,z);  
                for(int i = 1; i <= n; i ++){  
                    printf("a[%d] = %lld\n",i,query(i,i,1));  
                }
            }  
        }  
    }

	return 0;
}



你可能感兴趣的:(模板)