【CF429E】Points and Segments 欧拉回路

【CF429E】Points and Segments

题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能是+1,-1或0。

$n\le 10^5$

题解:首先,我们用扫描线,整个数轴被分成若干个小区间。对于一个小区间,如果有偶数条线段包含它,则它的权值只能是0,否则可以是+1/-1。我们可以在所有权值为+1/-1的小区间处人为的增加一条线段,这样的话我们只需要让所有小区间权值都是0就行了。

嗯。。。每个小区间都被偶数个线段包含。。。权值和是0。。。想到什么呢?

如果我们给线段定向,向右的为+1,向左的为-1,那么我们要求的就是整个图的欧拉回路!于是dfs求欧拉回路即可!

细节:如果我们直接建图跑欧拉回路的话,则一条1-2,2-3的路径其实是不合法的,因为2实际上被包含了2次,而我们再建图时相当于直接越过了2这个点。解决方法是将区间变成左闭右开,即ri++。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=200010;
struct node
{
	int x,k,org;
}p[maxn];
int n,m,cnt;
int last[maxn<<1],to[maxn<<1],nxt[maxn<<1],head[maxn],val[maxn],vis[maxn],used[maxn<<1];
inline int rd()
{
	int ret=0;	char gc=getchar();
	while(gc<‘0‘||gc>‘9‘)	gc=getchar();
	while(gc>=‘0‘&&gc<=‘9‘)	ret=ret*10+gc-‘0‘,gc=getchar();
	return ret;
}
bool cmp(const node &a,const node &b)
{
	return a.x<b.x;
}
inline void add(int a,int b)
{
	to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
	vis[x]=1;
	for(int i=head[x];i!=-1;i=nxt[i])	if(!used[i])
		used[i]=1,used[i^1]=2,dfs(to[i]);
}
int main()
{
	//freopen("a.in","r",stdin);
	n=rd();
	int i;
	for(i=1;i<=n;i++)	p[i].x=rd(),p[i+n].x=rd()+1,p[i].k=1,p[i+n].k=-1,p[i].org=p[i+n].org=i;
	sort(p+1,p+2*n+1,cmp);
	memset(head,-1,sizeof(head));
	for(i=1;i<=n+n;i++)
	{
		if(i==1||p[i].x>p[i-1].x)
		{
			m++;
			if(!(i&1))	add(m-1,m),add(m,m-1);
		}
		if(p[i].k==1)	last[p[i].org]=m;
		else	add(last[p[i].org],m),add(m,last[p[i].org]),last[p[i].org]=cnt-2;
	}
	for(i=1;i<=m;i++)	if(!vis[i])	dfs(i);
	for(i=1;i<=n;i++)	printf("%d ",used[last[i]]&1);
	return 0;
}

原文地址:https://www.cnblogs.com/CQzhangyu/p/8723603.html

时间: 2024-10-11 03:23:36

【CF429E】Points and Segments 欧拉回路的相关文章

CF429E Points and Segments

Link 把红蓝分别看成\(+1,-1\),然后给序列差个分,我们要做的就是给一个无向图中的边定向使得存在一条欧拉回路.因为度数可能是奇数所以补一些\(0\)边就行了. #include<bits/stdc++.h> #define N 1000007 using namespace std; namespace IO { char ibuf[(1<<21)+1],obuf[(1<<21)+1],*iS,*iT,*oS=obuf,*oT=obuf+(1<<2

Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point{ int index, pos; Point(int index_ = 0, int pos_ = 0){ index = index_; pos = pos_; } bool operator < (const Point& a) const{ return p

lightoj-1088 - Points in Segments(二分法)

1088 - Points in Segments PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBGiven n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment

LightOJ 1088 Points in Segments 二分查找

1088 - Points in Segments PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segme

Codeforces 430A Points and Segments (easy)

题意:让你染色点,要求在给出的区间内|红色个数-蓝色个数|<=1 思路:排序后依次交替染色就能达到效果 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 110; int arr[MAXN]; int n,m,x,y; int

ACM ——Points in Segments

Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B. For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then t

CodeForces A. Points in Segments

http://codeforces.com/contest/1015/problem/A You are given a set of nn segments on the axis OxOx, each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is charac

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include

codeforces 430A Points and Segments (easy)(理解能力有待提高……)

题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; struct tt { int a,b; }aa[110]; int cmp(tt x,tt y) { return x.a<