#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 100000000000000000
struct node {
int s, e, m, v, lz;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = (s+e)/2, v = 0, lz = -1;
if(s != e) {
l = new node(s, m);
r = new node(m+1, e);
}
}
void prop() {
if(lz == -1) return;
v = lz;
if(s != e) {
l->lz = lz;
r->lz = lz;
}
lz = -1;
}
void update(int x, int y, int val) {
prop();
if(x <= s && e <= y) {lz = val; return;}
else if(x > m) r->update(x, y, val);
else if(y <= m) l->update(x, y, val);
else l->update(x, y, val), r->update(x, y, val);
l->prop(); r->prop();
v = max(l->v, r->v);
}
int get(int x) {
prop();
if(s == e) return v;
else if(x > m) return r->get(x);
else return l->get(x);
}
} *root, *root2;
vector<int> adj[200000];
int p[200000][18], depth[200000];
void dfs(int x, int pa, int d) {
depth[x] = d;
p[x][0] = pa;
for(int i = 1; i < 18; i++) {
if(p[x][i-1] == -1) break;
p[x][i] = p[p[x][i-1]][i-1];
}
for(int i : adj[x]) dfs(i, x, d+1);
}
int getp(int x, int k) {
for(int i = 0; i < 18; i++) {
if(k&(1<<i)) x = p[x][i];
}
return x;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m, x, y, mn = INF, mx = 0;
cin >> n >> m;
pair<int, int> a[n];
vector<int> dis, dis2, act;
for(int i = 0; i < n; i++) {
cin >> x >> y;
if(y < x) y = y+m;
a[i] = {y, x};
dis.push_back(x);
dis.push_back(y);
dis2.push_back(2*x);
dis2.push_back(2*x-1);
dis2.push_back(2*x+1);
dis2.push_back(2*y);
dis2.push_back(2*y-1);
dis2.push_back(2*y+1);
mn = min(mn, x);
mx = max(mx, y);
}
if(mx-mn < m) {
cout << -1 << '\n';
return 0;
}
sort(dis.begin(), dis.end());
dis.erase(unique(dis.begin(), dis.end()), dis.end());
sort(dis2.begin(), dis2.end());
dis2.erase(unique(dis2.begin(), dis2.end()), dis2.end());
for(int i : dis) act.push_back(i);
root = new node(0, dis.size());
root2 = new node(0, dis2.size());
for(int i = 0; i < n; i++) {
x = lower_bound(dis2.begin(), dis2.end(), 2*a[i].second)-dis2.begin();
y = lower_bound(dis2.begin(), dis2.end(), 2*a[i].first)-dis2.begin();
root2->update(x, y, 1);
a[i].first = lower_bound(dis.begin(), dis.end(), a[i].first)-dis.begin();
a[i].second = lower_bound(dis.begin(), dis.end(), a[i].second)-dis.begin();
}
sort(a, a+n);
for(int i = 0; i < n; i++) {
root->update(a[i].second, a[i].first, a[i].first);
}
for(int i = 1; i < dis2.size()-1; i++) if(!root2->get(i)) {
cout << -1 << '\n';
return 0;
}
for(int i = 0; i < dis.size(); i++) {
if(root->get(i) != i) adj[root->get(i)].push_back(i);
}
memset(p, -1, sizeof(p));
dfs(dis.size()-1, -1, 0);
int l = 0, r = n, md;
while(l < r) {
md = (l+r)/2;
bool ok = false;
for(int i = 0; i < dis.size()-1; i++) {
if(act[getp(i, min(md, depth[i]))]-act[i] >= m) ok = true;
}
if(ok) r = md;
else l = md+1;
}
cout << l << '\n';
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:109:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 1; i < dis2.size()-1; i++) if(!root2->get(i)) {
| ~~^~~~~~~~~~~~~~~
Main.cpp:113:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int i = 0; i < dis.size(); i++) {
| ~~^~~~~~~~~~~~
Main.cpp:122:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int i = 0; i < dis.size()-1; i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
2 ms |
5968 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5968 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
2 ms |
5968 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5968 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
2 ms |
5968 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5968 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5968 KB |
Output is correct |
2 |
Correct |
6 ms |
34640 KB |
Output is correct |
3 |
Correct |
5 ms |
34640 KB |
Output is correct |
4 |
Correct |
7 ms |
34632 KB |
Output is correct |
5 |
Correct |
6 ms |
34640 KB |
Output is correct |
6 |
Correct |
2 ms |
6136 KB |
Output is correct |
7 |
Correct |
3 ms |
6136 KB |
Output is correct |
8 |
Correct |
6 ms |
34640 KB |
Output is correct |
9 |
Correct |
6 ms |
34896 KB |
Output is correct |
10 |
Correct |
8 ms |
34896 KB |
Output is correct |
11 |
Correct |
6 ms |
34896 KB |
Output is correct |
12 |
Correct |
6 ms |
34896 KB |
Output is correct |
13 |
Correct |
7 ms |
34896 KB |
Output is correct |
14 |
Correct |
6 ms |
34896 KB |
Output is correct |
15 |
Correct |
24 ms |
40380 KB |
Output is correct |
16 |
Correct |
20 ms |
37968 KB |
Output is correct |
17 |
Correct |
19 ms |
37200 KB |
Output is correct |
18 |
Correct |
19 ms |
37968 KB |
Output is correct |
19 |
Correct |
21 ms |
40272 KB |
Output is correct |
20 |
Correct |
79 ms |
55844 KB |
Output is correct |
21 |
Runtime error |
1191 ms |
466632 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5968 KB |
Output is correct |
2 |
Correct |
7 ms |
34640 KB |
Output is correct |
3 |
Correct |
6 ms |
34692 KB |
Output is correct |
4 |
Correct |
6 ms |
34640 KB |
Output is correct |
5 |
Correct |
2 ms |
5968 KB |
Output is correct |
6 |
Correct |
3 ms |
6136 KB |
Output is correct |
7 |
Correct |
2 ms |
5968 KB |
Output is correct |
8 |
Correct |
9 ms |
34896 KB |
Output is correct |
9 |
Correct |
6 ms |
34896 KB |
Output is correct |
10 |
Correct |
7 ms |
35068 KB |
Output is correct |
11 |
Correct |
7 ms |
34908 KB |
Output is correct |
12 |
Correct |
23 ms |
40424 KB |
Output is correct |
13 |
Correct |
19 ms |
37968 KB |
Output is correct |
14 |
Correct |
19 ms |
37968 KB |
Output is correct |
15 |
Correct |
84 ms |
55952 KB |
Output is correct |
16 |
Runtime error |
748 ms |
266888 KB |
Execution killed with signal 11 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
34640 KB |
Output is correct |
2 |
Correct |
2 ms |
5968 KB |
Output is correct |
3 |
Incorrect |
2 ms |
5968 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |