No.1920 Territory
Problem Statement
The territory of the yuki Kingdom is divided into several regions by $N+M$ fences.
When the territory of the yuki Kingdom is viewed on the $xy$-plane, the fences are represented as line segments. The first $N$ fences are parallel to the $x$-axis. Among these, the $i$-th fence connects the points $(b_i,a_i)$ and $(c_i,a_i)$ with a straight line. The remaining $M$ fences are parallel to the $y$-axis. Among these, the $i$-th fence connects the points $(p_i,q_i)$ and $(p_i,r_i)$ with a straight line.
Find the number of regions (of finite area) into which the territory is divided by these fences.
Definition of a region (click to expand)
Let $X$ be the set of points on the $xy$-plane that do not lie on any fence (line segment). For $2$ points $(a,b),(c,d)\in X$, define $(a,b)\sim(c,d)$ if there exists a curve connecting the $2$ points $(a,b),(c,d)$ that has no point in common with any fence (line segment); then $\sim$ is an equivalence relation on $X$. The classes obtained by classifying $X$ under this equivalence relation are called regions. See also the figures in the samples.In this problem, you need to find the number of regions of finite area, so the answer is $|X/\sim|-1$.
Input
$N$ $M$ $a_1$ $b_1$ $c_1$ $a_2$ $b_2$ $c_2$ $\vdots$ $a_N$ $b_N$ $c_N$ $p_1$ $q_1$ $r_1$ $p_2$ $q_2$ $r_2$ $\vdots$ $p_M$ $q_M$ $r_M$
- $1\leq N,M\leq 10^5$
- $1\leq a_i,b_i,c_i\leq 2\times 10^5$
- $1\leq p_i,q_i,r_i\leq 2\times 10^5$
- $a_i\neq a_j\ (i\neq j)$
- $b_i\lt c_i$
- $p_i\neq p_j\ (i\neq j)$
- $q_i\lt r_i$
- All input values are integers.
Output
Output the number of regions.
Sample
Sample 1
Input
4 4 1 1 3 2 1 3 3 2 4 4 1 4 1 1 3 2 1 3 3 1 4 4 1 4
Output
4
The territory is divided into $4$ regions as shown below.
Sample 2
Input
12 12 1 1 4 2 2 3 3 2 3 4 1 4 5 5 8 6 6 7 7 6 8 8 5 8 9 9 12 10 9 11 11 10 12 12 9 12 1 1 4 2 2 3 3 2 3 4 1 4 5 5 8 6 6 7 7 6 7 8 5 8 9 9 12 10 10 11 11 10 11 12 9 12
Output
7
The territory is divided into $7$ regions as shown below.
Sample 3
Input
4 5 1 2 5 2 3 4 4 2 4 5 1 5 1 1 5 2 1 4 3 2 3 4 2 4 5 1 5
Output
0
The territory is divided into $0$ regions as shown below.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
とりゐ
るさ
riano