如何找到数组所有子数组的按位与值的不同值数量。(数组大小<=1e5且数组元素<=1e6)。 例如: A[]={1,2,3} 不同的值有4个(1,2,3,0)。
如何找到数组所有子数组的按位与值的不同值数量。(数组大小<=1e5且数组元素<=1e6)。 例如: A[]={1,2,3} 不同的值有4个(1,2,3,0)。
and
的值发生改变时,它最多只能改变 O(log(MAX_VALUE)) 次。为什么这样说呢?因为当我们向左添加一个元素时,有两种情况:
子数组的 and
值不会发生改变。
子数组的 and
值会发生改变。此时,其位数会严格减少(因为它是前一个 and
值的子掩码)。
and
值发生改变的 l 值。现在我们需要快速找到它们。[1,2,3,4,0,1,2,3,4]
这样的重复计数? - גלעד ברקן想象数字像列一样代表它们的位。我们将有水平延伸的1序列。例如:
Array index: 0 1 2 3 4 5 6 7
Bit columns: 0 1 1 1 1 1 0 0
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 0
1 0 0 0 1 1 0 1
0 1 1 1 1 1 1 0
向左看,任何在零之后进行and
操作的子数组的位行将继续保持为零,这意味着该行在此之后不会发生任何变化。
以索引5为例。现在,从索引5到左侧排序1的水平序列将为我们提供一种检测位配置更改的简单方法(每次迭代都必须进行排序):
Index 5 ->
Sorted bit rows: 1 0 0 0 1 1
0 0 0 1 1 1
0 0 1 1 1 1
0 1 1 1 1 1
0 1 1 1 1 1
Index 5 to 4, no change
Index 4 to 3, change
Index 2 to 1, change
Index 1 to 0, change
Numbers: 1, 2, 3
Bits: 1 0 1
0 1 1
当我们从左到右移动时,记录每行最后一个未设置位的索引,并记录任何新的位配置(最多1e6
个):
Indexes of last unset bit for each row on each iteration
Numbers: 1, 2, 3
A[0]: -1 arrayHash = [false,true,false,false], count = 1
0
A[1]: -1 1 Now sort the column descending, representing (current - index)
0 0 the lengths of sequences of 1's extending to the left.
As we move from top to bottom on this column, each value change represents a bit
configuration and a possibly distinct count:
Record present bit configuration b10
=> arrayHash = [false,true,true,false]
1 => 1 - 1 => sequence length 0, ignore sequence length 0
0 => 1 - 0 => sequence length 1,
unset second bit: b10 => b00
=> new bit configuration b00
=> arrayHash = [true,true,true,false]
Numbers: 1, 2, 3
A[2]: -1 1 1
0 0 0
Record present bit configuration b11
=> arrayHash = [true,true,true,true]
由于我们不一定知道数组哈希已填满,因此我们继续执行。
1 => 2 - 1 => sequence length 1
unset first bit: b11 => b10
=> seen bit configuration b10
0 => 2 - 0 => sequence length 2,
unset second bit: b10 => b00
=> seen bit configuration b00