Red_Black trees. Dynamic statistics with renewing after adding each -a[i] going backward from the end. We store int d for each node balanced tree and for each subtree T(x). Here d[i]- number of sequences beggining from x with length i.
Re: How to solve this task? n*n*k=4e+9 - too long... (-)