在给定数组中为 Q 查询计算索引范围 [L, R] 中的设置位
给定一个包含N个整数的数组arr[]和一个包含{L, R}形式的Q个查询的数组queries[] ,任务是计算每个查询在数组arr中从L到R的设置位总数。
例子:
Input: arr[]={1, 2, 3, 4, 5, 6}, queries[]={{0, 2}, {1, 1}, {3, 5}}
Output:
4
2
5
Explanation:
Query 1 (0, 2): Elements {1, 2, 3} have set bits {1, 1, 2} respectively. So sum=1+1+2=4
Query 1 (1, 1): Element {2} have set bit {1}. So sum=1
Query 1 (3, 5): Elements {4, 5, 6} have set bits {1, 2, 2} respectively. So sum=1+2+2=5
Input: arr[]={2, 4, 3, 5, 6}, queries[]={{0, 1}, {2, 4}}
Output:
2
6
方法:要解决这个问题,请按照以下步骤操作:
- 创建一个向量,比如onBits ,其中每个i元素将表示从arr[0]到arr[i]的设置位数。
- 从i=0 到 i
运行一个循环,并在每次迭代中,找到arr[i]中设置的位数,比如 bits 并制作onBits[i]=onBits[i-1] + bits 。 - 现在遍历数组查询,对于每个查询{L, R} ,从L到R的设置位数为onBits[R]-onBits[L-1] 。
- 根据以上观察打印答案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to return the number
// of set bits from L to R
int setBitsinLtoR(int L, int R,
vector& onBits)
{
if (L == 0) {
return onBits[R];
}
return onBits[R] - onBits[L - 1];
}
// Function to find the number
// of set bits for Q queries
void totalSetBits(vector& arr,
vector >& queries)
{
int N = arr.size();
vector onBits(N, 0);
onBits[0] = __builtin_popcount(arr[0]);
for (int i = 1; i < N; ++i) {
onBits[i]
= onBits[i - 1]
+ __builtin_popcount(arr[i]);
}
// Finding for Q queries
for (auto x : queries) {
int L = x.first;
int R = x.second;
cout << setBitsinLtoR(L, R, onBits)
<< endl;
}
}
// Driver Code
int main()
{
vector arr = { 1, 2, 3, 4, 5, 6 };
vector > queries
= { { 0, 2 }, { 1, 1 }, { 3, 5 } };
totalSetBits(arr, queries);
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to return the number
// of set bits from L to R
static int setBitsinLtoR(int L, int R,int []onBits)
{
if (L == 0) {
return onBits[R];
}
return onBits[R] - onBits[L - 1];
}
// Function to find the number
// of set bits for Q queries
static void totalSetBits(int[] arr,
pair[] queries)
{
int N = arr.length;
int []onBits = new int[N];
onBits[0] = Integer.bitCount(arr[0]);
for (int i = 1; i < N; ++i) {
onBits[i]
= onBits[i - 1]
+ Integer.bitCount(arr[i]);
}
// Finding for Q queries
for (pair x : queries) {
int L = x.first;
int R = x.second;
System.out.print(setBitsinLtoR(L, R, onBits)
+"\n");
}
}
// Driver Code
public static void main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 6 };
pair []queries
= { new pair( 0, 2 ), new pair( 1, 1 ), new pair( 3, 5 ) };
totalSetBits(arr, queries);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
def __builtin_popcount(n):
count = 0
while (n):
count += n & 1
n >>= 1
return count
# Function to return the number
# of set bits from L to R
def setBitsinLtoR(L, R, onBits):
if (L == 0):
return onBits[R];
return onBits[R] - onBits[L - 1];
# Function to find the number
# of set bits for Q queries
def totalSetBits(arr, queries):
N = len(arr);
onBits = [0] * N
onBits[0] = __builtin_popcount(arr[0]);
for i in range(1, N):
onBits[i] = onBits[i - 1] + __builtin_popcount(arr[i]);
# Finding for Q queries
for x in queries:
L = x[0];
R = x[1];
print(setBitsinLtoR(L, R, onBits))
# Driver Code
arr = [ 1, 2, 3, 4, 5, 6 ];
queries = [ [ 0, 2 ], [ 1, 1 ], [ 3, 5 ] ];
totalSetBits(arr, queries);
# This code is contributed by gfgking
C#
// C# code for the above approach
using System;
class GFG{
class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to return the number
// of set bits from L to R
static int setBitsinLtoR(int L, int R, int []onBits)
{
if (L == 0)
{
return onBits[R];
}
return onBits[R] - onBits[L - 1];
}
// Function to find the number
// of set bits for Q queries
static void totalSetBits(int[] arr,
pair[] queries)
{
int N = arr.Length;
int []onBits = new int[N];
onBits[0] = bitCount(arr[0]);
for(int i = 1; i < N; ++i)
{
onBits[i] = onBits[i - 1] +
bitCount(arr[i]);
}
// Finding for Q queries
foreach (pair x in queries)
{
int L = x.first;
int R = x.second;
Console.Write(setBitsinLtoR(L, R, onBits) +
"\n");
}
}
static int bitCount(int x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 6 };
pair []queries = { new pair(0, 2),
new pair(1, 1),
new pair(3, 5) };
totalSetBits(arr, queries);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
4
1
5
时间复杂度: O(Q*N*logK),其中 N 是数组 arr 的大小,Q 是查询次数,K 是位数
辅助空间: O(N)