C. Division and Union

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值