Subarrays Distinct Element Sum of Squares II Problem
Description
LeetCode Problem 2916.
You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
- Let
nums[i..j]be a subarray ofnumsconsisting of all the indices fromitojsuch that0 <= i <= j < nums.length. Then the number of distinct values innums[i..j]is called the distinct count ofnums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
Since the answer may be very large, return it modulo 10^9 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 + 2^2 + 2^2 + 2^2 = 15.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 1^2 + 1^2 + 1^2 = 3.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^5
Solution
Approach: Segment Tree With Range Add and Sum of Squares
Scan the right end r from left to right. Let f(l) be the number of distinct values in nums[l..r]. When nums[r] arrives and its previous occurrence is at index p (or -1), the value f(l) grows by 1 exactly for l in [p + 1, r]; for smaller l the value was already counted. Positions to the right of r are still 0.
So we need a structure with range add +1 and the sum of squares of all f(l). A segment tree with lazy propagation stores in every node the sum and the sum of squares of its range. Adding v to a node of length len changes them to sumSq + 2 * v * sum + v * v * len and sum + v * len. After each update, the root's sum of squares is the answer for all subarrays ending at r. Add these up over all r, modulo 10^9 + 7. A hash map keeps the last index of every value.
Complexity: O(n log n) time, O(n) space.
MOD = 10**9 + 7
class Solution:
def sumCounts(self, nums: List[int]) -> int:
n = len(nums)
class Node:
__slots__ = ("l", "r", "sum", "sum_sq", "lazy")
def __init__(self):
self.l = self.r = 0
self.sum = self.sum_sq = self.lazy = 0
tr = [Node() for _ in range(n << 2)]
def build(u: int, l: int, r: int) -> None:
tr[u].l, tr[u].r = l, r
if l == r:
return
mid = (l + r) >> 1
build(u << 1, l, mid)
build(u << 1 | 1, mid + 1, r)
def apply(u: int, v: int) -> None:
length = tr[u].r - tr[u].l + 1
tr[u].sum_sq += 2 * v * tr[u].sum + v * v * length
tr[u].sum += v * length
tr[u].lazy += v
def pushdown(u: int) -> None:
if tr[u].lazy:
apply(u << 1, tr[u].lazy)
apply(u << 1 | 1, tr[u].lazy)
tr[u].lazy = 0
def pushup(u: int) -> None:
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum
tr[u].sum_sq = tr[u << 1].sum_sq + tr[u << 1 | 1].sum_sq
def modify(u: int, l: int, r: int, v: int) -> None:
if tr[u].l >= l and tr[u].r <= r:
apply(u, v)
return
pushdown(u)
mid = (tr[u].l + tr[u].r) >> 1
if l <= mid:
modify(u << 1, l, r, v)
if r > mid:
modify(u << 1 | 1, l, r, v)
pushup(u)
build(1, 0, n - 1)
last = {}
ans = 0
for i, x in enumerate(nums):
p = last.get(x, -1)
modify(1, p + 1, i, 1)
last[x] = i
ans = (ans + tr[1].sum_sq) % MOD
return ans