Domain: Algorithms & Data Structures

## Scenario
A corridor contains `n` unstable modules placed in a straight line, indexed from `1` to `n`.  
Module `i` has an **activation radius** `a[i]`.

You may **manually trigger** a module only if it is still intact. Triggering module `i` instantly destroys every module `j` satisfying:

\[
|i - j| < a[i]
\]

Destroyed modules cannot be triggered later. Some modules are inert with `a[i] = 0`, which means they cannot be triggered at all.

Your goal is to destroy **all** modules using the **fewest manual triggers**, or determine that it cannot be done.

---

## Input
The first line contains an integer `t` — the number of test cases.

For each test case:
- The first line contains `n` — the number of modules.
- The second line contains `n` integers `a[1], a[2], ..., a[n]`.

### Constraints
- \(1 \le t \le 10^4\)
- \(2 \le n \le 5 \cdot 10^5\)
- \(0 \le a[i] \le n\)
- The sum of `n` across all test cases is at most \(5 \cdot 10^5\)

---

## Output
For each test case:

- Print `-1` if it is impossible to destroy all modules.

Otherwise print two lines:
1. An integer `k` — the minimum number of triggers.
2. `k` integers `p1 p2 ... pk` — a sequence of module indices to trigger, in the order you will trigger them.

If multiple optimal solutions exist, print any.


Grading rubric:
criterion: Does the response define the destroyed interval of triggering index i as Li = i - a[i] + 1 and Ri = i + a[i] - 1?
weight: 10
criterion: Does the response state that indices with a[i] = 0 cannot be triggered?
weight: 10
criterion: Does the response define dp[i] using Ri as the farthest-right destroyed position represented by state i?
weight: 10
criterion: Does the response specify Pattern A as a range-min query over endpoints x in [Li - 1, i - 1]?
weight: 10
criterion: Does the response specify Pattern B as a range-min query over endpoints x in [Li - 1, Ri] evaluated using only centers up to Li - 1?
weight: 10
criterion: Does the response include an offline scheduling mechanism that executes Pattern B queries at sweep time Li - 1?
weight: 10
criterion: Does the response perform a point update of the segment tree at position Ri with the value dp[i]?
weight: 10
criterion: Does the response select the final answer using min dp[i] over indices satisfying Ri >= n?
weight: 10
criterion: Does the response describe reconstruction using a predecessor pointer pre[i] obtained from the argmin returned by the segment tree query?
weight: 10
criterion: Does the response justify sorting the reconstructed triggers by nondecreasing a[i] using an adjacent-swap validity argument based on |x-y| >= a[x]?
weight: 10

Answer:
## Key transformation
Triggering module `i` (only possible when `a[i] > 0`) destroys exactly the interval:

- \(L_i = i - a[i] + 1\)
- \(R_i = i + a[i] - 1\)

So each trigger corresponds to a closed range \([L_i, R_i]\).

The difficulty is that a module cannot be triggered after it has been destroyed, so the final set of triggered indices must admit some valid triggering order.

---

## DP state (compressed)
We only keep DP states where the **rightmost-triggered index is also the one that reaches farthest right**.

Let:

- `dp[i]` = minimum number of triggers needed so that, after executing them in some valid order, **all modules in `[1..R_i]` are destroyed**, and among the triggers used:
  - `i` is the **largest index**
  - `R_i` is the **maximum right endpoint** achieved

If `a[i] = 0`, then `dp[i] = +∞` since `i` cannot be triggered.

---

## Two valid transition patterns into `i`
Let \(L_i = i - a[i] + 1\) and note \(L_i - 1 = i - a[i]\).

We want to combine an earlier solution (represented by some endpoint `x`) with triggering `i` to achieve coverage up to `R_i`.

### Pattern A: trigger `i` after the earlier plan
To ensure `i` is still alive when it is triggered, the earlier plan must not destroy position `i`, which is guaranteed if the earlier plan’s farthest endpoint `x` satisfies:

- \(x < i\)

To ensure everything up to `L_i - 1` is already destroyed before triggering `i`, we need:

- \(x \ge L_i - 1\)

So we need:

- \(x \in [L_i - 1,\; i - 1]\)

### Pattern B: trigger `i` before finishing the earlier plan
If we trigger `i` earlier, we are allowed to use triggers strictly left of `L_i` later (their centers are not destroyed by `i` because `i` only destroys from `L_i` onwards).  
So we can combine with an earlier plan that only uses centers in `[1..L_i-1]`, as long as that plan already guarantees destruction of `[1..L_i-1]`, meaning its endpoint `x` satisfies:

- \(x \ge L_i - 1\)

Also, we only need `x` up to `R_i` in this transition layer, because `dp[i]` stores states whose farthest-right reach is `R_i`:

- \(x \in [L_i - 1,\; R_i]\)

---

## Segment tree acceleration
Maintain a segment tree over endpoints `pos = 1..n`.

At any sweep moment, the segment tree stores at each `pos` the best pair:

- `(min_triggers_to_destroy_[1..pos], argmin_index)`

Whenever we have computed `dp[i]`, we perform a point update:

- `tree[R_i] = min(tree[R_i], (dp[i], i))`

This encodes: “there exists a plan with cost `dp[i]` whose farthest destroyed position is exactly `R_i` and whose representative rightmost trigger is `i`.”

### Getting transition values
For `dp[i]`, we need:

- `bestA = rangeMinQuery(L_i - 1, i - 1)` using all processed triggers up to `i-1`
- `bestB = rangeMinQuery(L_i - 1, R_i)` but **using only triggers with index ≤ (L_i - 1)**

To implement Pattern B, do it offline:
- For every `i`, schedule its Pattern B query to be executed at sweep time `time = L_i - 1`.
- Pattern A query can be executed at sweep time `time = i - 1`.

Let:
- `valA` be the numeric min from Pattern A query
- `valB` be the numeric min from Pattern B query

Then:
- `dp[i] = 1 + min(valA, valB)`
- If both are `+∞`, then `dp[i] = +∞`.

Base case:
- A plan of cost `0` that has destroyed nothing is represented by endpoint `0`.  
  Practically, initialize the structure so that querying includes the possibility of starting fresh when `L_i <= 1`, by treating `endpoint 0` as cost `0` (often implemented via shifting indices or a special-case).

---

## Choosing the optimal finish
The whole line is destroyed if some chosen trigger `i` satisfies:

- \(R_i \ge n\)

Answer cost:
- \(\min_{i: R_i \ge n} dp[i]\)

If this minimum is `+∞`, print `-1`.

---

## Reconstruction
Store a predecessor pointer for each `i`:
- `pre[i]` = the `argmin_index` returned by the query that attained `min(valA, valB)` for `dp[i]`
- If the best came from the base case, use `pre[i] = 0`

Pick `best_end` = an index `i*` with minimal `dp[i]` among those with `R_i >= n`, then follow `pre[]` backwards to list all triggered indices.

---

## Output order (valid triggering sequence)
After reconstructing the set of indices, output them in **nondecreasing `a[i]`**.

Justification by swap argument:
- Consider two consecutive triggers `x` then `y` in a valid sequence with `a[x] > a[y]`.
- Since `y` was alive when triggered, `x` did not destroy `y`, so \(|x-y| \ge a[x]\).
- Then \(|x-y| \ge a[x] > a[y]\), so `y` would not destroy `x` either.
- Swapping `x` and `y` preserves validity.
- Repeating removes all inversions, producing a valid order sorted by `a[i]`.

---

## Complexity
- Segment tree operations: \(O(n \log n)\) per test file total across all tests due to sum of `n`
- Memory: \(O(n)\)