Pavel is developing a game. To do that, he needs functions available in a third-party library too famous to be called. It is known that the function i first appeared in version ai and existed until version bi, and starting from the version bi + 1, it is absent in this library.
The library is not free and Pavel needs all the functions. Which minimal number of versions he need to purchase to be able to use all the functions?
Input
The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of the functions.
Each of the next n lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ 109) — the interval of library versions where function i was available.
Output
In the first line output a single integer k — the minimal number of library versions need to be purchased to unlock all functions.
In the second line output k distinct integers — the numbers of versions need to be purchased.
If there are several possible answers, output any of them.
Example
Input
5
2 4
1 3
2 3
3 6
4 5
Output
2
3 4
先给区间按左区间从小到大排序,然后用mi和ma维护当前区间的左区间最大值和右区间最小值,用len记录更换的设备
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define N 200010
struct date
{
ll x,y;
}a[N];
bool cmp(date l,date r)
{
if(l.x==r.x)
return l.y<r.y;
else
return l.x<r.x;
}
int main()
{
int i,n,len;
ll s[N];
ll mi,ma;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y;
}
sort(a+1,a+1+n,cmp);
mi=a[1].x;
ma=a[1].y;
len=0;
for(i=2;i<=n;i++)
{
if(a[i].x>ma)
{
s[++len]=mi;//a[len]=mi或ma都可以,反正都是已输入区间的公共点
mi=a[i].x;
ma=a[i].y;
}
mi=max(mi,a[i].x);//s[len]必须保证每个区间都存在,所以不断缩小区间范围,找区间重叠点
ma=min(ma,a[i].y);
}
s[++len]=mi;//不要忘记最后一个区间
cout<<len<<endl;
for(i=1;i<=len;i++)
{
if(i==1)
cout<<s[i];
else
cout<<' '<<s[i];
}
cout<<endl;
return 0;
}