Day 28 - interval coverage
重铸华农荣光 我辈义不容辞
Day 28- interval coverage
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int n,st,ed;
struct Range{
int l,r;
bool operator < (const Range & w) const{
return l<w.l;
}
}range[N];
int main()
{
cin>>st>>ed;
cin>>n;
for(int i=0;i<n;i++)
{
int l,r;
cin>>l>>r;
range[i]={l,r};
}
sort(range,range+n);
int ans=0;
bool flag=false;
for(int i=0;i<n;i++)
{
int j=i,r=-2e9;
while(j<n&&range[j].l<=st)
{
r=max(r,range[j].r);
j++;
}
if(r<st)
{
ans=-1;
break;
}
ans++;
if(r>=ed)
{
flag=true;
break;
}
st=r;
i=j-1;
}
if(flag) cout<<ans;
else cout<<-1;
}