Nene vs. Monsters (Hard Version)
Contest: Codeforces Round 939 (Div. 2)
Difficulty rating: 2700
Tags: brute force, greedy, implementation, math

This is the hard version of the problem. The only difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if both versions of the problem are solved.

Nene is fighting with $$$n$$$ monsters, located in a circle. These monsters are numbered from $$$1$$$ to $$$n$$$, and the $$$i$$$-th ($$$1 \le i \le n$$$) monster's current energy level is $$$a_i$$$.

Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one:

- The $$$1$$$-st monster attacks the $$$2$$$-nd monster;
- The $$$2$$$-nd monster attacks the $$$3$$$-rd monster;
- $$$\ldots$$$
- The $$$(n-1)$$$-th monster attacks the $$$n$$$-th monster;
- The $$$n$$$-th monster attacks the $$$1$$$-st monster.

When the monster with energy level $$$x$$$ attacks the monster with the energy level $$$y$$$, the energy level of the defending monster becomes $$$\max(0, y-x)$$$ (the energy level of the attacking monster remains equal to $$$x$$$).

Nene is going to use this spell $$$10^{100}$$$ times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell $$$10^{100}$$$ times.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of test cases follows.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of monsters.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the current energy levels of monsters.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case,

- in the first line output an integer $$$m$$$ — the number of monsters with non-zero energy level after $$$10^{100}$$$ uses of the spell;
- in the second line of output $$$m$$$ integers $$$i_1,i_2,\ldots,i_m$$$ ($$$1 \le i_1 < i_2 < \ldots < i_m \le n$$$) — the indices of these monsters in the increasing order.

If $$$m=0$$$, you may either output an empty line or don't output it.

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

Note:
In the first test case, the following actions happen during the first $$$3$$$ uses of the spell in this order:

- Nene uses the Attack Your Neighbour spell for the first time;
- the $$$1$$$-st monster attacks the $$$2$$$-nd monster, after the attack the energy level of the $$$2$$$-nd monster becomes equal to $$$\max(0, 5-2)=3$$$;
- the $$$2$$$-nd monster attacks the $$$3$$$-rd monster, after the attack the energy level of the $$$3$$$-rd monster becomes equal to $$$\max(0, 3-3)=0$$$;
- the $$$3$$$-rd monster attacks the $$$1$$$-st monster, after the attack the energy level of the $$$1$$$-st monster becomes equal to $$$\max(0, 2-0)=2$$$;
- Nene uses the Attack Your Neighbour spell for the second time;
- the $$$1$$$-st monster attacks the $$$2$$$-nd monster, after the attack the energy level of the $$$2$$$-nd monster becomes equal to $$$\max(0, 3-2)=1$$$;
- the $$$2$$$-nd monster attacks the $$$3$$$-rd monster, after the attack the energy level of the $$$3$$$-rd monster becomes equal to $$$\max(0, 0-1)=0$$$;
- the $$$3$$$-rd monster attacks the $$$1$$$-st monster, after the attack the energy level of the $$$1$$$-st monster becomes equal to $$$\max(0, 2-0)=2$$$;
- Nene uses the Attack Your Neighbour spell for the third time;
- the $$$1$$$-st monster attacks the $$$2$$$-nd monster, after the attack the energy level of the $$$2$$$-nd monster becomes equal to $$$\max(0, 1-2)=0$$$;
- the $$$2$$$-nd monster attacks the $$$3$$$-rd monster, after the attack the energy level of the $$$3$$$-rd monster becomes equal to $$$\max(0, 0-0)=0$$$;
- the $$$3$$$-rd monster attacks the $$$1$$$-st monster, after the attack the energy level of the $$$1$$$-st monster becomes equal to $$$\max(0, 2-0)=2$$$.

After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the $$$1$$$-st monster has a non-zero energy level in the end.

In the second test case, both monsters initially have zero energy level.

Example input:
5
3
2 5 3
2
0 0
4
1 5 7 2
4
4 2 1 2
13
1 1 4 5 1 4 1 9 1 9 8 1 0

Example output:
1
1 
0

1
1 
2
1 3 
6
1 3 6 8 10 12

Editorial:
Hint 1
If three consecutive monsters have an energy level $$$0,x,y\ (x,y>0)$$$, the monster with an energy level $$$y$$$ will "die"(have an energy level $$$0$$$) at last.
Hint 2
If four consecutive monsters have energy levels $$$0,x,y,z\ (x,y,z>0)$$$, what will happen to the monster $$$z$$$?
Hint 3
If four consecutive monsters have an energy level $$$x,y,z,w\ (x,y,z,w>0)$$$, how many rounds of spells must be used to make at least one of these monsters die?
Solution
If four consecutive monsters have an energy level $$$x,y,z,w\ (x,y,z,w>0)$$$ and they did not die after $$$t$$$ rounds of spells, then $$$y$$$ will receive at least $$$t$$$ points of damage, $$$z$$$ will receive at least $$$(t-1) +(t-2)+\cdots=O(t^2)$$$ of damage, and $$$w$$$ will receive at least $$$O(t^3)$$$ of damage.
That is to say, let $$$V=\max_{i=1}^n a_i$$$, after $$$O(\sqrt[3]{V})$$$ rounds, at least one of $$$x,y,z,w$$$ will die.
So, we can simulate the process by brute force until there are no four consecutive alive monsters, and then the problem is reduced to the one described in Hint 2.
If four consecutive monsters have an energy level $$$0,x,y,z\ (x,y,z>0)$$$, $$$x$$$ will remain alive, $$$y$$$ will die at last and send $$$D=(y-x)+(y-2x)+\cdots+(y\bmod x)$$$ damage to $$$z$$$ before that. Therefore, $$$z$$$ will remain alive if and only if $$$z>D$$$.
The time complexity is $$$O(n\sqrt[3]{V})$$$.
Bonus: Actually, it can be shown that after $$$O(\sqrt[k]{V})$$$ rounds, there will be no $$$k$$$ consecutive alive monsters. Making $$$k$$$ bigger than $$$3$$$ can further reduce the time complexity, but it will be harder to implement and optimize little on actual performance.