树状数组--区间更新与区间求和--区间最大值最小值模板

区间更新与区间求和:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10000+10
#define LL long long
LL sum[N],lazy[N];
///向上更新求和
void pushUp(int root){
    sum[root]=sum[root<<1] + sum[(root<<1)+1];
}
///向下更新求和
void pushDown(int root,int len){
    lazy[root<<1]+=lazy[root];
    lazy[(root<<1)+1]+=lazy[root];
    sum[root<<1]+=(len-(len>>1))*lazy[root];
    sum[(root<<1)+1]+=(len>>1)*lazy[root];
    lazy[root]=0;
}
///建树
void build(int l,int r,int root){
    lazy[root]=0;
    if(l==r){
        sum[root]=0;
        return;
    }
    int m=(l+r)>>1;
    build(l,m,root<<1);
    build(m+1,r,(root<<1)+1);
    pushUp(root);
}
///在(l,r)区间内,根节点为root,将(L,R)区间的每个数 +c
void update(int L,int R,long long c,int l,int r,int root){
   if(L<=l&&R>=r){
        lazy[root]+=c;
        sum[root]+=c*(r-l+1);
        return;
    }
    if(lazy[root]) pushDown(root,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) 
		update(L,R,c,l,m,root<<1);
    if(R>m) 
		update(L,R,c,m+1,r,(root<<1)+1);
    pushUp(root);
}
///在(l,r)区间内,根节点为root,求(L,R)区间的数的和
LL query(int L,int R,int l,int r,int root){
    if(L<=l&&R>=r) return sum[root];
    if(lazy[root]){
        pushDown(root,r-l+1);
    }
    int m=(l+r)>>1;
    LL ans=0;
    if(L<=m) ans+=query(L,R,l,m,root<<1);
    if(R>m) ans+=query(L,R,m+1,r,(root<<1)+1);
    return ans;
}

区间查询最值:下面列举的是求最小值!

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define N 4000001
#define INF 0x3f3f3f3f
int Min[N];
int Max[N];
int lazy[N];
bool vis[N];
///向上更新最值
void PushUp(int root){
    Min[root]=min(Min[root<<1],Min[(root<<1)+1]);
    Max[root]=max(Max[root<<1],Max[(root<<1)+1]);
}
///向下更新最值
void PushDown(int root,int c){
    Min[root<<1]=Min[(root<<1)+1]=lazy[root];
    Max[root<<1]=Max[(root<<1)+1]=lazy[root];
    vis[root<<1]=vis[(root<<1)+1]=true;
    lazy[root<<1]=lazy[(root<<1)+1]=lazy[root];
    vis[root]=false;
}
///建树,根节点为root,区间为(l,r)
void build(int l,int r,int root){
    if(l==r){
        scanf("%d",&Min[root]);
        Max[root]=Min[root];
        return;
    }
    int m = (l+r)>>1;
    build(l,m,root<<1);
    build(m+1,r,(root<<1)+1);
    PushUp(root);
}
///在(l,r)区间内,根节点为root,求(L,R)区间的最小值
int getMin(int L,int R,int l,int r,int root){
  //  if(vis[root]) return Min[root];
    if(L<=l&&R>=r) return Min[root];
    if(vis[root]) PushDown(root,lazy[root]);
    int m = (l+r)>>1,ret=INF;
    if(L<=m) ret = min(ret,getMin(L,R,l,m,root<<1));
    if(R>m) ret = min(ret,getMin(L,R,m+1,r,(root<<1)+1));
    return ret;
}
///在(l,r)区间内,根节点为root,求(L,R)区间的最大值
int getMax(int L,int R,int l,int r,int root){
    //if(vis[root]) return Max[root];
    if(L<=l&&R>=r) return Max[root];
    if(vis[root]) PushDown(root,lazy[root]);
    int m = (l+r)>>1,ret=0;
    if(L<=m) ret = max(ret,getMax(L,R,l,m,root<<1));
    if(R>m) ret = max(ret,getMax(L,R,m+1,r,(root<<1)+1));
    return ret;
}
///在(l,r)区间内,根节点为root,将(L,R)区间的数更新为 c
void update(int L,int R,int c,int l,int r,int root){
    if(L<=l&&R>=r){
        Min[root]=Max[root]=c;
        lazy[root]=c;
        vis[root]=true;
        return ;
    }
    if(vis[root]) PushDown(root,c);
    int m = (l+r)>>1;
    if(L<=m) update(L,R,c,l,m,root<<1);
    if(R>m) update(L,R,c,m+1,r,(root<<1)+1);
    PushUp(root);
}
int main(){
    int n,m,a,b,c,t;
    while(scanf("%d",&n)!=EOF){
        memset(vis,false,sizeof(vis));
        build(1,n,1);
        scanf("%d\n",&m);
        while(m--){
            scanf("%d",&t);
            if(t==0){
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }else{
                scanf("%d%d",&a,&b);
                printf("%d\n",getMin(a,b,1,n,1));
            }
        }
    }
    return 0;
}
发布了150 篇原创文章 · 获赞 73 · 访问量 6576

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/104310596
今日推荐