There are ? segments [??,??] for 1≤?≤?
. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer ?
(1≤?≤50000
) — the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer ?
(2≤?≤105) — number of segments. It is guaranteed that ∑? over all queries does not exceed 105
.
The next ?
lines contains two integers ??, ?? per line (1≤??≤??≤2⋅105) — the ?
-th segment.
Output
For each query print ?
integers ?1,?2,…,?? (??∈{1,2}) — for each segment (in the same order as in the input) ?? equals 1 if the ?-th segment will belongs to the first group and 2
otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print −1
.
Example
Input
Copy
3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5
Output
Copy
2 1 -1 1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is −1
.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6
possible is correct
题解:将n个区间分成两组,使得任意两组不存在相交的点,我们可以找一个间隔点使得该点左边的区间为1组,右边的区间为2组,否则如果整个区间都相交(直接或者间接)是分不成2组的。
注意按照原数组顺序输出。
#include<bits/stdc++.h>
using namespace std;
struct node
{
int l,r,id;
}q[200020];
int cmp1(node a,node b)
{
return a.l<b.l;
}
int cmp2(node a,node b)
{
return a.id<b.id;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=1; i<=n; i++)
cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+1+n,cmp1);
int pos=0,r=q[1].r;
for(int i=2;i<=n;i++){
if(q[i].l>r)
pos=q[i].l;
r=max(r,q[i].r);
}
sort(q+1,q+1+n,cmp2);
if(pos){
for(int i=1;i<=n;i++){
if(q[i].l<pos)
cout<<"1 ";
else
cout<<"2 ";
}
}
else
cout<<"-1";
cout<<endl;
}
return 0;
}