链接
题解
这题挺好玩的
对于两个有交点的线段,假设右端点比较大的是 [ l 2 , r 2 ] [l_2,r_2] [l2,r2],右端点比较小的是 [ l 1 , r 1 ] [l_1,r_1] [l1,r1],那么肯定 r 1 ∈ [ l 2 , r 2 ] r_1 \in [l_2,r_2] r1∈[l2,r2]
如果这两个线段有包含关系,即 l 2 ≤ l 1 ≤ r 1 ≤ r 2 l_2 \le l_1 \le r_1 \le r_2 l2≤l1≤r1≤r2,那么异或的结果就是 r 2 − r 1 + l 1 − l 2 r_2-r_1+l_1-l_2 r2−r1+l1−l2。
如果没有包含关系,则异或的结果是 r 2 − r 1 + l 2 − l 1 r_2-r_1+l_2-l_1 r2−r1+l2−l1
你会发现,如果你把包含的当不包含的算,或者把不包含的当包含的算,算出来的结果都会比预期的小
所以,我可以都算一遍,然后取 m a x max max,不合法的答案肯定会被更新掉
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct SegmentTree
{
ll mn[maxn<<2], mx[maxn<<2], sum[maxn<<2], add[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
void maketag_set(ll o, ll v)
{
add[o]=0;
set[o]=v;
mx[o]=mn[o]=v;
sum[o]=(R[o]-L[o]+1)*v;
}
void maketag_add(ll o, ll v)
{
add[o]+=v;
mx[o]+=v, mn[o]+=v;
sum[o]+=(R[o]-L[o]+1)*v;
}
void pushdown(ll o)
{
if(L[o]==R[o])return;
if(~set[o])
{
maketag_set(o<<1,set[o]);
maketag_set(o<<1|1,set[o]);
set[o]=-1;
}
if(add[o])
{
maketag_add(o<<1,add[o]);
maketag_add(o<<1|1,add[o]);
add[o]=0;
}
}
void pushup(ll o)
{
mx[o]=max(mx[o<<1],mx[o<<1|1]);
mn[o]=min(mn[o<<1],mn[o<<1|1]);
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void build(ll o, ll l, ll r, ll* array=NULL)
{
ll mid(l+r>>1);
L[o]=l, R[o]=r;
add[o]=0;
set[o]=-1;
if(l==r)
{
if(array)mn[o]=mx[o]=sum[o]=array[l];
else mn[o]=mx[o]=sum[o]=-linf;
return;
}
build(o<<1,l,mid,array);
build(o<<1|1,mid+1,r,array);
pushup(o);
}
void Set(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
pushdown(o);
if(l<=mid)Set(o<<1,l,r,v);
if(r>mid)Set(o<<1|1,l,r,v);
pushup(o);
}
void Add(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
pushdown(o);
if(l<=mid)Add(o<<1,l,r,v);
if(r>mid)Add(o<<1|1,l,r,v);
pushup(o);
}
ll Sum(ll o, ll l, ll r)
{
pushdown(o);
ll mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum[o];
if(l<=mid)ans+=Sum(o<<1,l,r);
if(r>mid)ans+=Sum(o<<1|1,l,r);
return ans;
}
ll Min(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(linf);
if(l<=L[o] and r>=R[o])return mn[o];
pushdown(o);
if(l<=mid)ans=min(ans,Min(o<<1,l,r));
if(r>mid)ans=min(ans,Min(o<<1|1,l,r));
return ans;
}
ll Max(ll o, ll l, ll r)
{
ll mid(L[o]+R[o]>>1), ans(-linf);
if(l<=L[o] and r>=R[o])return mx[o];
pushdown(o);
if(l<=mid)ans=max(ans,Max(o<<1,l,r));
if(r>mid)ans=max(ans,Max(o<<1|1,l,r));
return ans;
}
}segtree;
struct Line
{
ll l, r;
}seg[maxn];
struct Lisan
{
int tmp[maxn], tot;
void clear(){tot=0;}
void insert(int x){tmp[++tot]=x;}
void run()
{
sort(tmp+1,tmp+tot+1);
tot=unique(tmp+1,tmp+tot+1)-tmp-1;
}
void lisan(int *a, int len)
{
for(int i=1;i<=len;i++)a[i]=lower_bound(tmp+1,tmp+tot+1,a[i])-tmp;
}
int lisan(int x)
{
return lower_bound(tmp+1,tmp+tot+1,x)-tmp;
}
}ls;
ll n;
int main()
{
ll i;
n=read();
rep(i,1,n)seg[i].l=read(), seg[i].r=read();
sort(seg+1,seg+n+1,[](Line s1, Line s2){return s1.r<s2.r;});
rep(i,1,n)ls.lisan(seg[i].l), ls.insert(seg[i].r);
ls.run();
ll ans=0;
// 没有包含关系 r2-r1 + l2-l1 = r2+l2 + (-r1-l1)
segtree.build(1,1,ls.tot);
rep(i,1,n)
{
ll posl=ls.lisan(seg[i].l), posr=ls.lisan(seg[i].r);
ans = max( ans, segtree.Max(1,posl,posr) + seg[i].r+seg[i].l );
ll t = segtree.Max(1,posr,posr);
segtree.Set(1,posr,posr,max(t,-seg[i].l-seg[i].r));
}
// 有包含关系 r2-r1 + l1-l2 = r2-l2 + l1-r1
segtree.build(1,1,ls.tot);
rep(i,1,n)
{
ll posl=ls.lisan(seg[i].l), posr=ls.lisan(seg[i].r);
ans = max( ans, segtree.Max(1,posl,posr) + seg[i].r-seg[i].l );
ll t = segtree.Max(1,posr,posr);
segtree.Set(1,posr,posr,max(t,seg[i].l-seg[i].r));
}
printf("%lld\n",ans);
return 0;
}