当前位置: 代码迷 >> 综合 >> CodeForces - 1101C - Division and Union
  详细解决方案

CodeForces - 1101C - Division and Union

热度:82   发布时间:2023-12-06 14:15:54.0

出自我自己的寒假康复训练,没康复成倒是wa了n多发,谨以此记。

C. Division and Union

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

3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5

output

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.

 

题意:这个题目就是给你n个范围[Li,Ri],然后把这些范围分成两堆(不能为空),每堆和另外一堆都不能有重合,最后输出每一个范围放在哪一堆(没有输出-1)。

分析:我们先将这些范围按照L从小到大排序,然后依次放到堆1,堆2,先放1再放2,再放1,不断重复。其中如果放1或者2不可以的话就放回原来那堆。例如前一个范围放的是堆1,现在的范围本应该放堆2,但是放堆2会和堆1冲突,于是我们将其放回堆1(放了肯定不会产生冲突)。依次类推直到放完所有的,如果最后有一个堆为空就说明不存在解,输出-1。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;struct node
{int l,r;int id;
}a[100005];//lmax控制堆1的最大值,rmax控制堆2的最大值
int ans[100005],lmax,rmax;//按照L升序,L相同R小的在前
int cmp(node a1,node a2)
{if (a1.l==a2.l)return a1.r<a2.r;elsereturn a1.l<a2.l;
}//flag1标记堆1不为空,flag2标记堆2不为空
int T,n,flag1,flag2;int main()
{cin >> T;while(T--){lmax=rmax=0; flag1=flag2=0;scanf("%d",&n); a[0].id=2;//记录idfor (int i=1;i<=n;i++){scanf("%d%d",&a[i].l,&a[i].r);a[i].id=i;}sort(a+1,a+n+1,cmp);for (int i=1;i<=n;i++){//放堆1if (i==1 || ans[a[i-1].id]==2){//放堆1不冲突if (a[i].l>rmax){ans[a[i].id]=1;//重要!!!一开始只写了a[i].r,而没有考虑到a[i].r可能小于a[i-1].rlmax=max(lmax,a[i].r);flag1=1;}//冲突,放堆2else{ans[a[i].id]=2;rmax=max(rmax,a[i].r);flag2=1;}}//放堆2else{//放堆2不冲突if (a[i].l>lmax){ans[a[i].id]=2;rmax=max(rmax,a[i].r);flag2=1;}//冲突,放堆1else{ans[a[i].id]=1;lmax=max(lmax,a[i].r);flag1=1;}}}//两堆都有if (flag1 && flag2){for (int i=1;i<=n;i++)printf("%d ",ans[i]);printf("\n");}//只有1堆elseprintf("-1\n");}return 0;
}