#include<bits/stdc++.h>
#include <chrono>
#include <random>
using namespace std;
#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;
const ll mxN=2e5+5;
const ll LOG=21;
const ll inf=1e18;
ll pw[LOG];
struct seg{
ll s, e, id;
seg *up[LOG];
bool operator< (const seg &tar) const{
if(s!=tar.s){
return s<tar.s;
}
return e<tar.e;
}
ll bs(ll tar){
ll re=0;
if(e>=tar){
return 1;
}
seg *cur=this;
for(ll i=LOG-1;i>=0;i--){
if(cur->up[i]->e<tar){
re+=pw[i];
cur=cur->up[i];
}
}
return re+2;
}
};
ll n, m;
vector<seg> a;
vll con;
ll getidx(ll tar){
return lower_bound(con.begin(), con.end(), tar)-con.begin();
}
void solve(){
cin>>n>>m;
//a.resize(n);
vector<pll> tep(n);
for(ll i=0;i<n;i++){
cin>>tep[i].F>>tep[i].S;
}
for(ll i=0;i<n;i++){
if(tep[i].F>tep[i].S){
tep[i].S+=m;
}
}
sort(tep.begin(), tep.end());
//a.erase(unique(a.begin(), a.end()), a.end());
pll pre={0, 0};
ll cnt=0;
for(ll i=0;i<n;i++){
if(tep[i].F!=pre.F || tep[i].S!=pre.S){
a.pb({tep[i].F, tep[i].S, cnt});
cnt++;
}
pre.F=tep[i].F;
pre.S=tep[i].S;
}
n=a.size();
for(ll i=0;i<n;i++){
seg tep={a[i].s+m, a[i].e+m, cnt};
cnt++;
tep.e=min(tep.e, 2*m);
a.pb(tep);
}
for(auto &cur:a){
con.pb(cur.s);
con.pb(cur.e);
}
sort(con.begin(), con.end());
con.erase(unique(con.begin(), con.end()), con.end());
ll pt=0;
seg *mx=new seg();
mx->s=0;
mx->e=0;
vector<seg*> bst(con.size());
for(ll i=0;i<con.size();i++){
ll it=con[i];
if(it<a[0].s){
assert(0);
continue;
}
while(pt<a.size() && a[pt].s<=it){
if(a[pt].e>(mx->e)){
mx=&a[pt];
}
pt++;
}
assert(mx->e!=0);
bst[i]=mx;
}
for(ll i=0;i<a.size();i++){
ll id=getidx(a[i].e);
a[i].up[0]=bst[id];
//cout<<i<<' '<<0<<' '<<a[i].up[0]->id<<'\n';
}
/*for(ll i=0;i<a.size();i++){
cout<<a[i].s<<' '<<a[i].e<<'\n';
}*/
for(ll j=1;j<LOG;j++){
for(ll i=0;i<a.size();i++){
a[i].up[j]=a[i].up[j-1]->up[j-1];
//cout<<i<<' '<<j<<' '<<a[i].up[j]->id<<'\n';
}
}
bool good=0;
for(ll i=0;i<n;i++){
if(a[i].up[LOG-1]->e>=a[i].s+m){
good=1;
}
}
if(!good){
cout<<-1<<'\n';
return;
}
ll ans=inf;
for(ll i=0;i<n;i++){
ll re=a[i].bs(a[i].s+m);
//cout<<a[i].s<<' '<<a[i].e<<' '<<re<<'\n';
ans=min(ans, re);
}
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
pw[0]=1;
for(ll i=1;i<LOG;i++){
pw[i]=pw[i-1]*2;
}
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:96:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(ll i=0;i<con.size();i++){
| ~^~~~~~~~~~~
Main.cpp:102:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while(pt<a.size() && a[pt].s<=it){
| ~~^~~~~~~~~
Main.cpp:111:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(ll i=0;i<a.size();i++){
| ~^~~~~~~~~
Main.cpp:120:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<seg>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(ll i=0;i<a.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
5 ms |
3792 KB |
Output is correct |
40 |
Correct |
4 ms |
3792 KB |
Output is correct |
41 |
Correct |
4 ms |
4816 KB |
Output is correct |
42 |
Correct |
4 ms |
3656 KB |
Output is correct |
43 |
Correct |
8 ms |
3792 KB |
Output is correct |
44 |
Correct |
5 ms |
5072 KB |
Output is correct |
45 |
Correct |
4 ms |
4816 KB |
Output is correct |
46 |
Correct |
4 ms |
3792 KB |
Output is correct |
47 |
Correct |
4 ms |
4812 KB |
Output is correct |
48 |
Correct |
4 ms |
4556 KB |
Output is correct |
49 |
Correct |
4 ms |
3656 KB |
Output is correct |
50 |
Correct |
4 ms |
3792 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 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
4 ms |
3532 KB |
Output is correct |
16 |
Correct |
4 ms |
3536 KB |
Output is correct |
17 |
Correct |
5 ms |
3536 KB |
Output is correct |
18 |
Correct |
4 ms |
4816 KB |
Output is correct |
19 |
Correct |
4 ms |
4816 KB |
Output is correct |
20 |
Correct |
18 ms |
3420 KB |
Output is correct |
21 |
Correct |
193 ms |
102020 KB |
Output is correct |
22 |
Correct |
191 ms |
102044 KB |
Output is correct |
23 |
Correct |
165 ms |
102104 KB |
Output is correct |
24 |
Correct |
207 ms |
102032 KB |
Output is correct |
25 |
Correct |
194 ms |
102056 KB |
Output is correct |
26 |
Correct |
174 ms |
102056 KB |
Output is correct |
27 |
Correct |
207 ms |
102032 KB |
Output is correct |
28 |
Correct |
181 ms |
102028 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 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 |
5072 KB |
Output is correct |
13 |
Correct |
4 ms |
5072 KB |
Output is correct |
14 |
Correct |
4 ms |
4816 KB |
Output is correct |
15 |
Correct |
16 ms |
3420 KB |
Output is correct |
16 |
Correct |
170 ms |
102228 KB |
Output is correct |
17 |
Correct |
176 ms |
102052 KB |
Output is correct |
18 |
Correct |
190 ms |
102048 KB |
Output is correct |
19 |
Correct |
189 ms |
102028 KB |
Output is correct |
20 |
Correct |
182 ms |
102052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
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 |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
860 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
5 ms |
3792 KB |
Output is correct |
40 |
Correct |
4 ms |
3792 KB |
Output is correct |
41 |
Correct |
4 ms |
4816 KB |
Output is correct |
42 |
Correct |
4 ms |
3656 KB |
Output is correct |
43 |
Correct |
8 ms |
3792 KB |
Output is correct |
44 |
Correct |
5 ms |
5072 KB |
Output is correct |
45 |
Correct |
4 ms |
4816 KB |
Output is correct |
46 |
Correct |
4 ms |
3792 KB |
Output is correct |
47 |
Correct |
4 ms |
4812 KB |
Output is correct |
48 |
Correct |
4 ms |
4556 KB |
Output is correct |
49 |
Correct |
4 ms |
3656 KB |
Output is correct |
50 |
Correct |
4 ms |
3792 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 |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 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 |
0 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
0 ms |
604 KB |
Output is correct |
62 |
Correct |
0 ms |
604 KB |
Output is correct |
63 |
Correct |
0 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
4 ms |
3532 KB |
Output is correct |
66 |
Correct |
4 ms |
3536 KB |
Output is correct |
67 |
Correct |
5 ms |
3536 KB |
Output is correct |
68 |
Correct |
4 ms |
4816 KB |
Output is correct |
69 |
Correct |
4 ms |
4816 KB |
Output is correct |
70 |
Correct |
18 ms |
3420 KB |
Output is correct |
71 |
Correct |
193 ms |
102020 KB |
Output is correct |
72 |
Correct |
191 ms |
102044 KB |
Output is correct |
73 |
Correct |
165 ms |
102104 KB |
Output is correct |
74 |
Correct |
207 ms |
102032 KB |
Output is correct |
75 |
Correct |
194 ms |
102056 KB |
Output is correct |
76 |
Correct |
174 ms |
102056 KB |
Output is correct |
77 |
Correct |
207 ms |
102032 KB |
Output is correct |
78 |
Correct |
181 ms |
102028 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
344 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 |
5072 KB |
Output is correct |
91 |
Correct |
4 ms |
5072 KB |
Output is correct |
92 |
Correct |
4 ms |
4816 KB |
Output is correct |
93 |
Correct |
16 ms |
3420 KB |
Output is correct |
94 |
Correct |
170 ms |
102228 KB |
Output is correct |
95 |
Correct |
176 ms |
102052 KB |
Output is correct |
96 |
Correct |
190 ms |
102048 KB |
Output is correct |
97 |
Correct |
189 ms |
102028 KB |
Output is correct |
98 |
Correct |
182 ms |
102052 KB |
Output is correct |
99 |
Correct |
190 ms |
106036 KB |
Output is correct |
100 |
Correct |
205 ms |
106040 KB |
Output is correct |
101 |
Correct |
205 ms |
105888 KB |
Output is correct |
102 |
Correct |
196 ms |
105900 KB |
Output is correct |
103 |
Correct |
196 ms |
106056 KB |
Output is correct |
104 |
Correct |
224 ms |
106068 KB |
Output is correct |
105 |
Correct |
176 ms |
104836 KB |
Output is correct |