打广告 51Nod - 1480

https://www.51nod.com/Challenge/Problem.html#!#problemId=1480

线段树离线处理线段 带权的不好处理 肯定要把不带权的线段更新到线段树上

答案分成三部分 对于一个带权线段 看所有被它完全包含的无权线段中最长的是谁 这部分线段树搞一下 然后就是左端点或右端点其中一个在其外 排个序完事了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=0x3f3f3f3f3f3f3f3f;
const int maxn=2e5+10;

struct node
{
    ll l,r,w;
};

node seg1[maxn],seg2[maxn];
ll maxx[16*maxn],tmp[4*maxn];
int n1,n2,n;

template <class T>
inline void _cin(T &ret)
{
    char c;
    ret=0;
    while((c=getchar())<'0'||c>'9');
    while(c>='0'&&c<='9'){
        ret=ret*10+(c-'0');
        c=getchar();
    }
}

bool cmpI(node n1,node n2)
{
    return n1.l<n2.l;
}

bool cmpII(node n1,node n2)
{
    return n1.r<n2.r;
}

void pushup(int cur)
{
    maxx[cur]=max(maxx[2*cur],maxx[2*cur+1]);
}

void update(int tar,ll val,int l,int r,int cur)
{
    int m;
    if(l==r){
        maxx[cur]=max(maxx[cur],val);
        return;
    }
    m=(l+r)/2;
    if(tar<=m) update(tar,val,l,m,2*cur);
    else update(tar,val,m+1,r,2*cur+1);
    pushup(cur);
}

ll query(int pl,int pr,int l,int r,int cur)
{
    ll res;
    int m;
    if(pl<=l&&r<=pr) return maxx[cur];
    res=0,m=(l+r)/2;
    if(pl<=m) res=max(res,query(pl,pr,l,m,2*cur));
    if(pr>m) res=max(res,query(pl,pr,m+1,r,2*cur+1));
    return res;
}

int main()
{
    ll tmaxx,tminn,ans;
    int i,j,pl,pr,t1,t2,t3;
    //scanf("%d%d",&n1,&n2);
    _cin(t1),_cin(t2);
    n1=t1,n2=t2;
    for(i=1;i<=n1;i++){
        //scanf("%lld%lld",&seg1[i].l,&seg1[i].r);
        _cin(t1),_cin(t2);
        seg1[i].l=t1,seg1[i].r=t2;
        tmp[++n]=seg1[i].l,tmp[++n]=seg1[i].r;
    }
    for(i=1;i<=n2;i++){
        //scanf("%lld%lld%lld",&seg2[i].l,&seg2[i].r,&seg2[i].w);
        _cin(t1),_cin(t2),_cin(t3);
        seg2[i].l=t1,seg2[i].r=t2,seg2[i].w=t3;
        tmp[++n]=seg2[i].l,tmp[++n]=seg2[i].r;
    }
    sort(seg1+1,seg1+n1+1,cmpI);
    sort(seg2+1,seg2+n2+1,cmpI);
    sort(tmp+1,tmp+n+1);
    n=unique(tmp+1,tmp+n+1)-tmp-1;
    ans=0;
    j=n1;
    for(i=n2;i>=1;i--){
        while(j>=1&&seg1[j].l>=seg2[i].l){
            pr=lower_bound(tmp+1,tmp+n+1,seg1[j].r)-tmp;
            update(pr,seg1[j].r-seg1[j].l,1,n,1);
            j--;
        }
        pl=lower_bound(tmp+1,tmp+n+1,seg2[i].l)-tmp;
        pr=lower_bound(tmp+1,tmp+n+1,seg2[i].r)-tmp;
        ans=max(ans,query(pl,pr,1,n,1)*seg2[i].w);
    }
    j=1,tmaxx=0;
    for(i=1;i<=n2;i++){
        while(j<=n1&&seg1[j].l<=seg2[i].l){
            tmaxx=max(tmaxx,seg1[j].r);
            j++;
        }
        ans=max(ans,max(0ll,min(tmaxx,seg2[i].r)-seg2[i].l)*seg2[i].w);
    }
    sort(seg1+1,seg1+n1+1,cmpII);
    sort(seg2+1,seg2+n2+1,cmpII);
    j=n1,tminn=N;
    for(i=n2;i>=1;i--){
        while(j>=1&&seg1[j].r>=seg2[i].r){
            tminn=min(tminn,seg1[j].l);
            j--;
        }
        ans=max(ans,max(0ll,seg2[i].r-max(tminn,seg2[i].l))*seg2[i].w);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/84869648