@Arkady and Rectangles@
@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
That’s how the plane looks in the second sample
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