codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1044A.md (5968B)


      1 Source: https://codeforces.com/problemset/problem/1044/A
      2 
      3 A. The Tower is Going Home
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 On a chessboard with a width of $10^9$ and a height of $10^9$, the rows are numbered from bottom to top from $1$ to $10^9$, and the columns are numbered from left to right from $1$ to $10^9$. Therefore, for each cell of the chessboard you can assign the coordinates $(x,y)$, where $x$ is the column number and $y$ is the row number.
     22 
     23 Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates $(1,1)$. But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number $10^9$).
     24 
     25 Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:
     26 
     27 * Vertical. Each of these is defined by one number $x$. Such spells create an infinite blocking line between the columns $x$ and $x+1$.
     28 * Horizontal. Each of these is defined by three numbers $x_1$, $x_2$, $y$. Such spells create a blocking segment that passes through the top side of the cells, which are in the row $y$ and in columns from $x_1$ to $x_2$ inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.
     29 
     30  ![](https://espresso.codeforces.com/90c70dd895c54815d19d5dc78635ba32c56114b6.png)  An example of a chessboard. 
     31 
     32 Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell $(r_0,c_0)$ into the cell $(r_1,c_1)$ only under the condition that $r_1 = r_0$ or $c_1 = c_0$ and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).
     33 
     34 Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!
     35 
     36 Input
     37 
     38 The first line contains two integers $n$ and $m$ ($0 \le n,m \le 10^5$) — the number of vertical and horizontal spells.
     39 
     40 Each of the following $n$ lines contains one integer $x$ ($1 \le x \lt 10^9$) — the description of the vertical spell. It will create a blocking line between the columns of $x$ and $x+1$.
     41 
     42 Each of the following $m$ lines contains three integers $x_1$, $x_2$ and $y$ ($1 \le x_{1} \le x_{2} \le 10^9$, $1 \le y \lt 10^9$) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number $y$, in columns from $x_1$ to $x_2$ inclusive.
     43 
     44 It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.
     45 
     46 Output
     47 
     48 In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell $(1,1)$ to at least one cell in the row with the number $10^9$
     49 
     50 Examples
     51 
     52 Input
     53 
     54 ```
     55 2 3  
     56 6  
     57 8  
     58 1 5 6  
     59 1 9 4  
     60 2 4 2
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 1
     67 ```
     68 
     69 Input
     70 
     71 ```
     72 1 3  
     73 4  
     74 1 5 3  
     75 1 9 4  
     76 4 6 6
     77 ```
     78 
     79 Output
     80 
     81 ```
     82 1
     83 ```
     84 
     85 Input
     86 
     87 ```
     88 0 2  
     89 1 1000000000 4  
     90 1 1000000000 2
     91 ```
     92 
     93 Output
     94 
     95 ```
     96 2
     97 ```
     98 
     99 Input
    100 
    101 ```
    102 0 0
    103 ```
    104 
    105 Output
    106 
    107 ```
    108 0
    109 ```
    110 
    111 Input
    112 
    113 ```
    114 2 3  
    115 4  
    116 6  
    117 1 4 3  
    118 1 5 2  
    119 1 6 5
    120 ```
    121 
    122 Output
    123 
    124 ```
    125 2
    126 ```
    127 
    128 Note
    129 
    130 In the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.
    131 
    132  ![](https://espresso.codeforces.com/79ba3e377dac6034a170469f1b79bf64978aede3.png) Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. 
    133 
    134 In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.
    135 
    136  ![](https://espresso.codeforces.com/7605a14fd36ffc8483622bcccb2053ffac4693c3.png) Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. 
    137 
    138 In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.
    139 
    140  ![](https://espresso.codeforces.com/8722835dc84cd3988660017beacd92a58254528f.png) Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. 
    141 
    142 In the fourth sample, we have no spells, which means that we do not need to remove anything.
    143 
    144 In the fifth example, we can remove the first vertical and third horizontal spells.
    145 
    146  ![](https://espresso.codeforces.com/6ff2f6d42f11a3f3dcea6f4f2fc7d1cc20557a06.png) Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.