Hard work of volunteers
Limits: 3 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Thousands of volunteers daily help Ukrainian soldiers perform their duties.
Zenyk is one such volunteer. He is engaged in transporting parcels along the front line. Frontline positions are numbered with integers from \(0\) to \(10^9\). Zenyk has already planned his route for today. Currently, he is at position \(0\), then he will go to position \(x_1\), then from there to position \(x_2\) and so on, until he finishes his work for today at position \(x_n\). Zenyk calls positions \(x_1\), \(x_2\), …, \(x_n\) key positions, but when traveling from one key position to another, he also visits all intermediate positions between them.
Zenyk knows about \(m\) parcels that need to be transported. The \(i\)-th parcel needs to be delivered from position \(a_i\) to position \(b_i\). Visiting a position, Zenyk picks up all parcels that need to be sent from this position and delivers all parcels he has already picked up that are addressed here. I wonder how many parcels Zenyk will be able to deliver today?
Input
The first line contains two integers separated by a space — \(n\) and \(m\) — the number of key positions and the number of parcels Zenyk knows about.
The second line contains \(n\) integers separated by a space — the numbers of key positions that Zenyk will visit.
The next \(m\) lines each contain two integers — \(a_i\) and \(b_i\) — where the \(i\)-th parcel needs to be delivered from and to.
Output
A single number — the number of parcels that Zenyk will deliver.
Constraints
\(1 \le n, m \le 10^5\),
\(0 \le x_i, a_i, b_i \le 10^9\), \(a_i \ne b_i\).
Problem evaluation consists of the following blocks:
1 point — example from the statement,
4 points — block of tests in which \(n, m, x_i, a_i, b_i \le 100\).
10 points — block of tests in which \(n, m \le 1000\).
10 points — block of tests without additional constraints.
You will receive points for a block only if you give the correct answer to all tests in the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 7 4 7 2 4 7 5 6 3 | 2 |
Notes
Initially, Zenyk will pick up the first parcel at position 2. At position 4, Zenyk will deliver the first parcel. Next, Zenyk will pick up the third and second parcels at positions 6 and 7, respectively. Heading to the next key position (numbered 4), Zenyk will be able to deliver the second parcel at position 5. On the way from position 4 to 7, there are no more parcels to pick up or deliver. Zenyk does not have an opportunity to deliver the third parcel.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Hard work of volunteers - Editorial
There are two options for the direction in which a parcel is sent: left or right. Consider the case when the parcel is sent to the right (\(a_i < b_i\)); the other one is solved similarly. To deliver such a parcel, we must first visit position \(a_i\) and then \(b_i\). Suppose we can deliver the parcel somehow, and consider the last time we were in cell \(a_i\) before delivering it. We must go right from this cell, that is, there must be a key position \(x_j\) followed by a position \(x_{j+1}\) with \(x_j \le a_i < x_{j+1}\). We do the same for \(b_i\), but consider the first visit to \(b_i\) after picking up the parcel at \(a_i\). Then we must have come to position \(b_i\) from the left, that is, there must be a moment \(x_k\) preceded by \(x_{k-1}\) with \(x_{k-1} < b_i \le x_k\). So if there are two indices \(j\) and \(k\) with \(x_j \le a_i\), \(b_i \le x_k\) and \(j < k\) (the conditions that we go right from \(a_i\), come to \(b_i\) from the left, and pick up the parcel before delivering it), the parcel can be delivered. If there are no such indices, we cannot deliver the parcel. If \(b_i < a_i\), we need to find indices \(j\) and \(k\) with \(a_i \le x_j\), \(x_k \le b_i\) and \(j < k\).
To check whether the required indices exist, we find the smallest \(j\) for which the condition between \(a_i\) and \(x_j\) holds (\(x_j \le a_i\) or \(a_i \le x_j\)). Similarly, we find the largest \(k\) for which the condition between \(b_i\) and \(x_k\) holds (\(b_i \le x_k\) or \(x_k \le b_i\)). If these indices exist and \(j < k\), the parcel can be delivered. This solution works in \(O(m \cdot n)\), since for each parcel we need to go over the index of the key position.
To speed it up, we sort the \(n\) pairs (\(x_{ind}\), \(ind\)); then each condition holds for some prefix or suffix of this array. Since we need the minimum and the maximum, we build an array storing, for each prefix and each suffix, the minimum and maximum of the second element of the pair. The prefix or suffix for which a condition holds can be found with binary search.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|