#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a; --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} else return false;
}
template<class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
const int N = 2e5 + 5;
const int inf = 1e9;
const int K = 18;
int n, m;
pii a[N];
int nxt[N][K];
vector<pii> opt;
int calc(int pos, int r) {
if(opt[pos].se >= r) return 1;
int ans = 1;
FORD(i, K - 1, 0) if(pos + MASK(i) < n) {
if(opt[nxt[pos][i]].se < r) {
ans += MASK(i);
pos = nxt[pos][i];
}
}
REP(i, 0, K) if(pos + MASK(i) < n) {
if(opt[nxt[pos][i]].se >= r) {
ans += MASK(i);
return ans;
}
}
return inf;
}
void solve() {
cin >> n >> m;
FOR(i, 1, n) {
cin >> a[i].fi >> a[i].se;
if(a[i].fi > a[i].se) a[i].se += m;
}
sort(a + 1, a + n + 1, [&](pii &x, pii &y) {
if(x.fi != y.fi) return x.fi < y.fi;
return x.se > y.se;
});
for(int i = 1, mx = 0; i <= n; ++i) {
if(a[i].se > mx) opt.push_back(a[i]);
maximize(mx, a[i].se);
}
n = opt.size();
for(int i = 0, j = 0; i < n; ++i) {
while(j < n && opt[j].fi <= opt[i].se) ++j;
nxt[i][0] = j - 1;
}
REP(j, 1, K) REP(i, 0, n) nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
int ans = inf;
REP(i, 0, n) {
minimize(ans, calc(i, opt[i].fi + m));
}
cout << (ans == inf ? -1 : ans) << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2512 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2512 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2512 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
4956 KB |
Output is correct |
41 |
Correct |
2 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
4696 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
2 ms |
2880 KB |
Output is correct |
45 |
Correct |
3 ms |
4828 KB |
Output is correct |
46 |
Correct |
2 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2396 KB |
Output is correct |
48 |
Correct |
2 ms |
2652 KB |
Output is correct |
49 |
Correct |
1 ms |
2784 KB |
Output is correct |
50 |
Correct |
2 ms |
4876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
18 ms |
4188 KB |
Output is correct |
21 |
Correct |
45 ms |
9900 KB |
Output is correct |
22 |
Correct |
44 ms |
8920 KB |
Output is correct |
23 |
Correct |
62 ms |
20684 KB |
Output is correct |
24 |
Correct |
56 ms |
19656 KB |
Output is correct |
25 |
Correct |
39 ms |
6740 KB |
Output is correct |
26 |
Correct |
52 ms |
15308 KB |
Output is correct |
27 |
Correct |
64 ms |
21448 KB |
Output is correct |
28 |
Correct |
43 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2516 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
4844 KB |
Output is correct |
13 |
Correct |
2 ms |
2840 KB |
Output is correct |
14 |
Correct |
2 ms |
3040 KB |
Output is correct |
15 |
Correct |
18 ms |
4188 KB |
Output is correct |
16 |
Correct |
60 ms |
20680 KB |
Output is correct |
17 |
Correct |
60 ms |
21452 KB |
Output is correct |
18 |
Correct |
59 ms |
21452 KB |
Output is correct |
19 |
Correct |
69 ms |
21452 KB |
Output is correct |
20 |
Correct |
54 ms |
20680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2512 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
0 ms |
2396 KB |
Output is correct |
39 |
Correct |
2 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
4956 KB |
Output is correct |
41 |
Correct |
2 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
4696 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
2 ms |
2880 KB |
Output is correct |
45 |
Correct |
3 ms |
4828 KB |
Output is correct |
46 |
Correct |
2 ms |
2908 KB |
Output is correct |
47 |
Correct |
2 ms |
2396 KB |
Output is correct |
48 |
Correct |
2 ms |
2652 KB |
Output is correct |
49 |
Correct |
1 ms |
2784 KB |
Output is correct |
50 |
Correct |
2 ms |
4876 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
0 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
0 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2516 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
0 ms |
2396 KB |
Output is correct |
62 |
Correct |
0 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
0 ms |
2396 KB |
Output is correct |
65 |
Correct |
2 ms |
2908 KB |
Output is correct |
66 |
Correct |
2 ms |
2908 KB |
Output is correct |
67 |
Correct |
1 ms |
2396 KB |
Output is correct |
68 |
Correct |
2 ms |
2908 KB |
Output is correct |
69 |
Correct |
1 ms |
2396 KB |
Output is correct |
70 |
Correct |
18 ms |
4188 KB |
Output is correct |
71 |
Correct |
45 ms |
9900 KB |
Output is correct |
72 |
Correct |
44 ms |
8920 KB |
Output is correct |
73 |
Correct |
62 ms |
20684 KB |
Output is correct |
74 |
Correct |
56 ms |
19656 KB |
Output is correct |
75 |
Correct |
39 ms |
6740 KB |
Output is correct |
76 |
Correct |
52 ms |
15308 KB |
Output is correct |
77 |
Correct |
64 ms |
21448 KB |
Output is correct |
78 |
Correct |
43 ms |
8784 KB |
Output is correct |
79 |
Correct |
1 ms |
2396 KB |
Output is correct |
80 |
Correct |
0 ms |
2396 KB |
Output is correct |
81 |
Correct |
1 ms |
2396 KB |
Output is correct |
82 |
Correct |
1 ms |
2396 KB |
Output is correct |
83 |
Correct |
0 ms |
2396 KB |
Output is correct |
84 |
Correct |
0 ms |
2396 KB |
Output is correct |
85 |
Correct |
0 ms |
2516 KB |
Output is correct |
86 |
Correct |
1 ms |
2396 KB |
Output is correct |
87 |
Correct |
1 ms |
2652 KB |
Output is correct |
88 |
Correct |
1 ms |
2396 KB |
Output is correct |
89 |
Correct |
0 ms |
2396 KB |
Output is correct |
90 |
Correct |
2 ms |
4844 KB |
Output is correct |
91 |
Correct |
2 ms |
2840 KB |
Output is correct |
92 |
Correct |
2 ms |
3040 KB |
Output is correct |
93 |
Correct |
18 ms |
4188 KB |
Output is correct |
94 |
Correct |
60 ms |
20680 KB |
Output is correct |
95 |
Correct |
60 ms |
21452 KB |
Output is correct |
96 |
Correct |
59 ms |
21452 KB |
Output is correct |
97 |
Correct |
69 ms |
21452 KB |
Output is correct |
98 |
Correct |
54 ms |
20680 KB |
Output is correct |
99 |
Correct |
47 ms |
11976 KB |
Output is correct |
100 |
Correct |
42 ms |
9920 KB |
Output is correct |
101 |
Correct |
63 ms |
21560 KB |
Output is correct |
102 |
Correct |
56 ms |
20168 KB |
Output is correct |
103 |
Correct |
61 ms |
21684 KB |
Output is correct |
104 |
Correct |
45 ms |
9584 KB |
Output is correct |
105 |
Correct |
45 ms |
14008 KB |
Output is correct |