4 lines C++
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {     int x = 0, y = 0, z = 0;     for (int a : A)         z += (++x *= a <= R) - (++y *= a < L);     re
    int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
        int x = 0, y = 0, z = 0;
        for (int a : A)
            z += (++x *= a <= R) - (++y *= a < L);
        return z;
    }

`x` counts the subarrays whose maximum is low enough.
`y` counts the subarrays whose maximum is too low.
`z` counts the subarrays whose maximum is just right, low enough but not too low.
[]