#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 5e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int n, m;
pii a[N];
namespace sub6 {
map <int, vector<pii>> qq;
set <pii> s;
int ans = mod, up[20][N];
void solve(){
forr (i, 1, n){
qq[a[i].nd].pb({1, i});
qq[a[i].st].pb({0, i});
}
for (pair<int, vector<pii>> ww : qq){
int u = ww.st;
vector <pii> v = ww.nd;
sort(all(v));
for (pii e : v){
if (e.st == 0){
s.insert(mp(a[e.nd].nd, e.nd));
} else {
s.erase(s.find(mp(a[e.nd].nd, e.nd)));
if (s.size()){
up[0][e.nd] = (*s.rbegin()).nd;
} else {
up[0][e.nd] = n + 1;
}
}
}
}
a[n + 1].nd = 2 * m + 1;
forr (i, 0, 17){
up[i][n + 1] = n + 1;
}
ford (i, n, 1){
if (up[0][i] < i) continue;
forr (j, 1, 17){
up[j][i] = up[j - 1][up[j - 1][i]];
}
int u = i, res = 2;
ford (j, 17, 0){
if (a[up[j][u]].nd < a[i].st + m){
res += mask(j);
u = up[j][u];
}
}
if (up[0][u] != n + 1 && a[up[0][u]].nd >= a[i].st + m){
minz(ans, res);
}
}
cout << (ans <= n ? ans : -1) << "\n";
}
}
namespace sub2 {
int f[500], ans = mod;
void solve(){
forr (i, 1, n){
memset (f, 63, sizeof f);
forr (j, 1, n){
if (a[j].st == a[i].st){
f[j] = 1;
} else {
ford (k, j - 1, 1){
if (a[j].st >= a[k].st && a[j].st <= a[k].nd){
minz(f[j], f[k] + 1);
}
}
}
if (a[j].nd >= a[i].st + m){
minz(ans, f[j]);
}
}
}
cout << (ans <= n ? ans : -1) << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef kaguya
freopen(file".inp", "r", stdin); freopen(file".ans", "w", stdout);
#endif
cin >> n >> m;
forr (i, 1, n){
cin >> a[i].st >> a[i].nd;
if (a[i].nd < a[i].st)
a[i].nd += m;
}
sort(a + 1, a + 1 + n);
if (n <= 300){
sub2::solve();
} else {
sub6::solve();
}
return 0;
}
/*
*/
Compilation message
Main.cpp: In function 'void sub6::solve()':
Main.cpp:55:8: warning: unused variable 'u' [-Wunused-variable]
55 | int u = ww.st;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2556 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
1 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
2388 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2388 KB |
Output is correct |
15 |
Correct |
1 ms |
2388 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
1 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2388 KB |
Output is correct |
20 |
Correct |
1 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2556 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
1 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
2388 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2388 KB |
Output is correct |
15 |
Correct |
1 ms |
2388 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
1 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2388 KB |
Output is correct |
20 |
Correct |
1 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
2388 KB |
Output is correct |
24 |
Correct |
1 ms |
2388 KB |
Output is correct |
25 |
Correct |
13 ms |
2388 KB |
Output is correct |
26 |
Correct |
14 ms |
2388 KB |
Output is correct |
27 |
Correct |
11 ms |
2388 KB |
Output is correct |
28 |
Correct |
11 ms |
2388 KB |
Output is correct |
29 |
Correct |
12 ms |
2388 KB |
Output is correct |
30 |
Correct |
13 ms |
2388 KB |
Output is correct |
31 |
Correct |
14 ms |
2524 KB |
Output is correct |
32 |
Correct |
12 ms |
2552 KB |
Output is correct |
33 |
Correct |
19 ms |
2524 KB |
Output is correct |
34 |
Correct |
10 ms |
2388 KB |
Output is correct |
35 |
Correct |
11 ms |
2552 KB |
Output is correct |
36 |
Correct |
12 ms |
2388 KB |
Output is correct |
37 |
Correct |
14 ms |
2580 KB |
Output is correct |
38 |
Correct |
13 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2556 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
1 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
2388 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2388 KB |
Output is correct |
15 |
Correct |
1 ms |
2388 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
1 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2388 KB |
Output is correct |
20 |
Correct |
1 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
2388 KB |
Output is correct |
24 |
Correct |
1 ms |
2388 KB |
Output is correct |
25 |
Correct |
13 ms |
2388 KB |
Output is correct |
26 |
Correct |
14 ms |
2388 KB |
Output is correct |
27 |
Correct |
11 ms |
2388 KB |
Output is correct |
28 |
Correct |
11 ms |
2388 KB |
Output is correct |
29 |
Correct |
12 ms |
2388 KB |
Output is correct |
30 |
Correct |
13 ms |
2388 KB |
Output is correct |
31 |
Correct |
14 ms |
2524 KB |
Output is correct |
32 |
Correct |
12 ms |
2552 KB |
Output is correct |
33 |
Correct |
19 ms |
2524 KB |
Output is correct |
34 |
Correct |
10 ms |
2388 KB |
Output is correct |
35 |
Correct |
11 ms |
2552 KB |
Output is correct |
36 |
Correct |
12 ms |
2388 KB |
Output is correct |
37 |
Correct |
14 ms |
2580 KB |
Output is correct |
38 |
Correct |
13 ms |
2552 KB |
Output is correct |
39 |
Correct |
10 ms |
40544 KB |
Output is correct |
40 |
Correct |
9 ms |
40508 KB |
Output is correct |
41 |
Correct |
9 ms |
40532 KB |
Output is correct |
42 |
Correct |
7 ms |
40020 KB |
Output is correct |
43 |
Correct |
9 ms |
40020 KB |
Output is correct |
44 |
Correct |
7 ms |
40020 KB |
Output is correct |
45 |
Correct |
7 ms |
39992 KB |
Output is correct |
46 |
Correct |
7 ms |
40020 KB |
Output is correct |
47 |
Correct |
7 ms |
40512 KB |
Output is correct |
48 |
Correct |
7 ms |
40532 KB |
Output is correct |
49 |
Correct |
7 ms |
40696 KB |
Output is correct |
50 |
Correct |
8 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
10 ms |
2388 KB |
Output is correct |
10 |
Correct |
12 ms |
2388 KB |
Output is correct |
11 |
Correct |
22 ms |
2388 KB |
Output is correct |
12 |
Correct |
15 ms |
2388 KB |
Output is correct |
13 |
Correct |
11 ms |
2388 KB |
Output is correct |
14 |
Correct |
11 ms |
2528 KB |
Output is correct |
15 |
Correct |
8 ms |
40696 KB |
Output is correct |
16 |
Correct |
7 ms |
40020 KB |
Output is correct |
17 |
Correct |
8 ms |
40020 KB |
Output is correct |
18 |
Correct |
7 ms |
40020 KB |
Output is correct |
19 |
Correct |
7 ms |
40532 KB |
Output is correct |
20 |
Correct |
91 ms |
46688 KB |
Output is correct |
21 |
Correct |
196 ms |
87256 KB |
Output is correct |
22 |
Correct |
192 ms |
87124 KB |
Output is correct |
23 |
Correct |
155 ms |
64316 KB |
Output is correct |
24 |
Correct |
174 ms |
87116 KB |
Output is correct |
25 |
Correct |
230 ms |
62812 KB |
Output is correct |
26 |
Correct |
134 ms |
65368 KB |
Output is correct |
27 |
Correct |
153 ms |
65356 KB |
Output is correct |
28 |
Correct |
164 ms |
86164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2552 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
11 ms |
2388 KB |
Output is correct |
9 |
Correct |
11 ms |
2388 KB |
Output is correct |
10 |
Correct |
12 ms |
2388 KB |
Output is correct |
11 |
Correct |
14 ms |
2520 KB |
Output is correct |
12 |
Correct |
9 ms |
40532 KB |
Output is correct |
13 |
Correct |
8 ms |
40020 KB |
Output is correct |
14 |
Correct |
8 ms |
40020 KB |
Output is correct |
15 |
Correct |
99 ms |
46708 KB |
Output is correct |
16 |
Correct |
157 ms |
64312 KB |
Output is correct |
17 |
Correct |
184 ms |
87284 KB |
Output is correct |
18 |
Correct |
204 ms |
88264 KB |
Output is correct |
19 |
Correct |
175 ms |
87240 KB |
Output is correct |
20 |
Correct |
154 ms |
64228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2556 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2388 KB |
Output is correct |
11 |
Correct |
1 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
2388 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2388 KB |
Output is correct |
15 |
Correct |
1 ms |
2388 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
1 ms |
2388 KB |
Output is correct |
18 |
Correct |
1 ms |
2516 KB |
Output is correct |
19 |
Correct |
1 ms |
2388 KB |
Output is correct |
20 |
Correct |
1 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
2388 KB |
Output is correct |
24 |
Correct |
1 ms |
2388 KB |
Output is correct |
25 |
Correct |
13 ms |
2388 KB |
Output is correct |
26 |
Correct |
14 ms |
2388 KB |
Output is correct |
27 |
Correct |
11 ms |
2388 KB |
Output is correct |
28 |
Correct |
11 ms |
2388 KB |
Output is correct |
29 |
Correct |
12 ms |
2388 KB |
Output is correct |
30 |
Correct |
13 ms |
2388 KB |
Output is correct |
31 |
Correct |
14 ms |
2524 KB |
Output is correct |
32 |
Correct |
12 ms |
2552 KB |
Output is correct |
33 |
Correct |
19 ms |
2524 KB |
Output is correct |
34 |
Correct |
10 ms |
2388 KB |
Output is correct |
35 |
Correct |
11 ms |
2552 KB |
Output is correct |
36 |
Correct |
12 ms |
2388 KB |
Output is correct |
37 |
Correct |
14 ms |
2580 KB |
Output is correct |
38 |
Correct |
13 ms |
2552 KB |
Output is correct |
39 |
Correct |
10 ms |
40544 KB |
Output is correct |
40 |
Correct |
9 ms |
40508 KB |
Output is correct |
41 |
Correct |
9 ms |
40532 KB |
Output is correct |
42 |
Correct |
7 ms |
40020 KB |
Output is correct |
43 |
Correct |
9 ms |
40020 KB |
Output is correct |
44 |
Correct |
7 ms |
40020 KB |
Output is correct |
45 |
Correct |
7 ms |
39992 KB |
Output is correct |
46 |
Correct |
7 ms |
40020 KB |
Output is correct |
47 |
Correct |
7 ms |
40512 KB |
Output is correct |
48 |
Correct |
7 ms |
40532 KB |
Output is correct |
49 |
Correct |
7 ms |
40696 KB |
Output is correct |
50 |
Correct |
8 ms |
40020 KB |
Output is correct |
51 |
Correct |
1 ms |
2388 KB |
Output is correct |
52 |
Correct |
1 ms |
2388 KB |
Output is correct |
53 |
Correct |
1 ms |
2388 KB |
Output is correct |
54 |
Correct |
1 ms |
2388 KB |
Output is correct |
55 |
Correct |
1 ms |
2388 KB |
Output is correct |
56 |
Correct |
1 ms |
2388 KB |
Output is correct |
57 |
Correct |
1 ms |
2388 KB |
Output is correct |
58 |
Correct |
1 ms |
2388 KB |
Output is correct |
59 |
Correct |
10 ms |
2388 KB |
Output is correct |
60 |
Correct |
12 ms |
2388 KB |
Output is correct |
61 |
Correct |
22 ms |
2388 KB |
Output is correct |
62 |
Correct |
15 ms |
2388 KB |
Output is correct |
63 |
Correct |
11 ms |
2388 KB |
Output is correct |
64 |
Correct |
11 ms |
2528 KB |
Output is correct |
65 |
Correct |
8 ms |
40696 KB |
Output is correct |
66 |
Correct |
7 ms |
40020 KB |
Output is correct |
67 |
Correct |
8 ms |
40020 KB |
Output is correct |
68 |
Correct |
7 ms |
40020 KB |
Output is correct |
69 |
Correct |
7 ms |
40532 KB |
Output is correct |
70 |
Correct |
91 ms |
46688 KB |
Output is correct |
71 |
Correct |
196 ms |
87256 KB |
Output is correct |
72 |
Correct |
192 ms |
87124 KB |
Output is correct |
73 |
Correct |
155 ms |
64316 KB |
Output is correct |
74 |
Correct |
174 ms |
87116 KB |
Output is correct |
75 |
Correct |
230 ms |
62812 KB |
Output is correct |
76 |
Correct |
134 ms |
65368 KB |
Output is correct |
77 |
Correct |
153 ms |
65356 KB |
Output is correct |
78 |
Correct |
164 ms |
86164 KB |
Output is correct |
79 |
Correct |
1 ms |
2388 KB |
Output is correct |
80 |
Correct |
1 ms |
2552 KB |
Output is correct |
81 |
Correct |
1 ms |
2388 KB |
Output is correct |
82 |
Correct |
1 ms |
2388 KB |
Output is correct |
83 |
Correct |
2 ms |
2388 KB |
Output is correct |
84 |
Correct |
1 ms |
2388 KB |
Output is correct |
85 |
Correct |
1 ms |
2388 KB |
Output is correct |
86 |
Correct |
11 ms |
2388 KB |
Output is correct |
87 |
Correct |
11 ms |
2388 KB |
Output is correct |
88 |
Correct |
12 ms |
2388 KB |
Output is correct |
89 |
Correct |
14 ms |
2520 KB |
Output is correct |
90 |
Correct |
9 ms |
40532 KB |
Output is correct |
91 |
Correct |
8 ms |
40020 KB |
Output is correct |
92 |
Correct |
8 ms |
40020 KB |
Output is correct |
93 |
Correct |
99 ms |
46708 KB |
Output is correct |
94 |
Correct |
157 ms |
64312 KB |
Output is correct |
95 |
Correct |
184 ms |
87284 KB |
Output is correct |
96 |
Correct |
204 ms |
88264 KB |
Output is correct |
97 |
Correct |
175 ms |
87240 KB |
Output is correct |
98 |
Correct |
154 ms |
64228 KB |
Output is correct |
99 |
Correct |
210 ms |
87116 KB |
Output is correct |
100 |
Correct |
216 ms |
87124 KB |
Output is correct |
101 |
Correct |
162 ms |
87116 KB |
Output is correct |
102 |
Correct |
172 ms |
87148 KB |
Output is correct |
103 |
Correct |
163 ms |
65352 KB |
Output is correct |
104 |
Correct |
154 ms |
84980 KB |
Output is correct |
105 |
Correct |
168 ms |
85836 KB |
Output is correct |