可以发现这个问题是存在边界的,那么我们可以先放宽一下条件思考一下没有边界的情况。

通过手玩可以发现,若不存在边界总是可以完成这个任务的。

因为两条曲线之间不存在交点,那么每次我们可以从空隙穿过一条直线然后一直绕道另一端。

那么存在边界的情况特殊在一条两个点都在边界上的曲线会将整个矩形划分成两个部分,而此时若两个部分还存在点没有连接就不合法了。

但是,如果这条曲线不全是两个端点都在边界上,那么我们先将其连接对两个端点都在边界上的曲线是不会影响的,一样可以通过缝隙绕道。

那么此时只需要考虑两个端点都在边界上的曲线了。

不难发现,此时合法当且仅当不存在一条曲线将矩形分成两个区域后存在两个端点在不同区域,即不存在两条有端点连成的线段会相交。

因为每条线段都只存在于边界上,可以考虑将二维的矩形变成一维的由边界展开变成的一条线段。

那么此时原图上的线段就会变成新线段上的若干个区间。

可以发现合法的条件当且仅当这些区间只有包含和相离关系。

于是只需要从左往右扫一遍开个栈做一遍括号匹配即可。

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
const int N = 2e5 + 5;
struct node { int x, y, id;} a[N];
int n, m, h, w, x, y, xx, yy, top, st[N], book[N];
bool check(int x, int y) { return (!x || !y || (x == h) || (y == w));}
bool cmp(node a, node b) {
if(a.y == w) return (b.y < w || b.x > a.x);
if(a.x == h) return (b.y != w && (b.x < h || b.y < a.y));
if(!a.y) return (b.y != w && b.x != h && (!b.x || b.x < a.x));
if(!a.x) return (b.y != w && b.x != h && b.y && b.y > a.y);
}
int main () {
cin >> h >> w >> m;
rep(i, 1, m) {
cin >> x >> y >> xx >> yy;
if(!check(x, y) || !check(xx, yy)) continue ;
a[++n] = (node){x, y, i}, a[++n] = (node){xx, yy, i};
}
sort(a + 1, a + n + 1, cmp);
rep(i, 1, n) {
if(book[a[i].id]) {
if(a[st[top]].id != a[i].id) { puts("NO"); return 0;}
else --top;
}
else st[++top] = i, book[a[i].id] = 1;
}
puts("YES");
return 0;
}

可以发现,先放宽条件是一个当条件非常多的时候对一道题下手的一个良好方法。

其次,当某些平面 / 三维空间上只涉及边界时,可以考虑降维将边界拆成一条连续的线段然后在序列上完成这个问题。

AT2644 [ARC076C] Connected?的更多相关文章

  1. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  2. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  3. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  4. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  5. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

  6. POJ1737 Connected Graph

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  7. [LintCode] Find the Weak Connected Component in the Directed Graph

      Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...

  8. Supporting Connected Routes to Subnet Zero

    Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...

  9. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

随机推荐

  1. Java代码性能优化

    (1)在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 控制资源的使用,通过线程同步来控制资源的并 ...

  2. 【C++】leetcode竞赛笔记

    *注--代码非博主本人所写,仅供学习参考,侵删 20200516双周赛: 1 to_string(int a) 将a转换成字符串形式 2 gcd(int a,int  b) 返回a,b的最大公约数,若 ...

  3. Least Angle Regression

    目录 引 一些基本的假设 LARS算法 算法 与别的方法结合 LARS与LASSO的关系 LARS 与 Stagewise 代码 Efron B, Hastie T, Johnstone I M, e ...

  4. 编写Java程序,使用Set实现不重复添加用户

    返回本章节 返回作业目录 需求说明: 在控制台输入用户信息,用户信息包括姓名.性别和年龄,将用户信息保存至User对象中. 将User对象保存至HashSet集合中. 规定如果两个User对象的姓名. ...

  5. 从零开始学springboot-2.配置项目

    ### 配置项目 #### 将application.properties改名为application.yml #### 在resources文件夹中(和上面那个配置文件同一路径下)新建一个文件app ...

  6. golang 算法题 : 二维数组搜索值

    package mainimport "fmt"func main() { matrix := [][]int{ {1, 4, 7, 11, 15}, {2, 5, 8, 12, ...

  7. 我踩过的python的坑

    1. string中Template用法 变量名不能是${tradeDate+1}, python无法识别其为变量,应改为 ${tradeDate1} 变量替换的语句:data_new = Templ ...

  8. 官方文档粗读 - Tutorial

    参考: https://www.jianshu.com/p/0d234e14b5d3 1.Connecting 我们通过 create_engine() 来链接数据库,假设我们我们采用SQLite. ...

  9. 实验 7 : OpenDaylight 实验 —— Python 中的 REST API 调用

    实验 7 : OpenDaylight 实验 -- Python 中的 REST API 调用 一.实验 目的 对 Python 调用 OpenDaylight 的 REST API 方法有初步了解. ...

  10. PPT制作手机手指滑动效果

    原文链接:https://www.toutiao.com/i6495304998786695694/ 上一节我们完成了手机滑动粗糙效果,这部分我们将给动画添加一个手指的图片. 首先,选择"插 ...