Points and Segments

题目链接www.codeforces.com/contest/429/problem/E

注释:略。


题解

先离散化。

发现每个位置如果被偶数条线段覆盖的话那么这个位置上的线段两种颜色的个数是相等的也就是确定的。

如果是奇数的话,我们就强制再放一条线段覆盖这个位置,无论这条新加入线段的颜色,剩下的颜色个数也就确定了。

那么,每条线段只能有一种颜色,而且每个位置值为$0$。

我们想一个东西:欧拉回路。

发现欧拉回路其实要干的就是这事,所以我们暴力建图欧拉回路即可。

至于怎么想到欧拉回路这个东西.....看感觉吧......

代码

#include <bits/stdc++.h>

#define setIO(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout) 

#define N 1000010 

using namespace std;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int b[N]; struct Node {
int x, y;
}a[N]; int head[N], to[N << 1], nxt[N << 1], tot = 1, id[N << 1], val[N << 1]; int ans[N]; inline void add(int x, int y, int z) {
to[ ++ tot] = y;
id[tot] = z;
nxt[tot] = head[x];
head[x] = tot;
} bool vis[N]; int d[N]; void dfs(int p) {
vis[p] = true;
for (int i = head[p]; i; i = nxt[i]) {
if (!val[i]) {
val[i] = val[i ^ 1] = 1;
if (p < to[i]) {
ans[id[i]] = 1;
}
else {
ans[id[i]] = 0;
}
dfs(to[i]);
}
}
} int main() {
setIO("party");
int n = rd(), cnt = 0;
for (int i = 1; i <= n; i ++ ) {
a[i].x = rd(), a[i].y = rd() + 1;
b[ ++ cnt] = a[i].x, b[ ++ cnt] = a[i].y;
}
sort(b + 1, b + cnt + 1);
cnt = unique(b + 1, b + cnt + 1) - b - 1;
// cout << cnt << endl ;
for (int i = 1; i <= n; i ++ ) {
a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b;
a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b;
add(a[i].x, a[i].y, i), add(a[i].y, a[i].x, i);
d[a[i].x] ++ , d[a[i].y] ++ ;
} // for (int i = 1; i <= n; i ++ ) {
// printf("%d %d\n", a[i].x, a[i].y);
// } int pre = 0;
for (int i = 1; i <= cnt; i ++ ) {
if (d[i] & 1) {
if (!pre) {
pre = i;
}
else {
add(pre, i, 0), add(i, pre, 0);
d[i] ++ , d[pre] ++ ;
pre = 0;
}
}
} for (int i = 1; i <= cnt; i ++ ) {
if (!vis[i]) {
dfs(i);
}
} for (int i = 1; i <= n; i ++ ) {
printf("%d ", ans[i]);
}
fclose(stdin), fclose(stdout);
return 0;
}

[CF429E]Points ans Segments_欧拉回路的更多相关文章

  1. CF429E Points and Segments

    链接 CF429E Points and Segments 给定\(n\)条线段,然后给这些线段红蓝染色,求最后直线上上任意一个点被蓝色及红色线段覆盖次数之差的绝对值不大于\(1\),构造方案,\(n ...

  2. 【CF429E】Points and Segments 欧拉回路

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

  3. CF429E Points and Segments 构造、欧拉回路

    传送门 如果把一条线段\([l,r]\)看成一条无向边\((l,r+1)\),从\(l\)走到\(r+1\)表示线段\([l,r]\)染成红色,从\(r+1\)走到\(l\)表示线段\([l,r]\) ...

  4. nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...

  5. bzoj 2935 [Poi1999]原始生物——欧拉回路思路!

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2935 有向图用最小的路径(==总点数最少)覆盖所有边. 完了完了我居然连1999年的题都做不 ...

  6. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. [Swift]LeetCode948. 令牌放置 | Bag of Tokens

    You have an initial power P, an initial score of 0 points, and a bag of tokens. Each token can be us ...

  8. 【CF429E】Points and Segments(欧拉回路)

    [CF429E]Points and Segments(欧拉回路) 题面 CF 洛谷 题解 欧拉回路有这样一个性质,如果把所有点在平面内排成一行,路径看成区间的覆盖,那么每个点被从左往右的覆盖次数等于 ...

  9. 【CF429E】 Points and Segments(欧拉回路)

    传送门 CodeForces 洛谷 Solution 考虑欧拉回路有一个性质. 如果把点抽出来搞成一条直线,路径看成区间覆盖,那么一个点从左往右被覆盖的次数等于从右往左被覆盖的次数. 发现这个性质和本 ...

随机推荐

  1. Educational Codeforces Round 72 (Rated for Div. 2) A题

    Problem Description: You play your favourite game yet another time. You chose the character you didn ...

  2. 百度ueditor中复制word图文时图片转存任然保持灰色不可用

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  3. python3 操作ppt

    # pip install pywin32com# ppt太大会读取失败import win32com from win32com.client import Dispatch, constants ...

  4. 实战 Prometheus 搭建监控系统

    实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ...

  5. Irrlicht引擎剖析二

  6. P1169 [ZJOI2007]棋盘制作——悬线法

    ---恢复内容开始--- 给你一个矩阵,选出最大的棋盘,棋盘的要求是黑白相间(01不能相邻),求出最大的正方形和矩形棋盘的面积: 数据n,m<=2000; 这个一看就可能是n2DP,但是写不出. ...

  7. OpenGL+VS2010环境配置及遇到的问题

    OpenGL+VS2010+GLUT工具包+WIN10系统: 第一步,安装GLUT工具包 Windows环境下的GLUT下载地址:(大小约为150k) http://www.opengl.org/re ...

  8. CF1197A

    CF1197A 题意: 定义k阶梯子为两边各一块木板长度至少k+1,中间k块木板至少为1 .问 给你n块木板,最多能搭成几阶的梯子. 解法: 读题两小时,代码五分钟. 考虑贪心,构成梯子的两侧的木棍一 ...

  9. 通过xshell上传和下载文件

    参考:http://www.cnblogs.com/mingaixin/p/5588699.html

  10. Jmeter Web 性能测试入门 (四):一个小实例带你学会 Jmeter 脚本编写

    测试场景: 模拟并发100个user,在TesterHome 站内搜索VV00CC 添加线程组 添加HTTP信息头管理器 添加HTTP Sampler 填写HTTP Sampler中的信息 添加监听器 ...