Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Consider the sum of the count of distinct values of subarrays ending with index <code>i</code>, let’s call it <code>sum</code>. Now if you need the sum of all subarrays ending with index <code>i + 1</code> think how it can be related to <code>sum</code> and what extra will be needed to add to this.
You can find that extra sum using the segment tree.