Domain: Algorithms & Data Structures

## Problem

Shivam is playing a game with his friends Varad (who controls the doll) and Tanmay.

There are `n` boxes in a line, numbered `1..n`. Varad hides a doll in one box. Shivam will make `m` guesses: on the `i`-th guess Shivam asks whether the doll is currently in box `a[i]`.

Varad can “cheat” by moving the doll **before the first guess** and also **after every guess**:
- she may move the doll to a neighboring box (`i -> i-1` or `i -> i+1`) or keep it in place,
- moves must stay within `1..n`.

So the order is:
- game starts (doll at start box `x`),
- Varad makes one move,
- Shivam guesses `a1`,
- Varad moves,
- Shivam guesses `a2`,
- ...
- Shivam guesses `am`,
- Varad moves,
- game ends (doll at end box `y`).

Shivam **loses** (i.e., Varad wins) if **none** of Shivam’s guesses ever equals the doll’s position at that moment.

You are given `n`, `m`, and the guess sequence `a1..am`.  
Count the number of pairs `(x, y)` with `1 ≤ x, y ≤ n` such that Varad can choose moves so that:
- the doll starts at `x`,
- Shivam never guesses correctly,
- the doll ends at `y`.

### Input
- First line: `n m` (`1 ≤ n, m ≤ 1e5`)
- Second line: `m` integers `a1..am` (`1 ≤ ai ≤ n`)

### Output
Print the number of winning scenarios `(x,y)`.


Grading rubric:
criterion: The answer states that for each fixed start box x, the set of reachable end boxes y forms a consecutive segment [L[x], R[x]] (except trivial n=1 handling).
weight: 10
criterion: The answer states that the final count is computed as the sum over x of the segment lengths (e.g., sum max(0, R[x]-L[x]+1)).
weight: 10
criterion: The answer describes computing L[x] and R[x] as extreme reachable ends using a greedy strategy (always push left for L, always push right for R).
weight: 10
criterion: The answer states that the right-bound computation is obtained by a symmetric/mirrored procedure of the left-bound computation.
weight: 10
criterion: The answer uses a DSU (union-find) idea to merge starting positions whose greedy simulation leads to the same current box, so their future evolution is identical.
weight: 10
criterion: The answer specifies that at guess i, any simulated state that equals the forbidden box a[i] must be shifted to an alternative position to avoid losing immediately (e.g., forced move to a[i]+1 for the left-bound sweep).
weight: 10
criterion: The answer includes boundary handling for moves at boxes 1 and n (cannot move to 0 or n+1).
weight: 10
criterion: The answer explicitly describes that after processing guess i, the greedy-left sweep applies a move curPos := max(1, curPos−1) (or an equivalent uniform “try move left” update).
weight: 10
criterion: The answer states that when two DSU components move into the same current box during the sweep, they are unioned/merged.
weight: 10
criterion: The answer states an overall time complexity of O((n+m) α(n)) or O(n+m) (near-linear) for computing all bounds.
weight: 10

Answer:
### 1) Model as reachable positions with “forbidden” points at guess times

Let the doll’s position **before** guess `i` be `pos[i]` (so `pos[1]` is after Varad’s initial move).
Constraints:
- `pos[i] != a[i]` for all `i = 1..m` (otherwise Shivam guesses correctly).
- `pos[i+1]` is obtained from `pos[i]` by a move in `{ -1, 0, +1 }` staying in `[1..n]`.
- The final position after the last extra move is `y = pos[m+1]`.

For a fixed start `x`, the set of reachable end positions `y` (over Varad’s choices) is always a **consecutive segment** `[L[x], R[x]]` (for `n=1` it is trivial). Intuition: the movement graph is 1D and “holes” created by forbidding a single point at a time get filled again by the ±1 closure; reachability remains interval-like.

Therefore the required answer is:
\[
\sum_{x=1}^{n} \max(0, R[x]-L[x]+1).
\]

So we only need to compute, for every start `x`, the **leftmost** possible end `L[x]` and the **rightmost** possible end `R[x]`.

---

### 2) Compute extreme ends via greedy, but for all starts simultaneously

To get `L[x]` (leftmost possible end), consider an optimal strategy that always tries to move the doll as far **left** as possible, only deviating when that would place it on the next forbidden guess box. Symmetrically, `R[x]` is obtained by always trying to move as far **right** as possible.

Doing this independently for each `x` would be `O(nm)`, too slow. The key is that many starting points quickly become indistinguishable under the greedy process: once two starts lead to the same current position under the greedy policy, their future behavior (and thus final bound) is identical. We can merge such starts.

---

### 3) DSU “merge-starts-by-current-cell” sweep for the left bound

We process guesses from `i=1` to `m` and maintain groups of starting positions whose greedy-simulated doll position (just before guess `i`) is the same cell.

Data structure:
- A disjoint set union over starting indices `x=1..n`.
- For each DSU component, we store the current simulated position `curPos(comp)` (a box number).
- We also maintain for each box `v` which DSU component currently sits at `v` (if any), so if two components move into the same `v`, we union them.

Update rule per step `i` for left-bound simulation:
1. **Avoid the forbidden box.**  
   If some component currently has `curPos = a[i]`, then to keep losing, it must shift to the smallest possible alternative, i.e. to the right neighbor `a[i]+1` if it exists, otherwise the component becomes impossible (no valid path for those starts).
2. **Apply Varad’s move after guess `i` to prepare for the next step.**  
   Greedy-left move: every component tries to move `curPos := max(1, curPos-1)`.

Whenever step (1) or (2) moves a component into a box already occupied by another component, we union them (because from this point on, their greedy evolution is identical).

At the end (after processing guess `m` and then the final move), the value `curPos` of the component containing start `x` equals `L[x]`.

Boundary handling:
- Moves that would go to `0` stay at `1`.
- If step (1) requires moving from `n` to `n+1`, that component is marked “dead” (contributes 0 end positions).

This sweep is near-linear because each union happens at most `n-1` times and each component changes position only when forced by a guess or by the uniform greedy shift.

Compute `R[x]` with the symmetric sweep (mirror the line):
- replace each guess `a[i]` by `a'[i] = n+1-a[i]`,
- compute left bounds `L'` on the mirrored instance,
- map back: `R[x] = n+1 - L'[n+1-x]`.

---

### 4) Final counting

After obtaining arrays `L[1..n]` and `R[1..n]` (with “dead” starts contributing 0),
sum:
\[
\text{ans} = \sum_{x=1}^n \max(0, R[x]-L[x]+1).
\]

---

### 5) Complexity

Both sweeps are `O((n+m) α(n))` with DSU (effectively linear for constraints), and memory is `O(n+m)`.