AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 100010;
int n;
struct Node {
int l, r;
bool operator <(const Node &w)
{
return l < w.l;//按照左端点从小到大进行排序
}
}node[N];
int main()
{
int st, ed;
scanf("%d%d", &st, &ed);
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int l, r;
scanf("%d%d", &l, &r);
node[i] = { l,r };
}
sort(node, node + n);
bool success = false;
int res = 0;
for (int i = 0; i < n; i++)
{
int j = i,r=-2e9;
while (j < n&&node[j].l <= st)
{
r = max(r, node[j].r);
j++;
}
if (r < st)
{
res = -1;
break;
}
res++;
if (r >= ed)
{
success = true;
break;
}
st = r;
i = j-1;
}
if (!success) res = -1;
printf("%d", res);
system("pause");
return 0;
}
/*
1 5
3
-1 3
2 4
3 5
*/