#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=2e5+5;
int L[N],R[N],n,m;
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>L[i]>>R[i];
int ans1=0,ans2=1;
vector<pair<int,int>> a;
vector<int> p(n);
for (int i=1;i<=n;i++){
if (L[i]<R[i]) a.pb({L[i],R[i]});
else a.pb({L[i],m-1});
}
sort(a.begin(),a.end());
a.pb({m-1,m-1});
p[0]=a[0].S;
for (int i=1;i<n;i++) p[i]=max(p[i-1],a[i].S);
int cur=0;
for (int i=0;i<=n;i++){
if (a[i].F<=cur) continue;
ans1++;
cur=p[i-1];
if (a[i].F>cur){
ans1=INT_MAX;
break;
}
}
if (cur!=m-1) ans1=INT_MAX;
a.clear();
int x=m;
for (int i=1;i<=n;i++){
if (R[i]) continue;
x=min(x,L[i]);
}
if (x==m){
if (ans1==INT_MAX) ans1=-1;
cout<<ans1<<endl;
return 0;
}
if (x==1){
ans1=min(ans1,1LL);
cout<<ans1<<endl;
return 0;
}
for (int i=1;i<=n;i++){
if (!R[i]) continue;
int l=max(L[i],1LL),r=min(R[i],x-1);
a.pb({l,r});
}
sort(a.begin(),a.end());
a.pb({x-1,x-1});
p.resize(a.size());
p[0]=a[0].S;
for (int i=1;i<n;i++) p[i]=max(p[i-1],a[i].S);
cur=1;
for (int i=0;i<a.size();i++){
if (a[i].F<=cur) continue;
ans2++;
cur=p[i-1];
if (a[i].F>cur){
ans2=INT_MAX;
break;
}
}
if (cur!=x-1) ans2=INT_MAX;
ans1=min(ans1,ans2);
if (ans1==INT_MAX) ans1=-1;
cout<<ans1<<endl;
return 0;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:71:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i=0;i<a.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
348 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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
856 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
856 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Incorrect |
25 ms |
10020 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 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 |
464 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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
476 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
808 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Incorrect |
30 ms |
9952 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |