显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int a,b,l,r,ans=0;
void work()
{
int cir=2*(a+b);
if(r-l+1>=cir)
{
if(b>=a)
ans=a+1;
else
ans=2*a-b;
return;
}
int L=l%cir,R=r%cir;
if(R<=a)
ans=R-L+1;
else
if(R<=a+b)
{
if(L<=a)
ans=a-L+1;
else
ans=1;
}
else
if(b>=a)
{
if(R<=2*a+b-1)
{
if(L<=a)
ans=max(a-L+1,R-(a+b));
else
if(a<=a+b)
ans=R-(a+b)+1;
else
ans=R-L+1;
}
else
{
if(L<=a+b)
ans=a+1;
else
ans=2*a+b-L+1;
}
}
else
{
if(R<=a+2*b)
{
if(L<=a)
ans=a-L+1;
else
ans=a;
}
else
{
if(R<=2*a+b)
{
if(L<=a)
ans=a-L+1+R-(a+2*b)+1;
else
ans=R-(a+b)+2;
}
else
{
if(L<=a)
ans=a-L+1+a-b;
else
if(L<=a+b)
ans=a-b+1;
else
if(L<=2*a+b)
ans=2*a+b-L+1;
else
ans=1;
}
}
}
}
int main()
{
freopen("MrBA1.in","r",stdin);
freopen("MrBA1.out","w",stdout);
scanf("%d%d%d%d",&a,&b,&l,&r);
work();
printf("%d\n",ans);
return 0;
}