• 【模板】动态最值


    题目大意:维护一个长度为 N 的序列,支持单点删除,区间求最值。

    题解:实际需要维护的操作是指定下标查询,区间下标求最值。
    采用线段树维护区间的长度,若某个点被删除,则其长度减一。找到指定下标可以采用在线段树上二分序列长度,对于区间最值查询也是如此。

    代码如下

    #include <bits/stdc++.h>
    #define mp make_pair
    using namespace std;
    const int maxn=1e6+10;
    const int inf=0x3f3f3f3f;
    
    inline int read(){
    	int x=0,f=1;char ch;
    	do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
    	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
    	return f*x;
    }
    
    int n,m,a[maxn];
    struct node{
    	#define ls(o) t[o].lc
    	#define rs(o) t[o].rc
    	int lc,rc,sz,mi,mx;
    }t[maxn<<1];
    int tot,root;
    inline void pushup(int o){
    	t[o].mi=min(t[ls(o)].mi,t[rs(o)].mi);
    	t[o].mx=max(t[ls(o)].mx,t[rs(o)].mx);
    	t[o].sz=t[ls(o)].sz+t[rs(o)].sz;
    }
    int build(int l,int r){
    	int o=++tot;
    	if(l==r){t[o].sz=1,t[o].mi=t[o].mx=a[l];return o;}
    	int mid=l+r>>1;
    	ls(o)=build(l,mid),rs(o)=build(mid+1,r);
    	return pushup(o),o;
    }
    void modify(int o,int pos){
    	if(t[o].sz==1){t[o].sz=0,t[o].mi=inf,t[o].mx=-inf;return;}
    	int lsz=t[ls(o)].sz;
    	if(pos<=lsz)modify(ls(o),pos);
    	else modify(rs(o),pos-lsz);
    	pushup(o);
    }
    node query(int o,int x,int y){
    	if(t[o].sz==y-x+1)return t[o];
    	int lsz=t[ls(o)].sz;
    	if(y<=lsz)return query(ls(o),x,y);
    	else if(x>lsz)return query(rs(o),x-lsz,y-lsz);
    	else{
    		node res1=query(ls(o),x,lsz);
    		node res2=query(rs(o),1,y-lsz);
    		node ret;
    		ret.mi=min(res1.mi,res2.mi),ret.mx=max(res1.mx,res2.mx);
    		return ret;
    	}
    }
    
    void read_and_parse(){
    	n=read(),m=read();
    	for(int i=1;i<=n;i++)a[i]=read();
    	root=build(1,n);	
    }
    
    void solve(){
    	while(m--){
    		int opt=read();
    		if(opt==1){
    			int k=read();
    			modify(root,k);
    		}else{
    			int l=read(),r=read();
    			node ret=query(root,l,r);
    			printf("%d %d
    ",ret.mi,ret.mx);
    		}
    	}
    }
    
    int main(){
    	read_and_parse();
    	solve();
    	return 0;
    }
    
  • 相关阅读:
    使用vue来开发一个下拉菜单组件(2)
    使用vue来开发一个下拉菜单组件(1)
    在Vue中引入Bootstrap,Font-awesome
    九宫格表格样式
    web upload 上传多张图片实例
    input 的multiple 上传多个文件
    常用 验证码 JS 代码
    最全 H5 form 表单 + 正则验证
    mysql的指令
    Ant编译MapReduce程序
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10793671.html
Copyright © 2020-2023  润新知