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<<21);
    char Get() { return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++); }
    void Flush() { fwrite(obuf,1,oS-obuf,stdout),oS=obuf; }
    void Put(char x) { *oS++=x; if(oS==oT) Flush(); }
    int read(){int x=0,f=0;char ch=Get();while((ch>57||ch<48)&&ch!='-')ch=Get();if(ch=='-')f=1,ch=Get();while(ch>=48&&ch<=57)x=x*10+(ch^48),ch=Get();return f? -x:x;}
}
using namespace IO;
int l[N],r[N],o[N],deg[N],head[N],ver[N],Next[N],id[N],tot=1,ans[N],vis[N],used[N];
void add(int u,int v,int num){ver[++tot]=v,Next[tot]=head[u],id[tot]=num,head[u]=tot;}
void dfs(int u)
{
    vis[u]=1;
    for(int &i=head[u];i;i=Next[i])if(!used[i])used[i]=used[i^1]=1,ans[id[i]]=(u<ver[i]),dfs(ver[i]);
}
int main()
{
    int n=read(),m=0,i,las=0;
    for(i=1;i<=n;++i) o[++m]=l[i]=read(),o[++m]=r[i]=read()+1;
    sort(o+1,o+m+1),m=unique(o+1,o+m+1)-(o+1);
    for(i=1;i<=n;++i) l[i]=lower_bound(o+1,o+m+1,l[i])-o,r[i]=lower_bound(o+1,o+m+1,r[i])-o,add(l[i],r[i],i),add(r[i],l[i],i),++deg[l[i]],++deg[r[i]];
    for(i=1;i<=m;++i) if(deg[i]&1) if(las) add(las,i,0),add(i,las,0),++deg[i],++deg[las],las=0; else las=i;
    for(i=1;i<=m;++i) if(!vis[i]) dfs(i);
    for(i=1;i<=n;++i) Put(ans[i]+'0'),Put(' ');
    return Flush(),0;
}

原文地址:https://www.cnblogs.com/cjoierShiina-Mashiro/p/12235542.html

时间: 2024-08-30 16:33:29

CF429E Points and Segments的相关文章

【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就行了. 嗯...每

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<