Visiting a Friend

Pig is visiting a friend.

Pig's house is located at point 0, and his friend's house is located at point m on an axis.

Pig can use teleports to move along the axis.

To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.

Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds.

Determine if Pig can visit the friend using teleports only, or he should use his car.

Input

The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of teleports and the location of the friend's house.

The next n lines contain information about teleports.

The i-th of these lines contains two integers ai and bi (0 ≤ ai ≤ bi ≤ m), where aiis the location of the i-th teleport, and bi is its limit.

扫描二维码关注公众号,回复: 5103734 查看本文章

It is guaranteed that ai ≥ ai - 1 for every i (2 ≤ i ≤ n).

Output

Print "YES" if there is a path from Pig's house to his friend's house that uses only teleports, and "NO" otherwise.

You can print each letter in arbitrary case (upper or lower).

Examples

Input

3 5
0 2
2 4
3 5

Output

YES

Input

3 7
0 4
2 5
6 7

Output

NO

Note

The first example is shown on the picture below:

Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.

The second example is shown on the picture below:

You can see that there is no path from Pig's house to his friend's house that uses only teleports.

题意
      给出n,m,然后是n个a,b(0<=a<=b<=m),且a非严格单调递增 
     表示在坐标轴上有n个在ai处的传送门,每个可以传送到[ai,bi]的任意位置,问是否可以仅通过传送门从0到达m.

思路:
       一开始我写的一次遍历每个段,如果当前段的左边小于等于前一个段的右边就可以,但是却忘记了如果前一个段直接可以到达m点,而这个段之后的每个段都是相互独立相互不可达的,这种情况也是可以到达的

借鉴别人:

       注意到如果位置x可达,那么[0,x]全部可达,所以仅需维护一个最远可达的距离,按顺序遍历传送门即可

代码:

    

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<map>
#include<set>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
int main(){
	int n,m;
	cin>>n>>m;
	int most=0;
	int a,b;
	while(n--){
	   scanf("%d%d",&a,&b);
	   if(most>=a)
	      most=max(most,b);	
	}
	if(most>=m) cout<<"YES"<<endl;
	else cout<<"NO"<<endl; 
	return 0;
}


 

猜你喜欢

转载自blog.csdn.net/daoshen1314/article/details/86656040