Hide and Seek
Contest: Forethought Future Cup - Final Round (Onsite Finalists Only)
Difficulty rating: 1500
Tags: graphs

Alice and Bob are playing a game on a line with $$$n$$$ cells. There are $$$n$$$ cells labeled from $$$1$$$ through $$$n$$$. For each $$$i$$$ from $$$1$$$ to $$$n-1$$$, cells $$$i$$$ and $$$i+1$$$ are adjacent.

Alice initially has a token on some cell on the line, and Bob tries to guess where it is.

Bob guesses a sequence of line cell numbers $$$x_1, x_2, \ldots, x_k$$$ in order. In the $$$i$$$-th question, Bob asks Alice if her token is currently on cell $$$x_i$$$. That is, Alice can answer either "YES" or "NO" to each Bob's question.

At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.

Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.

You are given $$$n$$$ and Bob's questions $$$x_1, \ldots, x_k$$$. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.

Let $$$(a,b)$$$ denote a scenario where Alice starts at cell $$$a$$$ and ends at cell $$$b$$$. Two scenarios $$$(a_i, b_i)$$$ and $$$(a_j, b_j)$$$ are different if $$$a_i \neq a_j$$$ or $$$b_i \neq b_j$$$.

Input format:
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n,k \leq 10^5$$$) — the number of cells and the number of questions Bob asked.

The second line contains $$$k$$$ integers $$$x_1, x_2, \ldots, x_k$$$ ($$$1 \leq x_i \leq n$$$) — Bob's questions.

Output format:
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
The notation $$$(i,j)$$$ denotes a scenario where Alice starts at cell $$$i$$$ and ends at cell $$$j$$$.

In the first example, the valid scenarios are $$$(1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5)$$$. For example, $$$(3,4)$$$ is valid since Alice can start at cell $$$3$$$, stay there for the first three questions, then move to cell $$$4$$$ after the last question.

$$$(4,5)$$$ is valid since Alice can start at cell $$$4$$$, stay there for the first question, the move to cell $$$5$$$ for the next two questions. Note that $$$(4,5)$$$ is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.

In the second example, Alice has no valid scenarios.

In the last example, all $$$(i,j)$$$ where $$$|i-j| \leq 1$$$ except for $$$(42, 42)$$$ are valid scenarios.

Example input:
5 3
5 1 4

Example output:
9

Example input:
4 8
1 2 3 4 4 3 2 1

Example output:
0

Editorial:
Another way to phrase this problem is to count the number of ordered pairs $$$(p,q)$$$ that satisfy the following
• $$$|p-q| \leq 1$$$
• There exists a number $$$i$$$ such that p doesn't appear in $$$x[0\ldots i]$$$ and $$$q$$$ doesn't appear in $$$x[i+1\ldots n]$$$.
There are only $$$O(n)$$$ pairs that satisfy the first condition, so we can loop over all these pairs and try to efficiently check the second condition.
If $$$p=q$$$, this pair is valid if $$$p$$$ doesn't appear in $$$x$$$, so we can just check this for all $$$1\ldots n$$$ directly with just one sweep through $$$x$$$.
For $$$p,q$$$ that differ by exactly one, we can check that the first occurrence of $$$p$$$ occurs after the last occurrence of $$$q$$$. This is because we can start at $$$p$$$, wait until the last question about $$$q$$$ has been asked, and then swap to $$$p$$$. This is guaranteed to avoid all of Bob's questions.
To answer this question efficiently, let's precompute the first and last occurrence of each number. This can be done by sweeping through the array once. Thus, checking if the condition is satisfied is just checking that $$$first[p] > last[q]$$$.
The total time complexity is $$$O(k)$$$ for pre-computation, and $$$O(n)$$$ for answering all the questions.