Ridiculously Simple O(n)/O(1) Java Solution based on State Machine
I literally submitted this in the last 10 seconds left in the contest and I'm surprised I didn't miss any corner cases, lol... Imagine you have a state machine.
I literally submitted this in the last 10 seconds left in the contest and I'm surprised I didn't miss any corner cases, lol...

Imagine you have a state machine. You really only have the following two states:
1 - Since seeing the last  >R element, you have not yet seen an [L,R]-element.   (call "haveYet = false")
2 - Since seeing the last  >R element, you have seen at least one or more [L,R]-element.   (call "haveYet = true")

We initialize like this:  we pretend A has a >R element at index -1 (imaginary index) and we are walking first at index 0, and we are in state (1).

If we see a new  >R  element, we transition to state (1) no matter whether we are currently in state (1) or state (2).  Save the position of this as 'LastGR' (stands for last index greater than R).
If we see a new  [L,R]-element, we transition from (1)-->(2) and mark this as the 'lastViable'.

Now, when we are on index i, we want to see if we can use this as an end index for ALL subarrays that could possibly end at this position. We are only able to do this if we are in state (2) AND the subarray encapsulates at least the last [L,R]-element we have seen. So, for a start index, everything since the position  lastGR+1  up to and including the lastViable (last [L,R]-element we saw)  is a possible starting position. We add those to our global running sum.


```
class Solution {
    public int numSubarrayBoundedMax(int[] A, int L, int R) {

        int lastGR = -1;
        int sum = 0;
        boolean haveYet = false;
        int lastViable = -1;

        for (int i = 0; i < A.length; i++) {
            if (A[i] > R) { lastGR = i; haveYet = false; continue; }
            if (A[i] >= L && A[i] <= R) { lastViable = i; haveYet = true; }
            if (haveYet) {
                sum += lastViable - lastGR;
            }
        }
        return sum;
    }
}
```
[]