#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)
#define int ll
typedef long long ll;
typedef pair<int,int> pii;
const ll maxn=2e5+69;
const ll mod=1e9+7;
const ll inf=1e18;
int n,m;
pii a[maxn];
int pre[maxn][21],val[maxn][21];
int suf[maxn];
pii jump(int u)
{
int r=a[u].fi,res=0;
for2(i,20,0)
{
if (r-a[pre[u][i]].se < m)
{
u=pre[u][i];
res+=val[u][i];
}
}
return {pre[u][0],res+(pre[u][0]!=u)};
}
void sol()
{
cin >> n>> m;
for1(i,1,n)
{
cin >>a[i].se>> a[i].fi;
if (a[i].fi<a[i].se) a[i].fi+=m;
// cerr<<n<< "adsa\n";
}
sort(a+1,a+1+n);
suf[n]=n;
for2(i,n-1,1)
{
if (a[i].se<a[suf[i+1]].se) suf[i]=i;
else suf[i]=suf[i+1];
}
for1(i,1,n)
{
pre[i][0]=suf[upper_bound(a+1,a+1+n,make_pair(a[i].se-1,-inf))-a];
val[i][0]=(pre[i][0]!=i);
}
for1(j,1,20) for1(i,1,n)
{
pre[i][j]=pre[pre[i][j-1]][j-1];
val[i][j]=val[i][j-1]+val[pre[i][j-1]][j-1];
}
// for1(i,1,n) cerr <<a[i].se<<' '<<a[i].fi <<' '<< pre[i][0]<<' '<<val[i][0]<<'\n';
int res=inf;
for1(i,1,n)
{
pii j=jump(i);
if (a[i].fi-a[j.fi].se>=m) res=min(res,j.se);
}
if (res==inf) cout << "-1";
else cout << res+1;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("fire.inp","r",stdin);
// freopen("fire.out","w",stdout);
int t=1;//cin >> t;
while (t--)
{
sol();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6608 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |