#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 2e9 + 7;
const ll BASE = 137;
const int szBL = 320;
int m, n;
pii a[N];
int spt[N][25];
pii mx[N][25];
void solution () {
cin >> n >> m;
rep (i, 1, n) {
cin >> a[i].fs >> a[i].se;
if (a[i].se < a[i].fs) a[i].se = m + a[i].se;
}
sort (a + 1, a + 1 + n, [] (pii A, pii B) { return A.fs < B.fs || (A.fs == B.fs && A.se > B.se); });
rep (i, 1, n) {
mx[i][0] = MP(a[i].se, i);
}
for (int j = 1; (1 << j) <= n; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
mx[i][j] = max(mx[i][j - 1], mx[i + (1 << j - 1)][j - 1]);
auto get = [] (int L, int R) -> pii {
assert (L <= R);
int K = 31 - __builtin_clz(R - L + 1);
return max(mx[L][K], mx[R - (1 << K) + 1][K]);
};
rep (i, 1, n) {
int lf = i, rt = n;
while (lf < rt) {
int mid = lf + rt + 1 >> 1;
if (a[mid].fs <= a[i].se) lf = mid;
else rt = mid - 1;
}
spt[i][0] = get(i, lf).se;
// cout << i << " "<<spt[i][0] <<"\n";
}
rep (j, 1, 20)
rep (i, 1, n)
spt[i][j] = spt[spt[i][j - 1]][j - 1];
int ans = INF;
rep (i, 1, n) {
int toreach = m + a[i].fs;
int cur = i, res = 0;
reb (j, 20, 0) {
if (a[spt[cur][j]].se < toreach) {
cur = spt[cur][j];
res += (1 << j);
}
}
++res;
cur = spt[cur][0];
// cout << i<<" "<<cur <<" "<<toreach<<"\n";
if (a[cur].se >= toreach) ans = min(ans, res + 1);
}
if (ans == INF) cout << -1 <<"\n";
else cout << ans <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
6 2
1 5 2 3 1 1
1 2 3 3 2
4 5
6 6
*/
Compilation message
Main.cpp: In function 'void solution()':
Main.cpp:45:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
45 | mx[i][j] = max(mx[i][j - 1], mx[i + (1 << j - 1)][j - 1]);
| ~~^~~
Main.cpp:54:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = lf + rt + 1 >> 1;
| ~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4444 KB |
Output is correct |
23 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4444 KB |
Output is correct |
23 |
Correct |
0 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4444 KB |
Output is correct |
23 |
Correct |
0 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6616 KB |
Output is correct |
39 |
Correct |
3 ms |
8792 KB |
Output is correct |
40 |
Correct |
2 ms |
8764 KB |
Output is correct |
41 |
Correct |
2 ms |
8948 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8796 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
1 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6532 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
49 ms |
60692 KB |
Output is correct |
21 |
Correct |
73 ms |
60500 KB |
Output is correct |
22 |
Correct |
76 ms |
60680 KB |
Output is correct |
23 |
Correct |
60 ms |
60500 KB |
Output is correct |
24 |
Correct |
71 ms |
60500 KB |
Output is correct |
25 |
Correct |
77 ms |
60500 KB |
Output is correct |
26 |
Correct |
69 ms |
60496 KB |
Output is correct |
27 |
Correct |
65 ms |
60500 KB |
Output is correct |
28 |
Correct |
70 ms |
60620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8792 KB |
Output is correct |
15 |
Correct |
53 ms |
60500 KB |
Output is correct |
16 |
Correct |
72 ms |
60708 KB |
Output is correct |
17 |
Correct |
66 ms |
60500 KB |
Output is correct |
18 |
Correct |
68 ms |
60684 KB |
Output is correct |
19 |
Correct |
65 ms |
60692 KB |
Output is correct |
20 |
Correct |
61 ms |
60704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
0 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4444 KB |
Output is correct |
23 |
Correct |
0 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
1 ms |
6492 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6616 KB |
Output is correct |
39 |
Correct |
3 ms |
8792 KB |
Output is correct |
40 |
Correct |
2 ms |
8764 KB |
Output is correct |
41 |
Correct |
2 ms |
8948 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
2 ms |
8796 KB |
Output is correct |
48 |
Correct |
2 ms |
8796 KB |
Output is correct |
49 |
Correct |
1 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8796 KB |
Output is correct |
51 |
Correct |
0 ms |
4444 KB |
Output is correct |
52 |
Correct |
1 ms |
4440 KB |
Output is correct |
53 |
Correct |
0 ms |
4444 KB |
Output is correct |
54 |
Correct |
0 ms |
4444 KB |
Output is correct |
55 |
Correct |
0 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 ms |
4444 KB |
Output is correct |
57 |
Correct |
1 ms |
4440 KB |
Output is correct |
58 |
Correct |
0 ms |
4444 KB |
Output is correct |
59 |
Correct |
0 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6532 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
1 ms |
6492 KB |
Output is correct |
63 |
Correct |
1 ms |
6492 KB |
Output is correct |
64 |
Correct |
1 ms |
6492 KB |
Output is correct |
65 |
Correct |
2 ms |
8796 KB |
Output is correct |
66 |
Correct |
2 ms |
8796 KB |
Output is correct |
67 |
Correct |
2 ms |
9048 KB |
Output is correct |
68 |
Correct |
2 ms |
8796 KB |
Output is correct |
69 |
Correct |
2 ms |
8796 KB |
Output is correct |
70 |
Correct |
49 ms |
60692 KB |
Output is correct |
71 |
Correct |
73 ms |
60500 KB |
Output is correct |
72 |
Correct |
76 ms |
60680 KB |
Output is correct |
73 |
Correct |
60 ms |
60500 KB |
Output is correct |
74 |
Correct |
71 ms |
60500 KB |
Output is correct |
75 |
Correct |
77 ms |
60500 KB |
Output is correct |
76 |
Correct |
69 ms |
60496 KB |
Output is correct |
77 |
Correct |
65 ms |
60500 KB |
Output is correct |
78 |
Correct |
70 ms |
60620 KB |
Output is correct |
79 |
Correct |
1 ms |
4444 KB |
Output is correct |
80 |
Correct |
0 ms |
4444 KB |
Output is correct |
81 |
Correct |
1 ms |
4440 KB |
Output is correct |
82 |
Correct |
0 ms |
4444 KB |
Output is correct |
83 |
Correct |
0 ms |
4444 KB |
Output is correct |
84 |
Correct |
0 ms |
4444 KB |
Output is correct |
85 |
Correct |
0 ms |
4444 KB |
Output is correct |
86 |
Correct |
1 ms |
6492 KB |
Output is correct |
87 |
Correct |
1 ms |
6492 KB |
Output is correct |
88 |
Correct |
1 ms |
6492 KB |
Output is correct |
89 |
Correct |
1 ms |
6492 KB |
Output is correct |
90 |
Correct |
2 ms |
8796 KB |
Output is correct |
91 |
Correct |
2 ms |
8796 KB |
Output is correct |
92 |
Correct |
3 ms |
8792 KB |
Output is correct |
93 |
Correct |
53 ms |
60500 KB |
Output is correct |
94 |
Correct |
72 ms |
60708 KB |
Output is correct |
95 |
Correct |
66 ms |
60500 KB |
Output is correct |
96 |
Correct |
68 ms |
60684 KB |
Output is correct |
97 |
Correct |
65 ms |
60692 KB |
Output is correct |
98 |
Correct |
61 ms |
60704 KB |
Output is correct |
99 |
Correct |
73 ms |
64568 KB |
Output is correct |
100 |
Correct |
75 ms |
64336 KB |
Output is correct |
101 |
Correct |
65 ms |
64336 KB |
Output is correct |
102 |
Correct |
70 ms |
64548 KB |
Output is correct |
103 |
Correct |
67 ms |
64332 KB |
Output is correct |
104 |
Correct |
73 ms |
64516 KB |
Output is correct |
105 |
Correct |
56 ms |
63312 KB |
Output is correct |