Number of Integers With Popcount-Depth Equal to K II Problem
Description
LeetCode Problem 3624.
You are given an integer array nums.
For any positive integer x, define the following sequence:
p_0 = xp_(i+1) = popcount(p_i)for alli >= 0, wherepopcount(y)is the number of set bits (1's) in the binary representation ofy.
This sequence will eventually reach the value 1.
The popcount-depth of x is defined as the smallest integer d >= 0 such that p_d = 1.
For example, if x = 7 (binary representation "111"). Then, the sequence is: 7 → 3 → 2 → 1, so the popcount-depth of 7 is 3.
You are also given a 2D integer array queries, where each queries[i] is either:
[1, l, r, k]- Determine the number of indicesjsuch thatl <= j <= rand the popcount-depth ofnums[j]is equal tok.[2, idx, val]- Updatenums[idx]toval.
Return an integer array answer, where answer[i] is the number of indices for the i^th query of type [1, l, r, k].
Example 1:
Input: nums = [2,4], queries = [[1,0,1,1],[2,1,1],[1,0,1,0]]
Output: [2,1]
Explanation:
i queries[i] nums binary(nums) popcount- depth [l, r] k Valid nums[j] updated nums Answer
0 [1,0,1,1] [2,4] [10, 100] [1, 1] [0, 1] 1 [0, 1] — 2
1 [2,1,1] [2,4] [10, 100] [1, 1] — — — [2,1] —
2 [1,0,1,0] [2,1] [10, 1] [1, 0] [0, 1] 0 [1] — 1
Thus, the final answer is [2, 1].
Example 2:
Input: nums = [3,5,6], queries = [[1,0,2,2],[2,1,4],[1,1,2,1],[1,0,1,0]]
Output: [3,1,0]
Explanation:
i queries[i] nums binary(nums) popcount- depth [l, r] k Valid nums[j] updated nums Answer
0 [1,0,2,2] [3, 5, 6] [11, 101, 110] [2, 2, 2] [0, 2] 2 [0, 1, 2] — 3
1 [2,1,4] [3, 5, 6] [11, 101, 110] [2, 2, 2] — — — [3, 4, 6] —
2 [1,1,2,1] [3, 4, 6] [11, 100, 110] [2, 1, 2] [1, 2] 1 [1] — 1
3 [1,0,1,0] [3, 4, 6] [11, 100, 110] [2, 1, 2] [0, 1] 0 [] — 0
Thus, the final answer is [3, 1, 0].
Example 3:
Input: nums = [1,2], queries = [[1,0,1,1],[2,0,3],[1,0,0,1],[1,0,0,2]]
Output: [1,0,1]
Explanation:
i queries[i] nums binary(nums) popcount- depth [l, r] k Valid nums[j] updated nums Answer
0 [1,0,1,1] [1, 2] [1, 10] [0, 1] [0, 1] 1 [1] — 1
1 [2,0,3] [1, 2] [1, 10] [0, 1] — — — [3, 2]
2 [1,0,0,1] [3, 2] [11, 10] [2, 1] [0, 0] 1 [] — 0
3 [1,0,0,2] [3, 2] [11, 10] [2, 1] [0, 0] 2 [0] — 1
Thus, the final answer is [1, 0, 1].
Constraints:
1 <= n == nums.length <= 10^51 <= nums[i] <= 10^151 <= queries.length <= 10^5queries[i].length == 3or4queries[i] == [1, l, r, k]or,queries[i] == [2, idx, val]0 <= l <= r <= n - 10 <= k <= 50 <= idx <= n - 11 <= val <= 10^15
Solution
Approach: One Fenwick Tree per Depth
The popcount-depth of any value up to 10^15 is at most 5, and it takes O(1) to compute: depth(x) = 1 + depth(popcount(x)), where the popcount is at most 50 and its depth comes from a small table, with depth(1) = 0.
Keep six Fenwick trees (binary indexed trees), one for each depth 0..5; tree d has a 1 at index j when nums[j] has depth d. A query [1, l, r, k] is a prefix-sum difference on tree k. An update [2, idx, val] removes the old depth of nums[idx] from its tree, stores val, and adds the new depth to its tree.
Complexity: O((n + q) log n) time and O(n) space.
class Solution:
def popcountDepth(self, nums: List[int], queries: List[List[int]]) -> List[int]:
n = len(nums)
small = [0] * 64
for c in range(2, 64):
small[c] = small[bin(c).count("1")] + 1
def depth(x):
return 0 if x == 1 else small[bin(x).count("1")] + 1
tree = [[0] * (n + 1) for _ in range(6)]
def add(t, i, v):
i += 1
while i <= n:
tree[t][i] += v
i += i & -i
def query(t, i):
s = 0
while i > 0:
s += tree[t][i]
i -= i & -i
return s
for i, x in enumerate(nums):
add(depth(x), i, 1)
ans = []
for q in queries:
if q[0] == 1:
_, l, r, k = q
ans.append(query(k, r + 1) - query(k, l))
else:
_, i, val = q
add(depth(nums[i]), i, -1)
nums[i] = val
add(depth(val), i, 1)
return ans