#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)
{
// cerr<<i<<' '<< u<<" -> "<<pre[u][i] <<' '<<val[u][i]<<'\n';
res+=val[u][i];
u=pre[u][i];
}
}
// cerr<< "dmm "<< res<<'\n';
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,-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);
// cerr<<"gg "<< i<<' '<<j.fi<<' '<<j.se<<'\n';
}
}
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();
}
}
/*
11 551
489 51
109 307
215 252
138 509
25 95
151 191
91 202
474 50
137 383
324 529
204 467
4 551
489 51
138 509
25 95
91 202
4 9
7 2
5 8
1 4
3 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
2140 KB |
Output is correct |
40 |
Correct |
3 ms |
2124 KB |
Output is correct |
41 |
Correct |
3 ms |
2140 KB |
Output is correct |
42 |
Correct |
3 ms |
2140 KB |
Output is correct |
43 |
Correct |
3 ms |
2140 KB |
Output is correct |
44 |
Correct |
4 ms |
2140 KB |
Output is correct |
45 |
Correct |
3 ms |
2140 KB |
Output is correct |
46 |
Correct |
3 ms |
2140 KB |
Output is correct |
47 |
Correct |
4 ms |
2140 KB |
Output is correct |
48 |
Correct |
3 ms |
2140 KB |
Output is correct |
49 |
Correct |
3 ms |
2136 KB |
Output is correct |
50 |
Correct |
3 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
2140 KB |
Output is correct |
16 |
Correct |
3 ms |
2136 KB |
Output is correct |
17 |
Correct |
3 ms |
2136 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2272 KB |
Output is correct |
20 |
Correct |
95 ms |
71504 KB |
Output is correct |
21 |
Correct |
127 ms |
74576 KB |
Output is correct |
22 |
Correct |
131 ms |
74512 KB |
Output is correct |
23 |
Correct |
128 ms |
73812 KB |
Output is correct |
24 |
Correct |
131 ms |
74580 KB |
Output is correct |
25 |
Correct |
140 ms |
74068 KB |
Output is correct |
26 |
Correct |
128 ms |
74580 KB |
Output is correct |
27 |
Correct |
140 ms |
74520 KB |
Output is correct |
28 |
Correct |
133 ms |
74648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
2348 KB |
Output is correct |
13 |
Correct |
3 ms |
2136 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
109 ms |
71508 KB |
Output is correct |
16 |
Correct |
138 ms |
73808 KB |
Output is correct |
17 |
Correct |
134 ms |
74620 KB |
Output is correct |
18 |
Correct |
122 ms |
74612 KB |
Output is correct |
19 |
Correct |
134 ms |
74840 KB |
Output is correct |
20 |
Correct |
130 ms |
73680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
344 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
2140 KB |
Output is correct |
40 |
Correct |
3 ms |
2124 KB |
Output is correct |
41 |
Correct |
3 ms |
2140 KB |
Output is correct |
42 |
Correct |
3 ms |
2140 KB |
Output is correct |
43 |
Correct |
3 ms |
2140 KB |
Output is correct |
44 |
Correct |
4 ms |
2140 KB |
Output is correct |
45 |
Correct |
3 ms |
2140 KB |
Output is correct |
46 |
Correct |
3 ms |
2140 KB |
Output is correct |
47 |
Correct |
4 ms |
2140 KB |
Output is correct |
48 |
Correct |
3 ms |
2140 KB |
Output is correct |
49 |
Correct |
3 ms |
2136 KB |
Output is correct |
50 |
Correct |
3 ms |
2136 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
344 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
600 KB |
Output is correct |
61 |
Correct |
0 ms |
600 KB |
Output is correct |
62 |
Correct |
1 ms |
460 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
2140 KB |
Output is correct |
66 |
Correct |
3 ms |
2136 KB |
Output is correct |
67 |
Correct |
3 ms |
2136 KB |
Output is correct |
68 |
Correct |
3 ms |
2140 KB |
Output is correct |
69 |
Correct |
3 ms |
2272 KB |
Output is correct |
70 |
Correct |
95 ms |
71504 KB |
Output is correct |
71 |
Correct |
127 ms |
74576 KB |
Output is correct |
72 |
Correct |
131 ms |
74512 KB |
Output is correct |
73 |
Correct |
128 ms |
73812 KB |
Output is correct |
74 |
Correct |
131 ms |
74580 KB |
Output is correct |
75 |
Correct |
140 ms |
74068 KB |
Output is correct |
76 |
Correct |
128 ms |
74580 KB |
Output is correct |
77 |
Correct |
140 ms |
74520 KB |
Output is correct |
78 |
Correct |
133 ms |
74648 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
344 KB |
Output is correct |
86 |
Correct |
1 ms |
344 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
1 ms |
600 KB |
Output is correct |
89 |
Correct |
1 ms |
348 KB |
Output is correct |
90 |
Correct |
3 ms |
2348 KB |
Output is correct |
91 |
Correct |
3 ms |
2136 KB |
Output is correct |
92 |
Correct |
3 ms |
2140 KB |
Output is correct |
93 |
Correct |
109 ms |
71508 KB |
Output is correct |
94 |
Correct |
138 ms |
73808 KB |
Output is correct |
95 |
Correct |
134 ms |
74620 KB |
Output is correct |
96 |
Correct |
122 ms |
74612 KB |
Output is correct |
97 |
Correct |
134 ms |
74840 KB |
Output is correct |
98 |
Correct |
130 ms |
73680 KB |
Output is correct |
99 |
Correct |
126 ms |
74516 KB |
Output is correct |
100 |
Correct |
150 ms |
74772 KB |
Output is correct |
101 |
Correct |
133 ms |
74588 KB |
Output is correct |
102 |
Correct |
125 ms |
74616 KB |
Output is correct |
103 |
Correct |
137 ms |
74748 KB |
Output is correct |
104 |
Correct |
133 ms |
74832 KB |
Output is correct |
105 |
Correct |
112 ms |
73300 KB |
Output is correct |