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个区间,让你把这N个区间分成2个非空的集合,使不存在任意一个元素x,它即被第一个集合的某一个区间包含即L<=x<=R,也被第二个集合的某些区间包含。

如果不可以分,输出-1,如果可以,输出1~n个数,代表第i的区间放在第d个集合,d为1或2.

思路,根据L和R把区间排序后,先把排序后的第一个区间的L和R作为第一个集合的总L和R,那么我们来维护这个L和R,使第一个集合的L~R是一个连续的区间。(L~R每一个元素都可以在第一个集合中找到区间包含)

接下来从2~n遍历区间

如果下一个区间和L~R有交集,那么加入到第一个集合,更新L和R,

否则加入到第二个集合之中。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
struct node
{int l;int r;int id;
};
typedef struct  node node;
std::vector<node> v;
int n;
bool cmp(node a,node b)
{if(a.l!=b.l){return a.l<b.l;}else{return a.r<b.r;}
}
int ans[maxn];
int main()
{scanf("%d",&t);while(t--){v.clear();scanf("%d",&n);node temp;repd(i,1,n){scanf("%d %d",&temp.l,&temp.r);temp.id=i;v.push_back(temp);}sort(v.begin(), v.end(),cmp);int le,ri;le=v[0].l;ri=v[0].r;int is2=0;ans[v[0].id]=1;for(int i=1;i<=n-1;i++){temp=v[i];if(temp.l<=le&&temp.r>=ri){le=temp.l;ri=temp.r;ans[v[i].id]=1;}else if(temp.l<=ri&&temp.r<=ri){
//                ri=temp.r;ans[v[i].id]=1;}else if(temp.l<=ri&&temp.r>ri){ri=temp.r;ans[v[i].id]=1;}else if(temp.l>ri){is2=1;ans[v[i].id]=2;}}if(is2){repd(i,1,n){printf("%d ",ans[i]);}printf("\n");}else{printf("-1\n");}}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/10259473.html

Division and Union CodeForces - 1101C (排序后处理)相关推荐

  1. mysql union如何排序_Mysql中UNION用法与排序

    最近也是在写项目中碰到的这个问题,需要将两个SELECT查询结果组合起来进行分组排序,想到了用union方法,用TP的union操作根本无法完成复杂的union操作,于是搜罗了一下,先说一下union ...

  2. Division and Union

    https://codeforces.com/contest/1101/problem/C C++版本一 题解: /* *@Author: STZG *@Language: C++ */ #inclu ...

  3. 【CF套题】 Educational Codeforces Round 58

    [前言] 组队CF之帮wyl上橙,我和sc打小号上紫. 结果sc成功FST两题,wyl成功skipped. 我的小号幸存了qwq. [题目] 原题地址 A.Minimum Integer 特判一下dd ...

  4. Codeforces数学1600day3[数学CodeForces - 1213D2, CodeForces - 1165E 数论,CodeForces - 1165D 因子分解]

    ps:day2太水了不写qwq A - Equalizing by Division (hard version) CodeForces - 1213D2 题目大意:给你n个数和一个k,然后你可以执行 ...

  5. 索引系列八--索引特性之有序难优化union

    ----UNION 是需要排序的 drop table t1 purge; create table t1 as select * from dba_objects where object_id i ...

  6. Union和Union All的区别

    Union和Union All的区别 假设我们有一个表Student,包括以下字段与数据: drop table student;   create table student ( id int pr ...

  7. sql中union和union all的区别

    union 连接两个表查询的结果 假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plaincopy drop table student; create tabl ...

  8. UNION 和UNION ALL 的区别

    UNION:的结果集没有重复行,且安union默认的排序规则进行排序了. UNION ALL:的结果集,如果各表有重复行就有重复行,不删重复行,不排序. ----------------------- ...

  9. SQL Server中SQL Union vs Union All

    This article explains to the SQL Union and vs Union All operators in SQL Server. We will also explor ...

最新文章

  1. 用 Python 和 OpenCV 检测和跟踪运动对象
  2. c语言如何获取串口列表,如何通过串口来读写数据,请教达人
  3. 【转】python-word2vec模块使用详解
  4. Linux 下 MySQL 启动与关闭 说明
  5. Linux之df命令
  6. 取消管理员取得所有权_苏州即将全面取消房迁落户!有房就能落户的时代结束了!...
  7. 应用安全-CMF/CMS漏洞整理
  8. java8函数式编程闭包_java8入门必备—函数式编程思维——函数式语言向语言和运行时让渡控制权的途径——迭代让位于高阶函数、闭包...
  9. 「轻阅读」基于 Flink SQL CDC的实时数据同步方案,附视频教程
  10. 【高等数学】多元函数f(x,y...)的泰勒(Taylor)展开式
  11. 迅为-iMX6ULL开发板原创嵌入式开发文档系统化学习
  12. HTML文件命名规范大全
  13. 手机上的Linux体验----Aid learning
  14. LwIP+ STM32+HTTP例程参考
  15. 利用AJAX做天气预报
  16. 冯诺伊曼体系结构建模与模拟 之TOY模型机※
  17. java获取环境变量路径/java获取环境变量和系统属性
  18. 2010网易校园招聘笔试题
  19. 项目实习(五)网络渗透实验
  20. checkra1n越狱错误79_苹果越狱工具 checkra1n 更新:支持 iOS 13.4.1

热门文章

  1. 如何手动创建SAP S/4HANA的生产订单
  2. 如何查看Kubernetes pod yaml文件的在线语法帮助
  3. 如何在Netweaver SE16里直接查看某数据库行记录 1
  4. SAP S4CRM和C4C的技术比较
  5. 船体6自由度英文名称
  6. mysql级联_MySQL 级联复制(A-B-C)
  7. python修改文件后缀_python实现遍历文件夹修改文件后缀
  8. 用python编写杨辉三角金字塔_用python实现三道简单算法题:杨辉三角,蛇形矩阵,金字塔...
  9. 傅里叶变换表_Numpy库小抄表!主要语法和代码都在这里啦
  10. 电路板上的插头怎么拔下来_中国连城 | 接插件在电路板上的作用及它的种类介绍...