zoukankan      html  css  js  c++  java
  • 【BZOJ4653】【NOI2016】区间(线段树)

    【BZOJ4653】【NOI2016】区间(线段树)

    题面

    BZOJ

    题解

    (NOI)良心送分题??
    既然是最大长度减去最小长度
    莫名想到那道反复减边求最小生成树
    从而求出最小的比值

    所以这题的套路是一样的

    按照长度排序之后
    依次加入
    如果当前有被覆盖了超过(m)次的点
    就从前面开始,依次把线段拿走
    每次更新一下答案就好啦

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define INF 2e9
    #define MAX 520000
    #define lson (now<<1)
    #define rson (now<<1|1)
    #define rg register
    inline int read()
    {
        int x=0,t=1;char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return x*t;
    }
    struct Seg{int l,r,v;}p[MAX];
    bool operator<(Seg a,Seg b){return a.v<b.v;}
    struct Node
    {
    	int v,ly;
    }t[MAX<<5];
    int al,ar;
    
    void Modify(int now,int l,int r,int w)
    {
    	if(al<=l&&r<=ar){t[now].v+=w;t[now].ly+=w;return;}
    	int mid=(l+r)>>1;
    	if(al<=mid)Modify(lson,l,mid,w);
    	if(ar>mid)Modify(rson,mid+1,r,w);
    	t[now].v=max(t[lson].v,t[rson].v)+t[now].ly;
    }
    inline int Query(){return t[1].v+t[1].ly;}
    int n,m,S[MAX<<1],tot;
    int main()
    {
    	n=read();m=read();
    	for(rg int i=1;i<=n;++i)
    	{
    		S[++tot]=p[i].l=read();
    		S[++tot]=p[i].r=read();
    		p[i].v=p[i].r-p[i].l;
    	}
    	sort(&S[1],&S[tot+1]);
    	tot=unique(&S[1],&S[tot+1])-S-1;
    	for(rg int i=1;i<=n;++i)
    	{
    		p[i].l=lower_bound(&S[1],&S[tot+1],p[i].l)-S;
    		p[i].r=lower_bound(&S[1],&S[tot+1],p[i].r)-S;
    	}
    	sort(&p[1],&p[n+1]);
    	rg int pos=1,ans=INF;
    	for(rg int i=1;i<=n;++i)
    	{
    		al=p[i].l;ar=p[i].r;
    		Modify(1,1,tot,1);
    		if(Query()==m)
    		{
    			while(Query()==m)
    			{
    				al=p[pos].l;ar=p[pos].r;
    				ans=min(ans,p[i].v-p[pos].v);
    				Modify(1,1,tot,-1);
    				pos++;
    			}
    		}
    	}
    	printf("%d
    ",ans==INF?-1:ans);
    	return 0;
    }
    
    
  • 相关阅读:
    socket.io的抽象实现:engine.io
    Juicer javascript 模板引擎
    phonegap 开发案例
    安卓升级提示 phoneGap APK软件更新提示
    搭建Titanium开发环境
    phonegap WebApp
    vue-cli 配置vue项目环境笔记
    从一张表里面查询后十条数据的前七条
    js实现文字搬运工
    MySQL —— 数据库数据备份命令 mysqlump 的使用
  • 原文地址:https://www.cnblogs.com/cjyyb/p/8306067.html
Copyright © 2011-2022 走看看