1101C. Division and Union

C. Division and Union

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn segments [li,ri][li,ri] for 1≤i≤n1≤i≤n. 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 TT (1≤T≤500001≤T≤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 nn (2≤n≤1052≤n≤105) — number of segments. It is guaranteed that ∑n∑n over all queries does not exceed 105105.

The next nn lines contains two integers lili, riri per line (1≤li≤ri≤2⋅1051≤li≤ri≤2⋅105) — the ii-th segment.

Output

For each query print nn integers t1,t2,…,tnt1,t2,…,tn (ti∈{1,2}ti∈{1,2}) — for each segment (in the same order as in the input) titi equals 11 if the ii-th segment will belongs to the first group and 22 otherwise.

If there are multiple answers, you can print any of them. If there is no answer, print −1−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−1.

In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 66 possible is correct.

题意就是看能不能把区间分成互不相交的两堆,然后输出每个区间可以放在哪个堆中。

是一个区间合并问题,所以我们要对每个区间的左端点进行排序,但是这样会打乱顺序,该怎么办呢? 这个时候就可以用到结构体,其实我觉得用map可能也行,用结构体把区间的序号存储起来,然后再写个函数把它排序这样就能在操作完后复现原来的区间顺序了。

#include <iostream>
#include <algorithm>
using namespace std;

struct node
{
	int l, r;
	int idx;//区间的序号
	int group;//区间所在的堆
}a[100005];

bool cmp(node a1, node a2)//对左端点排序的函数
{
	return a1.l < a2.l;
}

bool cmp1(node a1, node a2)//对区间序号排序的函数
{
	return a1.idx < a2.idx;
}
void solve()
{
	int n;
	cin >> n;
	int flag = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].l >> a[i].r;
		a[i].idx = i;
	}
	sort(a + 1, a + 1 + n,cmp);
	a[1].group = 1;
	int maxr = a[1].r;
	for (int i = 2; i <= n; i++)
	{
		if (a[i].l <= maxr)//左端点小于前一个区间的右端点,区间必然相交
		{
			a[i].group = 1;
			maxr = max(maxr, a[i].r);//取并集,即最大的右端点。
		}
		else
		{
			flag = 1;
			a[i].group = 2;//不在一号堆那肯定在二号堆。
		}
	}
	if (flag)
	{
		sort(a + 1, a + 1 + n, cmp1);
		for (int i = 1; i <= n; i++)
		{
			if (a[i].group == 1)
			{
				cout << 1 << " ";
			}
			else
			{
				cout << 2 << " ";
			}
		}
		cout << endl;
	}
	else//二号堆没有区间就输出-1
	{
		cout << -1 << endl;
	}
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值