Division and Union CodeForces - 1101C (排序后处理)
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
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
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 (排序后处理)的更多相关文章
- 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 ...
- CodeForces - 426A(排序)
Sereja and Mugs Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- CF1101C Division and Union 线段相交问题
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...
- mysql union (all) 后order by的排序失效问题解决
上sql select * FROM ( SELECT SUM(c.overtime_num) AS delay_num, ) rate , '全网' as reaCodeFROM calc_vmap ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 58 A,B,C,D,E,G
A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...
- Educational Codeforces Round 58 Solution
A. Minimum Integer 签到. #include <bits/stdc++.h> using namespace std; #define ll long long ll l ...
- 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 ...
- Codeforces Edu Round 58 A-E
A. Minimum Integer 如果\(d < l\),则\(d\)满足条件 否则,输出\(d * (r / d + 1)\)即可. #include <cstdio> #in ...
随机推荐
- jQuery中 对标签元素操作(1)
一:创建元素节点(添加) 创建元素节点并且把节点作为元素的子节点添加到DOM树上 append(): 在元素下添加元素 用法:$("id").append(" ...
- 为爱好舞蹈的人们做的软件,细究数据结构,操作系统,磁盘原理,用java/c/c++写一个开源 MP3助手
1.可以给歌曲间插播空白音乐 2.拖拽式调整 3.先排序,后一键写入顺序文件. 国外的开源软件 MP3 播放排序 http://www.murraymoffatt.com/software-prob ...
- 【vue】使用localStorage解决vuex在页面刷新后数据被清除的问题
通常,我们在使用vue编写页面时,会需要使用vuex在组件间传递(或者说共同响应)同一个数据的变化.例如:用户的登录信息. 下面,我们使用传递用户登录信息的例子来一步步解决这个问题. 首先,我们的第一 ...
- ant.design React使用Echarts,实力踩坑
最近项目用到Echarts(以下用ec代替),于是照猫画虎得引入到团队的antd项目中,但是遇到2个棘手问题: 1. ec对dom不渲染,检查后发现,原来是全局存在id重复,所以使用React时,最好 ...
- [CQOI2017]老C的键盘
[CQOI2017]老C的键盘 题目描述 额,网上题解好像都是用的一大堆组合数,然而我懒得推公式. 设\(f[i][j]\)表示以\(i\)为根,且\(i\)的权值为\(j\)的方案数. 转移: \[ ...
- Mysql 数据类型 以及约束
数据类型 整型 默认有符号 无符号(unsigned) 和有符号 用 0 填充 zerofill 约束的作用: 保证数据的完整性 和一致性 tinyint[ -128 , 127 ] 小整数 无符号( ...
- WPF设计の画刷(Brush)
一.什么是画刷 画刷是是一种渲染方式,用于填充图形形状,如矩形.椭圆.扇形.多边形和封闭路径.在GDI+中,画刷分为以下几种:SolidBrush,TextureBrush,HatchBrush,Li ...
- JDK动态代理给Spring事务埋下的坑!
一.场景分析 最近做项目遇到了一个很奇怪的问题,大致的业务场景是这样的:我们首先设定两个事务,事务parent和事务child,在Controller里边同时调用这两个方法,示例代码如下: 1.场景A ...
- [HEOI2016/TJOI2016]排序
嘟嘟嘟 首先这题的暴力是十分好写的,而且据说能得不少分. 正解写起来不难,就是不太好想. 根据做题经验,我想到了给这个序列转化成01序列,但是接下来我就不会了.还是看了题解. 因为查询只有一个数,所以 ...
- 【css】max-height,min-height,height一起使用时,优先级问题
MDN说法: max-height 这个属性会阻止 height 属性的设置值变得比 max-height 更大. max-height 属性用来设置给定元素的最大高度. 如果height 属性设置的 ...