Domain: Algorithms & Data Structures

Steve is trapped in a tunnel filled with hostile creepers. There are n creepers standing in a straight line, indexed from 1 to n.

For each position i, the creeper at that position has an explosive power e_i.
We use the term “creepers” consistently throughout; all entities referred to are the same.

Explosion Rule:
If Steve detonates the creeper at position i, it explodes and kills every creeper at position j such that:
|i − j| < e_i

This condition is strict. Positions where |i − j| = e_i are NOT affected.
Equivalently, detonating creeper i kills exactly the creepers in the interval:
[i − e_i + 1, i + e_i − 1], inclusive.

If e_i = 0, the interval is empty, meaning such a creeper cannot be detonated at all.
Dead creepers cannot be detonated again.

Objective:
Steve must kill all creepers using as few detonations as possible.

Input:
Each test contains multiple test cases.
The first line contains an integer t, the number of test cases.

For each test case:
- The first line contains an integer n (2 ≤ n ≤ 5·10^5), the number of creepers.
- The second line contains n integers e_1, e_2, …, e_n (0 ≤ e_i ≤ n), where e_i is the explosive power of the i-th creeper.

It is guaranteed that the sum of n over all test cases does not exceed 5·10^5.

Output:
For each test case:
- Output −1 if it is impossible to kill all creepers.
- Otherwise, output:
  1) An integer k, the minimum number of detonations.
  2) A sequence of k integers denoting the positions of the creepers to detonate.

If multiple valid solutions exist, any one may be printed.

Worked Example (Explosion Mechanics):

If n = 7 and e = [0, 2, 0, 3, 0, 0, 0]:

Detonating creeper at position 4 (e_4 = 3) kills creepers at positions:
|4 − j| < 3  →  j ∈ {2, 3, 4, 5, 6}

So the affected interval is [2, 6].


Grading rubric:
criterion: Does the answer explicitly define the detonation interval of creeper i as [i − e_i + 1, i + e_i − 1]?
weight: 10
criterion: Does the answer state that two creepers cannot both be detonated if one lies inside the other’s detonation interval ?
weight: 10
criterion: Does the answer define dp[i] as the minimum detonations needed to kill all creepers up to position (i + e_i − 1) with creeper i being the rightmost detonated creeper?
weight: 10
criterion: Does the answer list Case 1 transition conditions exactly as j + e_j − 1 < i and i − e_i + 1 ≤ j + e_j?
weight: 10
criterion: Does the answer list Case 2 transition conditions exactly as j < i − e_i + 1 and i − e_i + 1 ≤ j + e_j?
weight: 10
criterion: Does the answer state that dp[i] is computed using exactly two range minimum queries: [i − e_i, i − 1] and [i − e_i, i + e_i − 1]?
weight: 10
criterion: Does the answer state the dp formula dp[i] = 1 + min(query1, query2)?
weight: 10
criterion: Does the answer specify the exact point update rule segtree[i + e_i − 1] = min(current value, dp[i]) after computing dp[i]?
weight: 10
criterion: Does the answer state that the final result equals min(dp[i]) over all i satisfying i + e_i − 1 ≥ n, and outputs −1 if no finite dp[i] exists?
weight: 10
criterion: Does the answer state that reconstruction uses predecessor tracking and detonates the selected creepers in increasing order of explosive power e_i?
weight: 10

Answer:
Notation:
We use e_i to denote the explosive power of the creeper at position i, exactly as defined in the task prompt.

Detonation Interval:
Detonating creeper i kills all creepers in the interval:
[i − e_i + 1, i + e_i − 1]

Two creepers cannot both be detonated if their detonation intervals overlap in a way that one would destroy the other before it is detonated.

Dynamic Programming Definition:

Define:
dp[i] = the minimum number of detonations required to kill all creepers up to position (i + e_i − 1),
with creeper i being the rightmost detonated creeper.

Exact Transition Conditions:

For a transition from a previously detonated creeper j < i to creeper i, exactly two valid cases exist.

Case 1: Creeper i is strictly to the right of j’s detonation interval
j + e_j − 1 < i
i − e_i + 1 ≤ j + e_j

Case 2: Creeper j is strictly to the left of i’s detonation interval
j < i − e_i + 1
i − e_i + 1 ≤ j + e_j

Segment Tree Optimization:

A naive implementation of these transitions is O(n²).
To optimize, process creepers from left to right and maintain a segment tree where:
segtree[x] stores the minimum detonations needed to kill all creepers up to position x.
All values are initialized to infinity.

Exact Computation of dp[i]:

When processing creeper i, compute dp[i] using exactly two range minimum queries:

1) Range [i − e_i, i − 1]
2) Range [i − e_i, i + e_i − 1]

Then:
dp[i] = 1 + min(result of both queries)

Exact Point Update:

After computing dp[i], update:
segtree[i + e_i − 1] = min(current value, dp[i])

Final Answer:

The minimum number of detonations required is:
min over all i such that i + e_i − 1 ≥ n of dp[i]

If no such i exists with a finite dp[i], output −1.

Reconstruction and Order:

Store predecessor information during DP to reconstruct the detonated creepers.
Detonate the chosen creepers in increasing order of explosive power e_i to ensure no detonation kills a later one.

Complexity:
Time Complexity: O(n log n)
Space Complexity: O(n)