HI! Recently I learned new technique I want to share which solve this type of problems
- You are given array $$$a$$$ and you want to do something with subarrays whose maximum or minimum element are Important to you somehow
The general method is to when we are solving for the whole array , we will divide the array from the maximum/minimum and solve the left and right part like any normal d&c but we will make the d&c return a data structure that is built on this section of the array only which will help us on getting the answer , the only remaining thing is to answer the about the subarrays whose maximum/minimum is the place we divided from it , so we need to consider the subarrays whose left part less than or equal to the index $$$i$$$ that we divide from and the right end of the subarray belong to the right part of the index $$$i$$$. we will use small to large merging here , we will identify which part is the smaller , $$$[l , i]$$$ or $$$[i , r]$$$ and we will brute force on this part while getting the answer from the data structure built on top of the other part , then merge the 2 data structure and return it , probably we must use pointers to avoid copy the data structure among the recursion calls.
Like recent div3 problem $$$F$$$ , You are given an array of integers $$$(a_1 , a_2 , ... , a_n)$$$ and two integers $$$(s , x)$$$ and we need to count the number of subarrays whose sum is $$$s$$$ and maximum is $$$x$$$.



