(模板)线段树set(a,b) query(a,b)

#include <stdio.h>
#include <string.h>
#define MAX_N 100000
#define INF   (-(MAX_N))
typedef float Type;
int ol,or;
Type _min,_max,_sum,v;
Type sumv[MAX_N],minv[MAX_N],maxv[MAX_N],addv[MAX_N],setv[MAX_N];
Type max(Type a,Type b){
        return a > b ? a : b;
}
Type min(Type a,Type b){
        return a < b ? a : b;
}
void initVal(){
        _min = MAX_N;
        _max = -100;
        _sum = 0;
}
void maintain(int o,int l,int r){
        int lc=o*2,rc=o*2+1;
        if(r > l && setv[o]==INF){
                sumv[o]=0;
                minv[o]=MAX_N;
                maxv[o]=INF;
                sumv[o] = sumv[lc] + sumv[rc];
                minv[o] = min(minv[lc],minv[rc]);
                maxv[o] = max(maxv[lc],maxv[rc]);
                printf("maintain %d %d (%d %d)min:%f max:%f sum:%f pos:%d\n",l,r,lc,rc,minv[o],maxv[o],sumv[o],o);
        }
}
void pushdown(int o,int l,int r,int m){
        int lc = o * 2;
        int rc = o * 2 + 1;
        if(setv[o] != INF){
                maxv[lc] = minv[lc] = maxv[rc] = minv[rc] = setv[lc] = setv[rc] = setv[o];
                sumv[lc] = setv[o] * (m - l + 1);
                sumv[rc] = setv[o] * (r - m);
                setv[o]=INF;
        }
}
void update(int o,int l,int r){
        int lc = o * 2;
        int rc = o * 2 + 1;
        if( ol <= l && or >= r){
                printf("set %d %d val %f pos%d\n",l,r,v,o);
                minv[o]=maxv[o]=setv[o] = v;
                sumv[o]=v*(r-l+1);
        }else{
                int m =l + (r - l)/2;
                pushdown(o,l,r,m);
                if( ol <= m){
                        update(lc,l,m);
                }else{
                        maintain(lc,l,m);
                }
                if( or > m){
                        update(rc,m+1,r);
                }else{
                        maintain(rc,m+1,r);
                }
        }
        maintain(o,l,r);
}
void query(int o,int l,int r){
        if(setv[o] != INF){
                printf("(1:%d %d)\n",l,r);
                _sum += setv[o] * (min(or,r) - max(ol,l)+1);
                _min = min(_min,setv[o]);
                _max = max(_max,setv[o]);
        }else if(ol <= l && or >=r){
                printf("(2:%d %d)\n",l,r);
                _sum += sumv[o];
                _min = min(_min,minv[o]);
                _max = max(_max,maxv[o]);
        }else{
                int m = l + (r - l)/2;
                if( ol <= m){ query(o * 2,l,m); }
                if( or > m){ query(o * 2 + 1,m+1,r); }
        }               
}
int main(){
        int i;
        for(i=0;i<100;i++){
                setv[i]=INF;
        }
        while(10){
                int type;
                initVal();
                printf("1.set(l,r,v):\n");
                printf("2.query(l,r):\n");
                scanf("%d",&type);
                if(type==1){
                        scanf("%d%d%f",&ol,&or,&v);
                        update(1,1,8);
                }else if(type==2){
                        scanf("%d%d",&ol,&or);
                        query(1,1,8);
                        printf("result l:%d r:%d max:%f min:%f sum:%f\n",ol,or,_max,_min,_sum);
                }
        }
        return 0;
}
    原文作者:B树
    原文地址: https://blog.csdn.net/sapphirestart/article/details/44871593
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注