Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.
Chiaki is interested in the minimum number of intervals which need to be deleted.
Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.
Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤ 109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ n, li ≠ lj or ri ≠ rj.
It is guaranteed that the sum of all n does not exceed 500000.
For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the index of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.
1 11 2 5 4 7 3 9 6 11 1 12 10 15 8 17 13 18 16 20 14 21 19 22
43 5 7 10
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y,di;
} a[50005];
bool cmp(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
int s[50005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y); a[i].di=i+1;
}
sort(a,a+n,cmp);//把y按从小到大排序
int p1=0,p2=-1;//p1代表三个区间的第一个区间,p2代表三个区间的第二个区间
int p=0;
for(int i=1; i<n; i++)
{
if(a[i].x<=a[p1].y&&p2==-1)//先找到与第一个区间相交的区间下标记为p2=i
{
p2=i;
}
else if(a[i].x>a[p1].y&&p2==-1)//如果没有找到与第一个相交的区间,那么就更新第一个区间
{
p1=i;
}
else if(a[i].x<=a[p1].y&&a[i].x<=a[p2].y&&p2!=-1)//如果第三个区间与第一个第二个区间相交,那么就把第三个区间删了,记录删的下标
{
s[p++]=a[i].di;
}
else if(a[i].x>a[p1].y&&a[i].x<=a[p2].y&&p2!=-1)//如果第三个区间与第二个相交不与第一个相交,那么就更新p1,p2
{
p1=p2;
p2=i;
}
else if(a[i].x>a[p2].y&&p2!=-1)//如果第三个区间与第二个区间不相交,那么就更新p2
{
//p1=i;
p2=i;
}
}
printf("%d\n",p);
sort(s,s+p);
for(int i=0; i<p; i++)
{
if(i<=p-2)
printf("%d ",s[i]);
else
printf("%d\n",s[i]);
}
}
}