bzoj2369 区间(贪心+dp+决策单调性+分治)

304 篇文章 2 订阅
132 篇文章 0 订阅

首先我们可以贪心地发现,肯定是取两个区间,对于包含的区间肯定不如选最大的那个,因此把被包含的都去掉,但是要注意更新答案哟。
好了,我们只剩下一堆l,r都单增的区间了,我们考虑区间i,他的最优决策为wi(wi< i),我们可以发现wi是单调不降的。

于是我们可以分治+决策单调性来做。
复杂度 O(nlogn) O ( n l o g n )

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1000010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(T==S){T=(S=buf)+fread(buf,1,1<<16,stdin);if(S==T) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m=0;ll ans=0,f[N];
struct seg{
    int l,r;
    friend bool operator<(seg a,seg b){return a.l==b.l?a.r<b.r:a.l<b.l;}
}a[N];
inline ll calc(int i,int j){
    return (ll)(a[j].r-a[i].l)*(max(a[i].r-a[j].l,0));
}
inline void cdq(int l,int r,int L,int R){
    if(l>r) return;
    if(L==R){
        for(int i=l;i<=r;++i) f[i]=calc(L,i);return;
    }int mid=l+r>>1,pos=0;
    for(int i=L;i<=min(R,mid-1);++i){
        ll res=calc(i,mid);
        if(res>f[mid]) f[mid]=res,pos=i;
    }if(pos) cdq(l,mid-1,L,pos),cdq(mid+1,r,pos,R);
    else cdq(l,mid-1,L,R),cdq(mid+1,r,L,R);
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();
    for(int i=1;i<=n;++i) a[i].l=read(),a[i].r=read();
    sort(a+1,a+n+1);int r=0;
    for(int i=1;i<=n;++i){
        if(a[i].r>r) a[++m]=a[i],r=a[i].r;
        else ans=max(ans,(ll)(a[i].r-a[i].l)*(a[m].r-a[m].l));
    }cdq(2,m,1,m);
    for(int i=2;i<=m;++i) ans=max(ans,f[i]);
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值