Number of Integers With Popcount-Depth Equal to K II
hard · 59.4% accepted · 34 likes · top 57%
array · divide and conquer · binary indexed tree · segment tree
Description
You are given an integer array nums.
For any positive integer x, define the following sequence:
- p0 = x
- pi+1 = popcount(pi) for all i >= 0, where popcount(y) is the number of set bits (1's) in the binary representation of y.
This sequence will eventually reach the value 1.
The popcount-depth of x is defined as the smallest integer d >= 0 such that pd = 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 indices j such that l <= j <= r and the popcount-depth of nums[j] is equal to k.
- [2, idx, val] - Update nums[idx] to val.
Return an integer array answer, where answer[i] is the number of indices for the ith query of type [1, l, r, k].
Solution