http://www.elijahqi.win/archives/3213
Description
For an interval set
{A1, A2...Ak} (K>1, Ai is not equal to Aj (i is not equal to J), define its weight
S=|A1∪A2∪……AK|*|A1∩A2……∩Ak|
That is, the length of their intersection interval multiplied by the length of their union interval.
Obviously, if these intervals do not intersect, the weight is 0.
Your Task
gives you several unequal intervals, and selects several intervals to maximize their weights.
Input
The first line n represents the number of intervals, the
next n lines, two integers in each line lr describe an interval [l,r]
Output
Output maximum weights in one row
Sample Input
4
1 6
4 8
2 7
3 5
Sample Output
24
HINT
Example explanation
Choosing [1,6] and [2,7] is optimal.
data convention
100%:1< N<=10^6,1<=L< R<=10^6
Source
Suddenly I feel that the complexity is not very correct
Same as bzoj2687
https://blog.csdn.net/elijahqi/article/details/80077458
#include<cstdio>
#include<cctype>
#include<algorithm>
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
const int N=1e6+10;
struct node{
int l,r;
}line[N],t[N];
ll dp[N],ans;int n;
inline bool cmp(const node &a,const node &b){return a.l==b.l?a.r>b.r:a.l<b.l;};
inline void gao(int l,int r,int L,int R){
if(l>r) return;
if (L==R){
for (int i=l;i<=r;++i) dp[i]=(ll)(line[i].r-line[L].l)*(line[L].r-line[i].l);return;
}
int mid=l+r>>1;int p=0;
for (int i=L;i<=min(R,mid-1);++i){
ll tmp=(ll)(line[mid].r-line[i].l)*(line[i].r-line[mid].l);
if (tmp>dp[mid]) dp[mid]=tmp,p=i;
}if (!p) gao(l,mid-1,L,mid-1),gao(mid+1,r,L,R);
else gao(l,mid-1,L,p),gao(mid+1,r,p,R);
}
int main(){
freopen("bzoj2369.in","r",stdin);
n=read();int l=0,r=0,cnt=0;
for (int i=1;i<=n;++i) t[i].l=read(),t[i].r=read();
sort(t+1,t+n+1,cmp);
for (int i=1;i<=n;++i){
if (t[i].r>r) line[++cnt]=t[i],r=t[i].r,l=t[i].l;
else ans=max(ans,(ll)(t[i].r-t[i].l)*(r-l));
}
gao(2,cnt,1,cnt);
for (int i=1;i<=cnt;++i) ans=max(ans,dp[i]);
printf("%lld\n",ans);
return 0;
}