zoukankan      html  css  js  c++  java
  • 【NOI2016T4】区间-线段树+离散化+决策单调性优化

    测试地址:区间

    做法:先把所有区间按长度从小到大排序,我们发现只要选定了左右端点,方案的花费就已经确定了,那么就把中间的区间都加上,然后询问有没有点被覆盖的次数达到m即可。这个显然可以用线段树处理,使每次询问复杂度为O(logn)。由于区间端点可能很大,所以需要离散化。但是,关键就在于方案左右端点的选择上,如果枚举左右端点,复杂度O(n^2*logn),显然不能接受。那么考虑优化,我们又发现左端点确定的情况下,一定存在一个点使得将这个点左边的点作为右端点不合法,而将这个点右边的点作为右端点合法,满足单调性,所以可以枚举左端点然后二分查找最优的右端点,复杂度O(n*log^2 n),虽然得到了优化,然而还是过大。然后我们又观察到,不存在两种合法方案使得一个方案的左端点大于另一个方案的左端点,而这个方案的右端点小于另一个方案的右端点,即满足决策单调性,所以只要用一个右移的指针表示右端点即可,复杂度O(nlogn),可以通过。

    以下是本人代码:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define inf 1000000000
    using namespace std;
    struct forsort {int pos,lor,val;} f[1000010];
    struct inter {int l,r;} t[500010];
    struct segnode
    {
      int p;
      int mx,sum;
    }seg[3000010];
    int n,m,tot=0,s[1000010],ans=inf;
    
    bool cmp(forsort a,forsort b)
    {
      return a.val<b.val;
    }
    
    bool cmp2(inter a,inter b)
    {
      return a.r-a.l<b.r-b.l;
    }
    
    void pushdown(int no)
    {
      if (seg[no].p)
      {
        seg[no<<1].p+=seg[no].p;
    	seg[no<<1|1].p+=seg[no].p;
    	seg[no<<1].sum+=seg[no].p;
    	seg[no<<1|1].sum+=seg[no].p;
    	seg[no<<1].mx+=seg[no].p;
    	seg[no<<1|1].mx+=seg[no].p;
        seg[no].p=0;
      }
    }
    
    void pushup(int no)
    {
      seg[no].sum=seg[no<<1].sum+seg[no<<1|1].sum;
      seg[no].mx=max(seg[no<<1].mx,seg[no<<1|1].mx);
    }
    
    void buildtree(int no,int l,int r)
    {
      seg[no].p=seg[no].mx=seg[no].sum=0;
      if (l==r) return;
      int mid=(l+r)>>1;
      buildtree(no<<1,l,mid);
      buildtree(no<<1|1,mid+1,r);
    }
    
    void modify(int no,int l,int r,int s,int t,int val)
    {
      if (l>=s&&r<=t)
      {
        seg[no].p+=val;
    	seg[no].sum+=val;
    	seg[no].mx+=val;
    	return;
      }
      pushdown(no);
      int mid=(l+r)>>1;
      if (s<=mid) modify(no<<1,l,mid,s,t,val);
      if (t>mid) modify(no<<1|1,mid+1,r,s,t,val);
      pushup(no);
    }
    
    void work()
    {
      int r=0;
      for(int l=1;l<=n;l++)
      {
        while(seg[1].mx<m&&r<n)
    	{
    	  r++;
    	  modify(1,1,tot,t[r].l,t[r].r,1);
    	}
    	if (seg[1].mx>=m) ans=min(ans,(s[t[r].r]-s[t[r].l])-(s[t[l].r]-s[t[l].l]));
    	modify(1,1,tot,t[l].l,t[l].r,-1);
      }
    }
    
    int main()
    {
      scanf("%d%d",&n,&m);
      for(int i=1;i<=n;i++)
        scanf("%d%d",&t[i].l,&t[i].r);
      sort(t+1,t+n+1,cmp2);
      
      for(int i=1;i<=n;i++)
      {
        f[++tot].pos=i,f[tot].val=t[i].l,f[tot].lor=0;
    	f[++tot].pos=i,f[tot].val=t[i].r,f[tot].lor=1;
      }
      f[0].pos=f[0].val=-1;
      sort(f+1,f+tot+1,cmp);
      
      tot=0;
      for(int i=1;i<=2*n;i++)
      {
        if (f[i].val!=f[i-1].val) s[++tot]=f[i].val;
    	if (!f[i].lor) t[f[i].pos].l=tot;
    	else t[f[i].pos].r=tot;
      }
      
      buildtree(1,1,tot);
      
      work();
      
      if (ans!=inf) printf("%d",ans);
      else printf("-1");
      
      return 0;
    }
    


  • 相关阅读:
    docker tar 镜像 容器相互转换
    JavaScript执行上下文
    JavaScript 作用域
    原型与原型链
    使用Navicat for Oracle新建表空间、用户及权限赋予
    PL/SQL Developer使用教程(中文)
    一步一步使用bootstrap开发一个博客模板
    How to create a simple blog using ASP.NET MVC
    一个有意思的编程网站
    一个很好的java编程国外网站
  • 原文地址:https://www.cnblogs.com/Maxwei-wzj/p/9793739.html
Copyright © 2011-2022 走看看