Sesti
Sesti
h>
#define endl '\n'
#define pb push_back
#define ll long long
#define fi first
#define se second
#define pll pair<ll,ll>
void tests()
{
ll n,d,ptr,res=1e9,i;
// ll *pok1,*pok2;
cin >> n >> d;
vector<pll>x(n);
for(auto &x:x)
{
cin >> x.fi >> x.se;
}
set<ll>st;
sort(x.begin(),x.end(),greater<pll>());
ptr=0;
for(i=0;i<n;i++)
{
while(x[ptr].se-x[i].se>=d)
{
st.insert(x[ptr].fi);
ptr++;
}
if(st.empty())
{
continue;
}
auto pok1=st.lower_bound(x[i].fi);
auto pok2=st.upper_bound(x[i].fi);
if(pok1!=st.begin())
{
pok1--;
res=min(res,x[i].fi-(*pok1));
}
if(pok2!=st.end())
{
res=min(res,*pok2-x[i].fi);
}
}
if(res==1e9)
{
res-=1e9+1;
}
cout << res << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt=1;
// cin >> tt;
while(tt--)
{
tests();
}
}