#include <bits/stdc++.h>
#define int long long
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int LOG = 20;
const int maxn = 2e5 + 5;
int up[maxn][LOG], depth[maxn];
vector<int> graph[maxn+5];
void dfs(int u, int p) {
for(int i=1; i<LOG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(int &v : graph[u]) {
if(v == p) continue;
depth[v] = depth[u] + 1;
up[v][0] = u;
dfs(v, u);
}
}
struct SegTree {
int n;
vector<pii> tree, v;
SegTree(vector<pii> _v) {
v = _v;
n = _v.size();
tree.resize(4*n+5);
build(1, 0, n-1);
}
pii merge(pii a, pii b) {
if(a.first >= b.first) return a;
return b;
}
void build(int u, int tl, int tr) {
if(tl == tr) {
tree[u] = {v[tl].second, tl};
} else {
int tm = (tl + tr) / 2;
build(2*u, tl, tm);
build(2*u+1, tm+1, tr);
tree[u] = merge(tree[2*u], tree[2*u+1]);
}
}
pii query(int u, int tl, int tr, int l, int r) {
if(tl > tr || l > tr || tl > r) return pii{0, 0};
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return merge(
query(2*u, tl, tm, l, r),
query(2*u+1, tm+1, tr, l, r)
);
}
pii query(int l, int r) { return query(1, 0, n-1, l, r); }
};
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, m, ans=1e9;
cin >> n >> m;
vector<pii> v(n);
for(pii &x : v) cin >> x.first >> x.second;
map<ll, ll> mp;
for(int i=0; i<n; i++) {
if(v[i].first > v[i].second) v[i].second = m + v[i].second;
if(!mp.count(v[i].first)) mp[v[i].first] = v[i].second;
else mp[v[i].first] = max(mp[v[i].first], v[i].second);
}
v.clear();
for(auto &[a, b] : mp) {
//cout << a << ' ' << b << '\n';
v.push_back({ a, b });
}
sort(v.begin(), v.end());
n = v.size();
vector<int> out(n);
SegTree tree(v);
for(int i=0; i<n-1; i++) {
int mx=0, id=-1;
// for(int j=0; j<n; j++) {
// if(i == j) continue;
// if(v[j].first >= v[i].first && v[j].first <= v[i].second && v[j].second > mx && v[j].second > v[i].second) {
// mx = v[j].second;
// id = j;
// }
// }
if(v[i+1].first > v[i].second) continue;
int L = i+1, R = i+1;
int l=i+1, r=n-1, pos=i+1;
while(l <= r) {
int mid = (l + r) / 2;
if(v[mid].first >= v[i].first && v[mid].first <= v[i].second) R = mid, l = mid + 1;
else r = mid - 1;
}
pii res = tree.query(L, R);
id = res.second;
if(id != -1 && v[id].second > v[i].second) {
up[i][0] = id;
out[i]++;
//cout << i << " -> " << id << '\n';
graph[i].push_back(id);
graph[id].push_back(i);
}
}
for(int i=0; i<n; i++) {
if(out[i] == 0) {
up[i][0] = i;
dfs(i, i);
}
}
for(int i=0; i<n; i++) {
int l=0, r=depth[i], J=n;
while(l <= r) {
int mid = (l + r) / 2;
int u = i;
for(int j=LOG-1; j>=0; j--)
if(mid & (1 << j)) u = up[u][j];
if(v[u].second >= m + v[i].first) {
J = mid;
r = mid - 1;
} else l = mid + 1;
}
if(J != n) ans = min(ans, J + 1);
}
cout << (ans == 1e9 ? -1 : ans) << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:101:13: warning: unused variable 'mx' [-Wunused-variable]
101 | int mx=0, id=-1;
| ^~
Main.cpp:113:27: warning: unused variable 'pos' [-Wunused-variable]
113 | int l=i+1, r=n-1, pos=i+1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4960 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5160 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4960 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5160 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
5188 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5280 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5160 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4960 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5160 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
5188 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5280 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5160 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
6 ms |
7000 KB |
Output is correct |
40 |
Correct |
7 ms |
7260 KB |
Output is correct |
41 |
Correct |
7 ms |
6884 KB |
Output is correct |
42 |
Correct |
7 ms |
7260 KB |
Output is correct |
43 |
Correct |
5 ms |
6492 KB |
Output is correct |
44 |
Correct |
8 ms |
7392 KB |
Output is correct |
45 |
Correct |
10 ms |
7260 KB |
Output is correct |
46 |
Correct |
9 ms |
7260 KB |
Output is correct |
47 |
Correct |
5 ms |
7004 KB |
Output is correct |
48 |
Correct |
5 ms |
7004 KB |
Output is correct |
49 |
Correct |
5 ms |
7004 KB |
Output is correct |
50 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5160 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
6 ms |
7004 KB |
Output is correct |
16 |
Correct |
7 ms |
7260 KB |
Output is correct |
17 |
Correct |
5 ms |
6488 KB |
Output is correct |
18 |
Correct |
8 ms |
7260 KB |
Output is correct |
19 |
Correct |
6 ms |
7004 KB |
Output is correct |
20 |
Correct |
19 ms |
8928 KB |
Output is correct |
21 |
Correct |
265 ms |
83096 KB |
Output is correct |
22 |
Correct |
246 ms |
82836 KB |
Output is correct |
23 |
Correct |
344 ms |
95932 KB |
Output is correct |
24 |
Correct |
307 ms |
89108 KB |
Output is correct |
25 |
Correct |
200 ms |
66696 KB |
Output is correct |
26 |
Correct |
278 ms |
87032 KB |
Output is correct |
27 |
Correct |
366 ms |
96536 KB |
Output is correct |
28 |
Correct |
191 ms |
78612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
6 ms |
6876 KB |
Output is correct |
13 |
Correct |
7 ms |
7260 KB |
Output is correct |
14 |
Correct |
6 ms |
6844 KB |
Output is correct |
15 |
Correct |
20 ms |
9052 KB |
Output is correct |
16 |
Correct |
337 ms |
95496 KB |
Output is correct |
17 |
Correct |
227 ms |
82192 KB |
Output is correct |
18 |
Correct |
228 ms |
82172 KB |
Output is correct |
19 |
Correct |
300 ms |
91928 KB |
Output is correct |
20 |
Correct |
296 ms |
82196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4960 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
5160 KB |
Output is correct |
11 |
Correct |
3 ms |
4952 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4952 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
4952 KB |
Output is correct |
22 |
Correct |
2 ms |
4956 KB |
Output is correct |
23 |
Correct |
2 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
5188 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
2 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
2 ms |
5208 KB |
Output is correct |
30 |
Correct |
3 ms |
5212 KB |
Output is correct |
31 |
Correct |
2 ms |
5212 KB |
Output is correct |
32 |
Correct |
2 ms |
5280 KB |
Output is correct |
33 |
Correct |
3 ms |
5212 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
2 ms |
5212 KB |
Output is correct |
37 |
Correct |
2 ms |
5160 KB |
Output is correct |
38 |
Correct |
2 ms |
5212 KB |
Output is correct |
39 |
Correct |
6 ms |
7000 KB |
Output is correct |
40 |
Correct |
7 ms |
7260 KB |
Output is correct |
41 |
Correct |
7 ms |
6884 KB |
Output is correct |
42 |
Correct |
7 ms |
7260 KB |
Output is correct |
43 |
Correct |
5 ms |
6492 KB |
Output is correct |
44 |
Correct |
8 ms |
7392 KB |
Output is correct |
45 |
Correct |
10 ms |
7260 KB |
Output is correct |
46 |
Correct |
9 ms |
7260 KB |
Output is correct |
47 |
Correct |
5 ms |
7004 KB |
Output is correct |
48 |
Correct |
5 ms |
7004 KB |
Output is correct |
49 |
Correct |
5 ms |
7004 KB |
Output is correct |
50 |
Correct |
6 ms |
7004 KB |
Output is correct |
51 |
Correct |
2 ms |
4956 KB |
Output is correct |
52 |
Correct |
2 ms |
4956 KB |
Output is correct |
53 |
Correct |
2 ms |
5160 KB |
Output is correct |
54 |
Correct |
2 ms |
4956 KB |
Output is correct |
55 |
Correct |
2 ms |
4956 KB |
Output is correct |
56 |
Correct |
2 ms |
4956 KB |
Output is correct |
57 |
Correct |
2 ms |
4956 KB |
Output is correct |
58 |
Correct |
2 ms |
4956 KB |
Output is correct |
59 |
Correct |
3 ms |
5208 KB |
Output is correct |
60 |
Correct |
2 ms |
5168 KB |
Output is correct |
61 |
Correct |
2 ms |
5208 KB |
Output is correct |
62 |
Correct |
2 ms |
5212 KB |
Output is correct |
63 |
Correct |
2 ms |
5212 KB |
Output is correct |
64 |
Correct |
2 ms |
5212 KB |
Output is correct |
65 |
Correct |
6 ms |
7004 KB |
Output is correct |
66 |
Correct |
7 ms |
7260 KB |
Output is correct |
67 |
Correct |
5 ms |
6488 KB |
Output is correct |
68 |
Correct |
8 ms |
7260 KB |
Output is correct |
69 |
Correct |
6 ms |
7004 KB |
Output is correct |
70 |
Correct |
19 ms |
8928 KB |
Output is correct |
71 |
Correct |
265 ms |
83096 KB |
Output is correct |
72 |
Correct |
246 ms |
82836 KB |
Output is correct |
73 |
Correct |
344 ms |
95932 KB |
Output is correct |
74 |
Correct |
307 ms |
89108 KB |
Output is correct |
75 |
Correct |
200 ms |
66696 KB |
Output is correct |
76 |
Correct |
278 ms |
87032 KB |
Output is correct |
77 |
Correct |
366 ms |
96536 KB |
Output is correct |
78 |
Correct |
191 ms |
78612 KB |
Output is correct |
79 |
Correct |
2 ms |
4956 KB |
Output is correct |
80 |
Correct |
2 ms |
4956 KB |
Output is correct |
81 |
Correct |
2 ms |
4956 KB |
Output is correct |
82 |
Correct |
3 ms |
4956 KB |
Output is correct |
83 |
Correct |
2 ms |
4956 KB |
Output is correct |
84 |
Correct |
2 ms |
4956 KB |
Output is correct |
85 |
Correct |
2 ms |
4956 KB |
Output is correct |
86 |
Correct |
2 ms |
5212 KB |
Output is correct |
87 |
Correct |
2 ms |
5212 KB |
Output is correct |
88 |
Correct |
2 ms |
5212 KB |
Output is correct |
89 |
Correct |
2 ms |
5212 KB |
Output is correct |
90 |
Correct |
6 ms |
6876 KB |
Output is correct |
91 |
Correct |
7 ms |
7260 KB |
Output is correct |
92 |
Correct |
6 ms |
6844 KB |
Output is correct |
93 |
Correct |
20 ms |
9052 KB |
Output is correct |
94 |
Correct |
337 ms |
95496 KB |
Output is correct |
95 |
Correct |
227 ms |
82192 KB |
Output is correct |
96 |
Correct |
228 ms |
82172 KB |
Output is correct |
97 |
Correct |
300 ms |
91928 KB |
Output is correct |
98 |
Correct |
296 ms |
82196 KB |
Output is correct |
99 |
Correct |
226 ms |
82468 KB |
Output is correct |
100 |
Correct |
263 ms |
83236 KB |
Output is correct |
101 |
Correct |
340 ms |
95004 KB |
Output is correct |
102 |
Correct |
316 ms |
91572 KB |
Output is correct |
103 |
Correct |
341 ms |
96536 KB |
Output is correct |
104 |
Correct |
191 ms |
79384 KB |
Output is correct |
105 |
Correct |
201 ms |
84240 KB |
Output is correct |