#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using namespace std;
const int N = 4e5 + 42, T = 18, INF = 1e9 + 42;
struct Inter {
int l, r;
};
struct Event {
int t, id, isDeb;
};
int bl[T][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<Inter> inter;
for(int i = 0; i < n; i++) {
int s, e; cin >> s >> e;
if(s < e) {
inter.push_back({s, e});
inter.push_back({s+m, e+m});
} else {
inter.push_back({s, e+m});
}
}
vector<Event> ev;
for(int i = 0; i < sz(inter); i++) {
ev.push_back({inter[i].l, i, 0});
ev.push_back({inter[i].r, i, 1});
}
sort(all(ev),
[](Event a, Event b) {
if(a.t == b.t) return a.isDeb < b.isDeb;
return a.t < b.t;
});
set<array<int, 2>> fins;
vector<bool> occ(sz(inter));
for(Event e : ev) {
if(occ[e.id]) {
occ[e.id] = 0;
auto it = fins.end();
it--;
bl[0][e.id] = (*it)[1];
fins.erase(array<int, 2>{inter[e.id].r, e.id});
} else {
occ[e.id] = 1;
fins.insert(array<int, 2>{inter[e.id].r, e.id});
}
}
for(int i = 1; i < T; i++)
for(int j = 0; j < sz(inter); j++)
bl[i][j] = bl[i-1][bl[i-1][j]];
int mini = INF;
for(int i = 0; i < sz(inter); i++) if(inter[i].l <= m) {
int id = i, ans = 2;
for(int j = T-1; j >= 0; j--)
if((j == 0 || bl[j][id] != bl[j-1][id]) && inter[bl[j][id]].r < inter[i].l + m) {
ans += (1 << j);
id = bl[j][id];
}
if(inter[bl[0][id]].r >= inter[i].l + m)
mini = min(mini, ans);
}
cout << (mini == INF ? -1 : mini) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
668 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
2 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
616 KB |
Output is correct |
36 |
Correct |
1 ms |
616 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
668 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
2 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
616 KB |
Output is correct |
36 |
Correct |
1 ms |
616 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
612 KB |
Output is correct |
39 |
Correct |
5 ms |
1692 KB |
Output is correct |
40 |
Correct |
4 ms |
1776 KB |
Output is correct |
41 |
Correct |
4 ms |
1688 KB |
Output is correct |
42 |
Correct |
5 ms |
1684 KB |
Output is correct |
43 |
Correct |
5 ms |
1684 KB |
Output is correct |
44 |
Correct |
5 ms |
1936 KB |
Output is correct |
45 |
Correct |
6 ms |
1756 KB |
Output is correct |
46 |
Correct |
5 ms |
1684 KB |
Output is correct |
47 |
Correct |
6 ms |
1688 KB |
Output is correct |
48 |
Correct |
4 ms |
1688 KB |
Output is correct |
49 |
Correct |
3 ms |
1688 KB |
Output is correct |
50 |
Correct |
4 ms |
1668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 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 |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
1688 KB |
Output is correct |
16 |
Correct |
4 ms |
1848 KB |
Output is correct |
17 |
Correct |
5 ms |
1684 KB |
Output is correct |
18 |
Correct |
6 ms |
1936 KB |
Output is correct |
19 |
Correct |
4 ms |
1688 KB |
Output is correct |
20 |
Correct |
165 ms |
41124 KB |
Output is correct |
21 |
Correct |
212 ms |
48040 KB |
Output is correct |
22 |
Correct |
203 ms |
48104 KB |
Output is correct |
23 |
Correct |
294 ms |
46956 KB |
Output is correct |
24 |
Correct |
255 ms |
47956 KB |
Output is correct |
25 |
Correct |
266 ms |
43952 KB |
Output is correct |
26 |
Correct |
225 ms |
48036 KB |
Output is correct |
27 |
Correct |
219 ms |
48092 KB |
Output is correct |
28 |
Correct |
178 ms |
48100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1940 KB |
Output is correct |
13 |
Correct |
3 ms |
1688 KB |
Output is correct |
14 |
Correct |
4 ms |
1684 KB |
Output is correct |
15 |
Correct |
160 ms |
41112 KB |
Output is correct |
16 |
Correct |
217 ms |
47004 KB |
Output is correct |
17 |
Correct |
210 ms |
48040 KB |
Output is correct |
18 |
Correct |
227 ms |
45972 KB |
Output is correct |
19 |
Correct |
234 ms |
47856 KB |
Output is correct |
20 |
Correct |
246 ms |
47144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
668 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
608 KB |
Output is correct |
34 |
Correct |
2 ms |
608 KB |
Output is correct |
35 |
Correct |
1 ms |
616 KB |
Output is correct |
36 |
Correct |
1 ms |
616 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
612 KB |
Output is correct |
39 |
Correct |
5 ms |
1692 KB |
Output is correct |
40 |
Correct |
4 ms |
1776 KB |
Output is correct |
41 |
Correct |
4 ms |
1688 KB |
Output is correct |
42 |
Correct |
5 ms |
1684 KB |
Output is correct |
43 |
Correct |
5 ms |
1684 KB |
Output is correct |
44 |
Correct |
5 ms |
1936 KB |
Output is correct |
45 |
Correct |
6 ms |
1756 KB |
Output is correct |
46 |
Correct |
5 ms |
1684 KB |
Output is correct |
47 |
Correct |
6 ms |
1688 KB |
Output is correct |
48 |
Correct |
4 ms |
1688 KB |
Output is correct |
49 |
Correct |
3 ms |
1688 KB |
Output is correct |
50 |
Correct |
4 ms |
1668 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
600 KB |
Output is correct |
58 |
Correct |
0 ms |
464 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
600 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
600 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
6 ms |
1688 KB |
Output is correct |
66 |
Correct |
4 ms |
1848 KB |
Output is correct |
67 |
Correct |
5 ms |
1684 KB |
Output is correct |
68 |
Correct |
6 ms |
1936 KB |
Output is correct |
69 |
Correct |
4 ms |
1688 KB |
Output is correct |
70 |
Correct |
165 ms |
41124 KB |
Output is correct |
71 |
Correct |
212 ms |
48040 KB |
Output is correct |
72 |
Correct |
203 ms |
48104 KB |
Output is correct |
73 |
Correct |
294 ms |
46956 KB |
Output is correct |
74 |
Correct |
255 ms |
47956 KB |
Output is correct |
75 |
Correct |
266 ms |
43952 KB |
Output is correct |
76 |
Correct |
225 ms |
48036 KB |
Output is correct |
77 |
Correct |
219 ms |
48092 KB |
Output is correct |
78 |
Correct |
178 ms |
48100 KB |
Output is correct |
79 |
Correct |
1 ms |
464 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
1 ms |
604 KB |
Output is correct |
87 |
Correct |
1 ms |
604 KB |
Output is correct |
88 |
Correct |
1 ms |
604 KB |
Output is correct |
89 |
Correct |
1 ms |
604 KB |
Output is correct |
90 |
Correct |
5 ms |
1940 KB |
Output is correct |
91 |
Correct |
3 ms |
1688 KB |
Output is correct |
92 |
Correct |
4 ms |
1684 KB |
Output is correct |
93 |
Correct |
160 ms |
41112 KB |
Output is correct |
94 |
Correct |
217 ms |
47004 KB |
Output is correct |
95 |
Correct |
210 ms |
48040 KB |
Output is correct |
96 |
Correct |
227 ms |
45972 KB |
Output is correct |
97 |
Correct |
234 ms |
47856 KB |
Output is correct |
98 |
Correct |
246 ms |
47144 KB |
Output is correct |
99 |
Correct |
184 ms |
47868 KB |
Output is correct |
100 |
Correct |
260 ms |
48040 KB |
Output is correct |
101 |
Correct |
245 ms |
48076 KB |
Output is correct |
102 |
Correct |
214 ms |
48108 KB |
Output is correct |
103 |
Correct |
246 ms |
47860 KB |
Output is correct |
104 |
Correct |
195 ms |
48036 KB |
Output is correct |
105 |
Correct |
109 ms |
46708 KB |
Output is correct |