牛客第六场 H-Hopping Rabbit

给出平面上的n个矩形,一只兔子从(x0+0.5,y0+0.5)(x_0+0.5,y_0+0.5)(x0​+0.5,y0​+0.5)出发,每一次可以平行于x轴或y轴跳跃d的距离,求出一个初始位置使得不管怎么跳都不会跳入到矩形中。
数据范围n,d<=1e5,平面坐标在-1e9到1e9当中

容易想到转化,变为了这样的问题:在一个d*d的矩阵当中进行矩形覆盖,求出最后有没有点没有被覆盖到,如果存在则求出它。

但是这个问题卡住了,最后没有想出来做法。后面听说了线段树的解决方法,觉得这类型的方法值得去研究一下。

对于每个矩形,取出其上边和下边,然后从下往上遍历,遇到一个下边,便在线段树的(x1, x2)区间进行+1;遇到一个上边,边-1。在操作之后,如果线段树的最小值的0的话,那么说明这一行上有一个空格,这里就可以作为初始位置。

这个题有点码,不过都是比较基础的,还算是好写。

#include <bits/stdc++.h>
using namespace std;const int N = 1e5 + 10, inf = 1e9;
int n, k, tot = 0;struct trap
{int x1, y1, x2, y2;
};
trap a[N << 2];struct line
{int y, l, r;bool flag;
};
line b[N << 3];trap build(int x1, int y1, int x2, int y2)
{trap temp;temp.x1 = x1;temp.y1 = y1;temp.x2 = x2;temp.y2 = y2;return temp;
}line buildline(int y, int l, int r, bool flag)
{line temp;temp.y = y;temp.l = l;temp.r = r;temp.flag = flag;return temp;
}inline int mo(long long x)
{x = (x + (long long)k * inf) % k;if (x == 0) x = k;return x;
}struct tree
{int l, r, mn, lazy;
};
tree t[N << 2];void buildtree(int l, int r, int x)
{t[x].l = l; t[x].r = r;t[x].mn = t[x].lazy = 0;if (l == r) return;int mid = (l + r) >> 1;buildtree(l, mid, x << 1);buildtree(mid + 1, r, x << 1 | 1);return;
}void pushdown(int x)
{t[x << 1].mn += t[x].lazy;t[x << 1].lazy += t[x].lazy;t[x << 1 | 1].mn += t[x].lazy;t[x << 1 | 1].lazy += t[x].lazy;t[x].lazy = 0;
}int query(int l, int r, int x)
{if (l <= t[x].l && t[x].r <= r)return t[x].mn;if (l > t[x].r || t[x].l > r)return inf;if (t[x].lazy != 0)pushdown(x);return min(query(l, r, x << 1), query(l, r, x << 1 | 1));
}void change(int l, int r, int x, int d)
{if (l <= t[x].l && t[x].r <= r){t[x].mn += d;t[x].lazy += d;return;}if (l > t[x].r || t[x].l > r)return;if (t[x].lazy != 0)pushdown(x);change(l, r, x << 1, d);change(l, r, x << 1 | 1, d);t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
}bool cmp(const line &a, const line &b)
{return a.y < b.y;
}int main()
{//freopen("in.txt", "r", stdin);ios::sync_with_stdio(false);cin.tie(0);int T = 1;//cin >> T;while (T --){bool flag = false;cin >> n >> k;buildtree(1, k, 1);for (int i = 1; i <= n; i ++){int x1, y1, x2, y2;cin >> x1 >> y1 >> x2 >> y2;if (y2 - y1 >= k && x2 - x1 >= k)flag = true;else{if (y2 - y1 >= k){x1 = mo(x1); x2 = mo(x2 - 1);if (x1 <= x2)a[++ tot] = build(x1, 1, x2, k);else{a[++ tot] = build(x1, 1, k, k);a[++ tot] = build(1, 1, x2, k);}}else if (x2 - x1 >= k){y1 = mo(y1); y2 = mo(y2 - 1);if (y1 <= y2)a[++ tot] = build(1, y1, k, y2);else{a[++ tot] = build(1, y1, k, k);a[++ tot] = build(1, 1, k, y2);}}else{x1 = mo(x1); x2 = mo(x2 - 1); y1 = mo(y1); y2 = mo(y2 - 1);if (x1 <= x2){if (y1 <= y2)a[++ tot] = build(x1, y1, x2, y2);else{a[++ tot] = build(x1, y1, x2, k);a[++ tot] = build(x1, 1, x2, y2);}}else{if (y1 <= y2){a[++ tot] = build(x1, y1, k, y2);a[++ tot] = build(1, y1, x2, y2);}else{a[++ tot] = build(x1, y1, k, k);a[++ tot] = build(1, y1, x2, k);a[++ tot] = build(x1, 1, k, y2);a[++ tot] = build(1, 1, x2, y2);}}}}}if (flag){cout << "NO";return 0;}for (int i = 1; i <= tot; i ++){b[i * 2 - 1] = buildline(a[i].y1, a[i].x1, a[i].x2, 1);b[i * 2] = buildline(a[i].y2 + 1, a[i].x1, a[i].x2, 0);}sort (b + 1, b + 2 * tot + 1, cmp);int p = 1;for (int i = 1; i <= k; i ++){while (b[p].y == i){if (b[p].flag)change(b[p].l, b[p].r, 1, 1);elsechange(b[p].l, b[p].r, 1, -1);p ++;}int mn = t[1].mn;if (mn == 0){for (int j = 1; j <= k; j ++){if (query(j, j, 1) == 0){cout << "YES\n";cout << j << " " << i;return 0;}}}}cout << "NO";}return 0;
}

