#include "bits/stdc++.h"
//#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
int lift[N][LOG];
vector<array<int,2>> v;
vector<array<int,2>> ar;
struct SegT{
int n;
vector<int> seg;
SegT(int _n){
n=_n;
seg.assign(4*n+5,-1);
}
int merge(int a,int b){
if(a==-1 || b==-1) return (a==-1 ? b : a);
if(ar[a][1]>ar[b][1]) return a;
return b;
}
void update(int rt,int l,int r,int ind,int u){
if(r<ind || l>ind) return;
if(l==r){
seg[rt]=merge(seg[rt],u);
return;
}
int m=(l+r)/2;
update(rt*2,l,m,ind,u),update(rt*2+1,m+1,r,ind,u);
seg[rt]=merge(seg[rt*2],seg[rt*2+1]);
}
int query(int rt,int l,int r,int gl,int gr){
if(r<gl || l>gr) return -1;
if(l>=gl && r<=gr) return seg[rt];
int m=(l+r)/2;
return merge(query(rt*2,l,m,gl,gr),query(rt*2+1,m+1,r,gl,gr));
}
};
void _(){
int n,m;
cin >> n >> m;
for(int i=0;i<n;i++){
int a,b;
cin >> a >> b;
if(a>b) b+=m;
else{
a+=m;
b+=m;
}
v.push_back({a,b});
}
sort(all(v));
vector<int> st(n,0);
SegT T(2*n+5);
ar.resize(n);
vector<int> zip;
for(int i=0;i<n;i++){
zip.push_back(v[i][0]);
zip.push_back(v[i][1]);
}
sort(all(zip));
zip.erase(unique(all(zip)),zip.end());
for(int i=0;i<n;i++){
if(v[i][0]<m && v[i][1]>=m) st[i]=1;
ar[i][0]=lower_bound(all(zip),v[i][0])-zip.begin();
ar[i][1]=lower_bound(all(zip),v[i][1])-zip.begin();
}
memset(lift,-1,sizeof(lift));
for(int i=0;i<n;i++) T.update(1,0,2*n,ar[i][0],i);
for(int i=0;i<n;i++){
lift[i][0]=T.query(1,0,2*n,ar[i][0]+1,ar[i][1]);
if(lift[i][0]!=-1 && ar[lift[i][0]][1]<=ar[i][1]) lift[i][0]=-1;
//cout << i << ' ' << lift[i][0] << '\n';
}
for(int i=1;i<LOG;i++)
for(int j=0;j<n;j++){
if(lift[j][i-1]!=-1){
lift[j][i]=lift[lift[j][i-1]][i-1];
}
}
int ans=(int)1e9;
for(int i=0;i<n;i++){
if(!st[i]) continue;
int dur=m+v[i][0];
//cout << i << ' ' << dur << '\n';
int p=i,cur=1;
for(int j=LOG-1;j>=0;j--){
if(lift[p][j]==-1) continue;
if(v[lift[p][j]][1]>=dur) continue;
cur+=(1<<j);
p=lift[p][j];
}
//cout << p << ' ' << cur << '\n';
if(lift[p][0]!=-1 && v[lift[p][0]][1]>=dur) ans=min(ans,cur+1);
}
if(ans==(int)1e9) cout << -1 << '\n';
else cout << ans << '\n';
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15960 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
8 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15908 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
8 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15964 KB |
Output is correct |
12 |
Correct |
7 ms |
16080 KB |
Output is correct |
13 |
Correct |
8 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15964 KB |
Output is correct |
15 |
Correct |
6 ms |
15964 KB |
Output is correct |
16 |
Correct |
6 ms |
15960 KB |
Output is correct |
17 |
Correct |
6 ms |
15964 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
7 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
7 ms |
15964 KB |
Output is correct |
23 |
Correct |
7 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15960 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
8 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15908 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
8 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15964 KB |
Output is correct |
12 |
Correct |
7 ms |
16080 KB |
Output is correct |
13 |
Correct |
8 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15964 KB |
Output is correct |
15 |
Correct |
6 ms |
15964 KB |
Output is correct |
16 |
Correct |
6 ms |
15960 KB |
Output is correct |
17 |
Correct |
6 ms |
15964 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
7 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
7 ms |
15964 KB |
Output is correct |
23 |
Correct |
7 ms |
15960 KB |
Output is correct |
24 |
Correct |
7 ms |
15960 KB |
Output is correct |
25 |
Correct |
8 ms |
15964 KB |
Output is correct |
26 |
Correct |
6 ms |
15964 KB |
Output is correct |
27 |
Correct |
7 ms |
15964 KB |
Output is correct |
28 |
Correct |
7 ms |
15964 KB |
Output is correct |
29 |
Correct |
7 ms |
16112 KB |
Output is correct |
30 |
Correct |
7 ms |
16044 KB |
Output is correct |
31 |
Correct |
8 ms |
16064 KB |
Output is correct |
32 |
Correct |
7 ms |
15964 KB |
Output is correct |
33 |
Correct |
6 ms |
15964 KB |
Output is correct |
34 |
Correct |
7 ms |
15964 KB |
Output is correct |
35 |
Correct |
7 ms |
15964 KB |
Output is correct |
36 |
Correct |
6 ms |
16032 KB |
Output is correct |
37 |
Correct |
7 ms |
15964 KB |
Output is correct |
38 |
Correct |
7 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15960 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
8 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15908 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
8 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15964 KB |
Output is correct |
12 |
Correct |
7 ms |
16080 KB |
Output is correct |
13 |
Correct |
8 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15964 KB |
Output is correct |
15 |
Correct |
6 ms |
15964 KB |
Output is correct |
16 |
Correct |
6 ms |
15960 KB |
Output is correct |
17 |
Correct |
6 ms |
15964 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
7 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
7 ms |
15964 KB |
Output is correct |
23 |
Correct |
7 ms |
15960 KB |
Output is correct |
24 |
Correct |
7 ms |
15960 KB |
Output is correct |
25 |
Correct |
8 ms |
15964 KB |
Output is correct |
26 |
Correct |
6 ms |
15964 KB |
Output is correct |
27 |
Correct |
7 ms |
15964 KB |
Output is correct |
28 |
Correct |
7 ms |
15964 KB |
Output is correct |
29 |
Correct |
7 ms |
16112 KB |
Output is correct |
30 |
Correct |
7 ms |
16044 KB |
Output is correct |
31 |
Correct |
8 ms |
16064 KB |
Output is correct |
32 |
Correct |
7 ms |
15964 KB |
Output is correct |
33 |
Correct |
6 ms |
15964 KB |
Output is correct |
34 |
Correct |
7 ms |
15964 KB |
Output is correct |
35 |
Correct |
7 ms |
15964 KB |
Output is correct |
36 |
Correct |
6 ms |
16032 KB |
Output is correct |
37 |
Correct |
7 ms |
15964 KB |
Output is correct |
38 |
Correct |
7 ms |
15964 KB |
Output is correct |
39 |
Correct |
11 ms |
16476 KB |
Output is correct |
40 |
Correct |
9 ms |
16476 KB |
Output is correct |
41 |
Correct |
9 ms |
16568 KB |
Output is correct |
42 |
Correct |
9 ms |
16528 KB |
Output is correct |
43 |
Correct |
9 ms |
16472 KB |
Output is correct |
44 |
Correct |
8 ms |
16476 KB |
Output is correct |
45 |
Correct |
8 ms |
16476 KB |
Output is correct |
46 |
Correct |
9 ms |
16452 KB |
Output is correct |
47 |
Correct |
10 ms |
16476 KB |
Output is correct |
48 |
Correct |
10 ms |
16420 KB |
Output is correct |
49 |
Correct |
10 ms |
16476 KB |
Output is correct |
50 |
Correct |
9 ms |
16476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15960 KB |
Output is correct |
2 |
Correct |
7 ms |
16084 KB |
Output is correct |
3 |
Correct |
7 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
16016 KB |
Output is correct |
6 |
Correct |
7 ms |
15960 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
9 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
6 ms |
16216 KB |
Output is correct |
11 |
Correct |
7 ms |
15960 KB |
Output is correct |
12 |
Correct |
6 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
16120 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
10 ms |
16472 KB |
Output is correct |
16 |
Correct |
8 ms |
16476 KB |
Output is correct |
17 |
Correct |
10 ms |
16488 KB |
Output is correct |
18 |
Correct |
10 ms |
16476 KB |
Output is correct |
19 |
Correct |
10 ms |
16476 KB |
Output is correct |
20 |
Correct |
87 ms |
28760 KB |
Output is correct |
21 |
Correct |
154 ms |
31644 KB |
Output is correct |
22 |
Correct |
153 ms |
31800 KB |
Output is correct |
23 |
Correct |
120 ms |
30860 KB |
Output is correct |
24 |
Correct |
140 ms |
31804 KB |
Output is correct |
25 |
Correct |
169 ms |
31312 KB |
Output is correct |
26 |
Correct |
124 ms |
31712 KB |
Output is correct |
27 |
Correct |
120 ms |
31808 KB |
Output is correct |
28 |
Correct |
132 ms |
31668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15960 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
16056 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
6 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
6 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
10 ms |
16476 KB |
Output is correct |
13 |
Correct |
9 ms |
16476 KB |
Output is correct |
14 |
Correct |
8 ms |
16404 KB |
Output is correct |
15 |
Correct |
105 ms |
28744 KB |
Output is correct |
16 |
Correct |
119 ms |
30792 KB |
Output is correct |
17 |
Correct |
147 ms |
31816 KB |
Output is correct |
18 |
Correct |
153 ms |
31604 KB |
Output is correct |
19 |
Correct |
127 ms |
31820 KB |
Output is correct |
20 |
Correct |
122 ms |
30792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15960 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
6 ms |
15964 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
8 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15908 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
6 ms |
15964 KB |
Output is correct |
10 |
Correct |
8 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15964 KB |
Output is correct |
12 |
Correct |
7 ms |
16080 KB |
Output is correct |
13 |
Correct |
8 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15964 KB |
Output is correct |
15 |
Correct |
6 ms |
15964 KB |
Output is correct |
16 |
Correct |
6 ms |
15960 KB |
Output is correct |
17 |
Correct |
6 ms |
15964 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
7 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
7 ms |
15964 KB |
Output is correct |
23 |
Correct |
7 ms |
15960 KB |
Output is correct |
24 |
Correct |
7 ms |
15960 KB |
Output is correct |
25 |
Correct |
8 ms |
15964 KB |
Output is correct |
26 |
Correct |
6 ms |
15964 KB |
Output is correct |
27 |
Correct |
7 ms |
15964 KB |
Output is correct |
28 |
Correct |
7 ms |
15964 KB |
Output is correct |
29 |
Correct |
7 ms |
16112 KB |
Output is correct |
30 |
Correct |
7 ms |
16044 KB |
Output is correct |
31 |
Correct |
8 ms |
16064 KB |
Output is correct |
32 |
Correct |
7 ms |
15964 KB |
Output is correct |
33 |
Correct |
6 ms |
15964 KB |
Output is correct |
34 |
Correct |
7 ms |
15964 KB |
Output is correct |
35 |
Correct |
7 ms |
15964 KB |
Output is correct |
36 |
Correct |
6 ms |
16032 KB |
Output is correct |
37 |
Correct |
7 ms |
15964 KB |
Output is correct |
38 |
Correct |
7 ms |
15964 KB |
Output is correct |
39 |
Correct |
11 ms |
16476 KB |
Output is correct |
40 |
Correct |
9 ms |
16476 KB |
Output is correct |
41 |
Correct |
9 ms |
16568 KB |
Output is correct |
42 |
Correct |
9 ms |
16528 KB |
Output is correct |
43 |
Correct |
9 ms |
16472 KB |
Output is correct |
44 |
Correct |
8 ms |
16476 KB |
Output is correct |
45 |
Correct |
8 ms |
16476 KB |
Output is correct |
46 |
Correct |
9 ms |
16452 KB |
Output is correct |
47 |
Correct |
10 ms |
16476 KB |
Output is correct |
48 |
Correct |
10 ms |
16420 KB |
Output is correct |
49 |
Correct |
10 ms |
16476 KB |
Output is correct |
50 |
Correct |
9 ms |
16476 KB |
Output is correct |
51 |
Correct |
7 ms |
15960 KB |
Output is correct |
52 |
Correct |
7 ms |
16084 KB |
Output is correct |
53 |
Correct |
7 ms |
15964 KB |
Output is correct |
54 |
Correct |
7 ms |
15964 KB |
Output is correct |
55 |
Correct |
7 ms |
16016 KB |
Output is correct |
56 |
Correct |
7 ms |
15960 KB |
Output is correct |
57 |
Correct |
7 ms |
15964 KB |
Output is correct |
58 |
Correct |
9 ms |
15964 KB |
Output is correct |
59 |
Correct |
6 ms |
15964 KB |
Output is correct |
60 |
Correct |
6 ms |
16216 KB |
Output is correct |
61 |
Correct |
7 ms |
15960 KB |
Output is correct |
62 |
Correct |
6 ms |
15964 KB |
Output is correct |
63 |
Correct |
7 ms |
16120 KB |
Output is correct |
64 |
Correct |
7 ms |
15964 KB |
Output is correct |
65 |
Correct |
10 ms |
16472 KB |
Output is correct |
66 |
Correct |
8 ms |
16476 KB |
Output is correct |
67 |
Correct |
10 ms |
16488 KB |
Output is correct |
68 |
Correct |
10 ms |
16476 KB |
Output is correct |
69 |
Correct |
10 ms |
16476 KB |
Output is correct |
70 |
Correct |
87 ms |
28760 KB |
Output is correct |
71 |
Correct |
154 ms |
31644 KB |
Output is correct |
72 |
Correct |
153 ms |
31800 KB |
Output is correct |
73 |
Correct |
120 ms |
30860 KB |
Output is correct |
74 |
Correct |
140 ms |
31804 KB |
Output is correct |
75 |
Correct |
169 ms |
31312 KB |
Output is correct |
76 |
Correct |
124 ms |
31712 KB |
Output is correct |
77 |
Correct |
120 ms |
31808 KB |
Output is correct |
78 |
Correct |
132 ms |
31668 KB |
Output is correct |
79 |
Correct |
7 ms |
15960 KB |
Output is correct |
80 |
Correct |
6 ms |
15964 KB |
Output is correct |
81 |
Correct |
6 ms |
16056 KB |
Output is correct |
82 |
Correct |
7 ms |
15964 KB |
Output is correct |
83 |
Correct |
6 ms |
15964 KB |
Output is correct |
84 |
Correct |
7 ms |
15964 KB |
Output is correct |
85 |
Correct |
6 ms |
15964 KB |
Output is correct |
86 |
Correct |
6 ms |
15964 KB |
Output is correct |
87 |
Correct |
6 ms |
15964 KB |
Output is correct |
88 |
Correct |
7 ms |
15964 KB |
Output is correct |
89 |
Correct |
6 ms |
15960 KB |
Output is correct |
90 |
Correct |
10 ms |
16476 KB |
Output is correct |
91 |
Correct |
9 ms |
16476 KB |
Output is correct |
92 |
Correct |
8 ms |
16404 KB |
Output is correct |
93 |
Correct |
105 ms |
28744 KB |
Output is correct |
94 |
Correct |
119 ms |
30792 KB |
Output is correct |
95 |
Correct |
147 ms |
31816 KB |
Output is correct |
96 |
Correct |
153 ms |
31604 KB |
Output is correct |
97 |
Correct |
127 ms |
31820 KB |
Output is correct |
98 |
Correct |
122 ms |
30792 KB |
Output is correct |
99 |
Correct |
165 ms |
31820 KB |
Output is correct |
100 |
Correct |
159 ms |
31816 KB |
Output is correct |
101 |
Correct |
125 ms |
31732 KB |
Output is correct |
102 |
Correct |
146 ms |
31820 KB |
Output is correct |
103 |
Correct |
129 ms |
31812 KB |
Output is correct |
104 |
Correct |
132 ms |
32064 KB |
Output is correct |
105 |
Correct |
128 ms |
30532 KB |
Output is correct |