Lanterns
Contest: Educational Codeforces Round 103 (Rated for Div. 2)
Difficulty rating: 3000
Tags: binary search, data structures, dp

There are $$$n$$$ lanterns in a row. The lantern $$$i$$$ is placed in position $$$i$$$ and has power equal to $$$p_i$$$.

Each lantern can be directed to illuminate either some lanterns to the left or some lanterns to the right. If the $$$i$$$-th lantern is turned to the left, it illuminates all such lanterns $$$j$$$ that $$$j \in [i - p_i, i - 1]$$$. Similarly, if it is turned to the right, it illuminates all such lanterns $$$j$$$ that $$$j \in [i + 1, i + p_i]$$$.

Your goal is to choose a direction for each lantern so each lantern is illuminated by at least one other lantern, or report that it is impossible.

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases.

Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of lanterns.

The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \le p_i \le n$$$) — the power of the $$$i$$$-th lantern.

The sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output format:
For each test case, print the answer as follows:

If it is possible to direct all lanterns so that each lantern is illuminated, print YES in the first line and a string of $$$n$$$ characters L and/or R (the $$$i$$$-th character is L if the $$$i$$$-th lantern is turned to the left, otherwise this character is R) in the second line. If there are multiple answers, you may print any of them.

If there is no answer, simply print NO for that test case.

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

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

Example output:
YES
RRLLLLRL
YES
RL
YES
RL
NO

Editorial:
The main idea of the solution is to calculate the following dynamic programming: $$$dp_i$$$ is the maximum prefix we can fully cover with $$$i$$$ first lanterns.
Let's look at how can we solve it in $$$O(n^2)$$$ with this kind of dynamic programming. First of all, let's write it forward. Which transitions from $$$dp_i$$$ do we have?
• iterate on the lantern facing left that will cover the lantern $$$dp_i + 1$$$. Let this lantern be $$$j$$$. It should cover all lanterns in $$$[dp_i + 1, j - 1]$$$, so all lanterns from $$$[i, j)$$$ can be turned to the right (and we need a max query to determine the new covered prefix);
• if $$$dp_i > i$$$ (lantern $$$i$$$ is already covered), we can just extend the prefix by turning the $$$i$$$-th lantern to the right. Note that turning it to the right when it is not covered yet will be modeled by the first transition.
It is obviously $$$O(n^2)$$$, how can we optimize it? Let's write this dynamic programming backward. The second transition is changed to backward dp easily, what about the first one? Suppose we want to turn some lantern $$$i$$$ to the left. Let's iterate on the prefix $$$j$$$ that we will "connect" to it; for this prefix, $$$dp_j$$$ should be at least $$$i - p_i - 1$$$, and we update $$$dp_i$$$ with the maximum of $$$i - 1$$$ (since it is covered by lantern $$$i$$$) and the result of max query on $$$[j + 1, i - 1]$$$.
In fact, we need only one such prefix — the one with the minimum $$$j$$$ among those which have $$$dp_j \ge i - p_i - 1$$$. So, we build a minimum segment tree where each pair $$$(i, dp_i)$$$ is interpreted as the value of $$$i$$$ in position $$$dp_i$$$, and with min query on the suffix from $$$i - p_i - 1$$$ we find this optimal prefix, from which we should update (and to update, we can use any DS that allows max queries on segment — in my solution, it's another segment tree).