CF161C Abracadabra
*2400.
分治,每次有 \(4\) 种情况:左左,左右,右右,右左(相对于当前对称轴)。
复杂度看似是 \(O(n^2)\) 的,但是我们可以用以个剪枝将其优化到 \(O(\log n)\):如果两个区间完全无交,直接返回 \(0\)。
#include <bits/stdc++.h>
#define GC c=getchar()
#define IG isdigit(c)
template<class T=int>T frd(T x=0,char GC,bool f=1)
{
for(;!IG;GC)f=c!='-';for(;IG;GC)x=x*10+(c^48);return f?x:-x;
}
using namespace std;
int solve(int l1,int r1,int l2,int r2,int len=1<<29)
{
if(l1>r1||l2>r2) return 0;
int ans(min(r1,r2)-max(l1,l2)+1);
if(l1<=l2&&r1>=r2||l2<=l1&&r2>=r1) return ans;
ans=max(ans,solve(min(l1,len),min(r1,len-1),max(l2,len+1)-len,max(r2,len)-len,len>>1));
ans=max(ans,solve(min(l1,len),min(r1,len-1),min(l2,len),min(r2,len-1),len>>1));
ans=max(ans,solve(max(l1,len+1)-len,max(r1,len)-len,max(l2,len+1)-len,max(r2,len)-len,len>>1));
ans=max(ans,solve(max(l1,len+1)-len,max(r1,len)-len,min(l2,len),min(r2,len-1),len>>1));
return ans;
}
signed main()
{
int l1(frd()),r1(frd()),l2(frd()),r2(frd());
printf("%lld\n",max(0ll,solve(l1,r1,l2,r2)));
return 0;
}