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=;
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,,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[].l;
ri=v[].r;
int is2=;
ans[v[].id]=;
for(int i=;i<=n-;i++)
{
temp=v[i];
if(temp.l<=le&&temp.r>=ri)
{
le=temp.l;
ri=temp.r;
ans[v[i].id]=;
}else if(temp.l<=ri&&temp.r<=ri)
{
// ri=temp.r;
ans[v[i].id]=;
}else if(temp.l<=ri&&temp.r>ri)
{
ri=temp.r;
ans[v[i].id]=;
}
else if(temp.l>ri)
{
is2=;
ans[v[i].id]=;
} }
if(is2)
{
repd(i,,n)
{
printf("%d ",ans[i]);
}
printf("\n");
}else
{
printf("-1\n");
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Division and Union CodeForces - 1101C (排序后处理)的更多相关文章

  1. Educational Codeforces Round 4 D. The Union of k-Segments 排序

    D. The Union of k-Segments   You re given n segments on the coordinate axis Ox and the number k. The ...

  2. CodeForces - 426A(排序)

    Sereja and Mugs Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  3. CF1101C Division and Union 线段相交问题

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...

  4. mysql union (all) 后order by的排序失效问题解决

    上sql select * FROM ( SELECT SUM(c.overtime_num) AS delay_num, ) rate , '全网' as reaCodeFROM calc_vmap ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  6. Educational Codeforces Round 58 A,B,C,D,E,G

    A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...

  7. Educational Codeforces Round 58 Solution

    A. Minimum Integer 签到. #include <bits/stdc++.h> using namespace std; #define ll long long ll l ...

  8. Educational Codeforces Round 58 (Rated for Div. 2)

    A. Minimum Integer 水 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using name ...

  9. Codeforces Edu Round 58 A-E

    A. Minimum Integer 如果\(d < l\),则\(d\)满足条件 否则,输出\(d * (r / d + 1)\)即可. #include <cstdio> #in ...

随机推荐

  1. pom文件

    groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...

  2. puppet master 用 nginx + unicorn 作为前端

    目录 1. 概要 2. nginx + unicorn 配置 2.1. package 安装 2.2. 配置文件设置 2.2.1. 配置 unicorn 2.2.2. 配置nginx 2.3. 测试配 ...

  3. ubuntu 安装 GCC 和 G++ C++ 开发环境

    1.先安装 :sudo apt-get install build-essential 2.查看 gcc 版本 然后安装 统一版本的 g++ gcc --version gcc (Ubuntu/Lin ...

  4. js刷新页面的几种方式与区别

    Javascript刷新页面的几种方法:1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locati ...

  5. JavaScript判断数据类型的方法

    typeof操作符 typeof 操作符作用:是用来检测变量的数据类型.对于值或变量使用 typeof 操作符会返回如下字符串. 数据类型undefined的判断示例 变量定义了但未初始化,就是und ...

  6. layui之日期和时间组件

    参考文档:https://www.layui.com/doc/modules/laydate.html代码片段如下: layui.use('laydate', function(){ var layd ...

  7. 为什么我的mac插入耳机耳机没有声音呢?

    macOS 系统莫名其妙就遇到声音和音频播放问题的情况相当普遍,在新添音频设备.应用程序之间进行切换或更新操作系统后,都可能会遇到音频错误.好加在,解决大多数 macOS 声音无法正常工作的方法都非常 ...

  8. eclipse导入maven项目,资源文件位置显示不正确

    eclipse导入maven项目后,资源文件位置显示不正确,如下图所示 解决方法: 在resources上右键Build Path,选择Use as Source Folder即可正确显示资源文件

  9. mysql 行转列 列转行

    一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id ) ...

  10. Java IO(四)——字符流

    一.字符流 字节流提供了处理任何类型输入/输出操作的功能(因为对于计算机而言,一切都是0和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,因为一个Unicode字符占用2 ...