牛客第六场 H-Hopping Rabbit相关推荐

  1. 线段树 ---- 牛客多校2021多校第6场 H Hopping Rabbit 扫描线

    思维误区:扫描线 扫描线模板里面有两个变量一个是cover和lengthcover和lengthcover和length covercovercover是标记下面是否覆盖满了,但是covercover ...

  2. 【2021牛客暑期多校训练营6】H Hopping Rabbit(扫描线)

    H Hopping Rabbit 题意: #include<bits/stdc++.h> using namespace std; const int maxn = 5e5+10;//扫描 ...

  3. 【2020年牛客暑假第九场】E题 Groundhog Chasing Death

    [2020年牛客暑假第九场]E题 Groundhog Chasing Death 质因子分解 题意 思路 方法一:先枚举iii再枚举公共质因子 Code(286ms) 方法二:先枚举公共质因子再枚举i ...

  4. 牛客小白月赛2 H.武

    牛客小白月赛2 H.武 题目链接 题目描述 其次,Sεlιнα(Selina) 要进行体力比武竞赛. 在 Sεlιнα 所在的城市,有 NNN 个街区,编号为 1∼N1 \sim N1∼N,总共有 N ...

  5. 牛客第三场多校 H Diff-prime Pairs

    链接:https://www.nowcoder.com/acm/contest/141/H 来源:牛客网 Eddy has solved lots of problem involving calcu ...

  6. 牛客小白月赛6 H 挖沟

    H 挖沟 题目: 链接:https://www.nowcoder.com/acm/contest/136/H 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144 ...

  7. 牛客小白月赛12 H 华华和月月种树 (离线dfs序+线段树)

    链接:https://ac.nowcoder.com/acm/contest/392/H 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 131072K,其他语言2621 ...

  8. CDMA(牛客第八场构造题)

    链接:https://ac.nowcoder.com/acm/contest/888/C 来源:牛客网 Gromah and LZR have entered the third level. The ...

  9. Gemstones(牛客第八场多校)

    链接:https://ac.nowcoder.com/acm/contest/888/G 来源:牛客网 Gromah and LZR have entered the seventh level. T ...

最新文章

  1. linux 安装x11 apt-get,Mac 安装apt-get
  2. 深入理解Javascript闭包
  3. JavaScript总结摘要
  4. 最佳开源大数据工具-2015
  5. 入门OJ 3172【导游】
  6. 一次排查服务器挖矿病毒
  7. 基于python的税额计算器
  8. CPU与GPU协同工作
  9. 大学计算机课程日记,大学计算机实习日记.docx
  10. .Net开源框架列表
  11. 三维重建 几何方法 深度学习_基于深度学习的三维重建算法综述
  12. win 10 读写EFI分区
  13. gif验证码识别,gif动态验证码识别
  14. 基于Spark的案例:同义词识别
  15. Android蓝牙开发的各种坑
  16. 两点三次埃尔米特插值法
  17. Outlook邮箱设置多个别名
  18. 中华云盒M1刷Linux教程,N1 盒子刷最新版 armbian 及软件安装
  19. C++ 贪心算法 摇摆序列
  20. expect ‘:‘ at 0, actual =] with root

热门文章

  1. 原生Get请求和Post请求
  2. Python当中的a += a 与 a = a + a 的区别,可变类型与不可变类型的数据类型,引用传参...
  3. Codeforces732D Exams
  4. SEO优化之一步一步诊断网站
  5. PTA-1011——World Cup Betting
  6. 一年的天数 Exercise06_16
  7. 登录plsql developer时候出现连接串问题导致的下拉列表中没有出现tnsnames.ora文件中配置的那些服务...
  8. 03-29复利计算单元测试
  9. 验证视图状态MAC失败问题正确的解决办法
  10. IHttpHandler 介绍演示(from 张子阳)