#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int mxN = 200'000;
int N, M;
pair<int,int> se[mxN + 1];
int nxt[mxN + 1][21];
vector<int> normal;
i64 dp[3 * mxN + 5];
int getNorm(int x) {
int lo = 0, hi = (int) normal.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (normal[mid] == x) return mid;
else if (normal[mid] < x) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}
void solve() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> se[i].first >> se[i].second;
if (se[i].second < se[i].first)
se[i].second += M;
normal.emplace_back(se[i].first);
normal.emplace_back(se[i].second);
}
sort(normal.begin(), normal.end());
normal.erase(unique(normal.begin(), normal.end()), normal.end());
sort(se, se + N);
priority_queue<pair<int,int>> pq;
int id = 0, r = 0;
for (int i = 0; i < (int) normal.size(); i++) {
for (; id < N && se[id].first == normal[i]; id++) {
pq.push({-se[id].second, id});
if (se[id].second > se[r].second) {
r = id;
}
}
if (!pq.empty()) {
int tmp = r;
for (; !pq.empty() && -pq.top().first == normal[i]; pq.pop()) {
nxt[pq.top().second][0] = tmp;
}
}
}
/*
cout << "Nxt: ";
for (int i = 0; i < N; i++) {
cout << nxt[i][0] << " " ;
}
cout << endl;
*/
for (int j = 1; j < 20; j++) {
for (int i = 0; i < N; i++) {
nxt[i][j] = nxt[nxt[i][j - 1]][j - 1];
}
}
int ans = N + 1;
for (int i = 0; i < N; i++) {
int now = i, cnt = 1;
for (int j = 19; j >= 0; j--) {
if (se[nxt[now][j]].second < se[i].first + M) {
now = nxt[now][j];
cnt += (1 << j);
}
}
now = nxt[now][0];
cnt++;
if (se[now].second >= se[i].first + M) {
ans = min(ans, cnt);
}
}
if (ans > N) ans = -1;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while (tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
0 ms |
468 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 |
476 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
0 ms |
468 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 |
476 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
472 KB |
Output is correct |
25 |
Correct |
0 ms |
472 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
476 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
0 ms |
468 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 |
476 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
472 KB |
Output is correct |
25 |
Correct |
0 ms |
472 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
476 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
1112 KB |
Output is correct |
40 |
Correct |
2 ms |
1052 KB |
Output is correct |
41 |
Correct |
3 ms |
1116 KB |
Output is correct |
42 |
Correct |
3 ms |
936 KB |
Output is correct |
43 |
Correct |
3 ms |
1068 KB |
Output is correct |
44 |
Correct |
3 ms |
1112 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
3 ms |
1116 KB |
Output is correct |
48 |
Correct |
2 ms |
1116 KB |
Output is correct |
49 |
Correct |
1 ms |
860 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
476 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
1116 KB |
Output is correct |
16 |
Correct |
2 ms |
1088 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
2 ms |
1112 KB |
Output is correct |
19 |
Correct |
2 ms |
980 KB |
Output is correct |
20 |
Correct |
64 ms |
22508 KB |
Output is correct |
21 |
Correct |
110 ms |
24008 KB |
Output is correct |
22 |
Correct |
114 ms |
23848 KB |
Output is correct |
23 |
Correct |
92 ms |
23028 KB |
Output is correct |
24 |
Correct |
102 ms |
23960 KB |
Output is correct |
25 |
Correct |
111 ms |
23900 KB |
Output is correct |
26 |
Correct |
91 ms |
23936 KB |
Output is correct |
27 |
Correct |
96 ms |
23968 KB |
Output is correct |
28 |
Correct |
98 ms |
23852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
476 KB |
Output is correct |
12 |
Correct |
3 ms |
1008 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
58 ms |
22328 KB |
Output is correct |
16 |
Correct |
85 ms |
22948 KB |
Output is correct |
17 |
Correct |
98 ms |
24008 KB |
Output is correct |
18 |
Correct |
102 ms |
24264 KB |
Output is correct |
19 |
Correct |
123 ms |
23748 KB |
Output is correct |
20 |
Correct |
110 ms |
22884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
472 KB |
Output is correct |
2 |
Correct |
0 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
476 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
0 ms |
468 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 |
476 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
472 KB |
Output is correct |
25 |
Correct |
0 ms |
472 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
476 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
1112 KB |
Output is correct |
40 |
Correct |
2 ms |
1052 KB |
Output is correct |
41 |
Correct |
3 ms |
1116 KB |
Output is correct |
42 |
Correct |
3 ms |
936 KB |
Output is correct |
43 |
Correct |
3 ms |
1068 KB |
Output is correct |
44 |
Correct |
3 ms |
1112 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
2 ms |
1116 KB |
Output is correct |
47 |
Correct |
3 ms |
1116 KB |
Output is correct |
48 |
Correct |
2 ms |
1116 KB |
Output is correct |
49 |
Correct |
1 ms |
860 KB |
Output is correct |
50 |
Correct |
3 ms |
860 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 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 |
476 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
468 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
1116 KB |
Output is correct |
66 |
Correct |
2 ms |
1088 KB |
Output is correct |
67 |
Correct |
2 ms |
1116 KB |
Output is correct |
68 |
Correct |
2 ms |
1112 KB |
Output is correct |
69 |
Correct |
2 ms |
980 KB |
Output is correct |
70 |
Correct |
64 ms |
22508 KB |
Output is correct |
71 |
Correct |
110 ms |
24008 KB |
Output is correct |
72 |
Correct |
114 ms |
23848 KB |
Output is correct |
73 |
Correct |
92 ms |
23028 KB |
Output is correct |
74 |
Correct |
102 ms |
23960 KB |
Output is correct |
75 |
Correct |
111 ms |
23900 KB |
Output is correct |
76 |
Correct |
91 ms |
23936 KB |
Output is correct |
77 |
Correct |
96 ms |
23968 KB |
Output is correct |
78 |
Correct |
98 ms |
23852 KB |
Output is correct |
79 |
Correct |
0 ms |
472 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
1 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 |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
344 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
1 ms |
476 KB |
Output is correct |
90 |
Correct |
3 ms |
1008 KB |
Output is correct |
91 |
Correct |
3 ms |
860 KB |
Output is correct |
92 |
Correct |
2 ms |
860 KB |
Output is correct |
93 |
Correct |
58 ms |
22328 KB |
Output is correct |
94 |
Correct |
85 ms |
22948 KB |
Output is correct |
95 |
Correct |
98 ms |
24008 KB |
Output is correct |
96 |
Correct |
102 ms |
24264 KB |
Output is correct |
97 |
Correct |
123 ms |
23748 KB |
Output is correct |
98 |
Correct |
110 ms |
22884 KB |
Output is correct |
99 |
Correct |
106 ms |
23768 KB |
Output is correct |
100 |
Correct |
107 ms |
23972 KB |
Output is correct |
101 |
Correct |
94 ms |
23768 KB |
Output is correct |
102 |
Correct |
103 ms |
23868 KB |
Output is correct |
103 |
Correct |
93 ms |
23928 KB |
Output is correct |
104 |
Correct |
103 ms |
23816 KB |
Output is correct |
105 |
Correct |
54 ms |
22776 KB |
Output is correct |