【CodeForces - 983D】Arkady and Rectangles

本文介绍了一道编程题目,涉及到在无限平面上绘制n个不同颜色的矩形,之后计算能观察到多少种不同颜色。通过离线处理、扫描线算法和线段树来解决此问题,最终实现复杂度为O(nlog^2n)的解决方案。
摘要由CSDN通过智能技术生成


@Description - English@

Arkady has got an infinite plane painted in color 0. Then he draws n rectangles filled with paint with sides parallel to the Cartesian coordinate axes, one after another. The color of the i-th rectangle is i (rectangles are enumerated from 1 to n in the order he draws them). It is possible that new rectangles cover some of the previous ones completely or partially.

Count the number of different colors on the plane after Arkady draws all the rectangles.

Input
The first line contains a single integer n (1≤n≤100000) — the number of rectangles.

The i-th of the next n lines contains 4 integers x1, y1, x2 and y2 (−109≤x1<x2≤109, −109≤y1<y2≤109) — the coordinates of corners of the i-th rectangle.

Output
In the single line print the number of different colors in the plane, including color 0.

Examples
input
5
-1 -1 1 1
-4 0 0 4
0 0 4 4
-4 -4 0 0
0 -4 4 0
output
5

input
4
0 0 4 4
-4 -4 0 0
0 -4 4 0
-2 -4 2 4
output
5

Note
That’s how the plane looks in the first sample
题目描述图1
That’s how the plane looks in the second sample
题目描述图2
0 = white, 1 = cyan, 2 = blue, 3 = purple, 4 = yellow, 5 = red.

@Decription - Chinese@

给一个二维平面上涂颜色。每次涂一个完整的矩形。后涂的会覆盖掉先涂的。

问这样涂了 n 种颜色后,有几种颜色可以被看见。( n <= 10^5 )

注:平面无限大,且一开始覆盖着白色。

@Solution@

算法略暴力的一道题qwq。

为了把这个二维问题降为一维的,我们考虑 离线 + 扫描线 ,并用一个线段树维护扫描线。

怎么维护呢?先给每种颜色编号,为涂上颜色的次序。编号大的颜色会覆盖编号小的。然后,对于当前的扫描线,我们找出一个能被看到的颜色,把这个颜色打上标记;再找一个没有被打上标记的,能被看到的颜色,把它打上标记……重复若干次,直到当前扫描线上所有的颜色都被打上标记后,向前移动扫描线。

然后就是怎么实现的问题:
显然编号越大越容易被看见。所以对于线段树上的每个区间,我们定义 mx 表示这个区间能被看到的,编号最大的,没有被打上标记的颜色编号。为了方便维护 mx,我们再给每个区间定义 mn 表示这个区间能被看到的,编号最小的颜色编号;(最暴力的一步)定义一个 set 存储能够完全覆盖这个区间的颜色集合。

mx 可以这样维护: m x = max ⁡ ( s e t m a x , max ⁡ ( m x l e f t , m x r i g h t ) ) mx = \max(set_{max}, \max(mx_{left}, mx_{right})) mx=max(setmax,max(mxleft,mxright))
因为 set 中的最大颜色会覆盖掉 set 中的其他颜色,所以可以直接取最大值。
如果这个 set 中的最大颜色比左右子树中的最大颜色都大,则这个区间就肯定只能看到这一种颜色;否则就可以取左右子树中的较大值。
如果发现这个 mx 已经标记过了,则令 mx = -1;这样就不会影响上一层的答案(因为是取较大值,而 -1 比所有颜色编号都小的,故不会被取)。

mn 可以这样维护: m n = max ⁡ ( s e t m a x , min ⁡ ( m n l e f t , m n r i g h t ) ) mn = \max(set_{max}, \min(mn_{left}, mn_{right})) mn=max(setmax,min(mnleft,mnright

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值