📌  相关文章
📜  查询以[L,R]为范围的数组元素总和,索引的数量为K的倍数

📅  最后修改于: 2021-04-25 00:56:44             🧑  作者: Mango

给定由N个整数组成的数组arr []和由(L,R,K)形式的查询组成的矩阵Q [] [] ,每个查询的任务是从范围[ L,R]出现在索引(基于0的索引)上,该索引K

例子:

方法:可以使用前缀求和数组和范围和查询技术解决该问题。请按照以下步骤解决问题:

  1. 初始化一个大小为prefixSum [] []的矩阵,以使prefixSum [i] [j]存储索引中存在的元素之和,这些索引之和是ij索引的倍数
  2. 遍历数组并预先计算前缀和。
  3. 遍历每个查询,打印出prefixSum [K] [R] – prefixSum [K] [L – 1]的结果

下面是上述方法的实现:

C++
// C++ Program to implement
// the above appoach
#include 
using namespace std;
  
// Structure of a Query
struct Node {
    int L;
    int R;
    int K;
};
  
// Function to calculate the sum of array
// elements at indices from range [L, R]
// which are multiples of K for each query
int kMultipleSum(int arr[], Node Query[],
                 int N, int Q)
{
    // Stores Prefix Sum
    int prefixSum[N + 1][N];
  
    // prefixSum[i][j] : Stores the sum from
    // indices [0, j] which are multiples of i
    for (int i = 1; i <= N; i++) {
        prefixSum[i][0] = arr[0];
        for (int j = 0; j < N; j++) {
  
            // If index j is a multiple of i
            if (j % i == 0) {
  
                // Compute prefix sum
                prefixSum[i][j]
                    = arr[j] + prefixSum[i][j - 1];
            }
  
            // Otherwise
            else {
                prefixSum[i][j]
                    = prefixSum[i][j - 1];
            }
        }
    }
  
    // Traverse each query
    for (int i = 0; i < Q; i++) {
  
        // Sum of all indices upto R which
        // are a multiple of K
        int last
            = prefixSum[Query[i].K][Query[i].R];
        int first;
  
        // Sum of all indices upto L - 1 which
        // are a multiple of K
        if (Query[i].L == 0) {
            first
                = prefixSum[Query[i].K][Query[i].L];
        }
        else {
            first
                = prefixSum[Query[i].K][Query[i].L - 1];
        }
  
        // Calculate the difference
        cout << last - first << endl;
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int Q = 2;
    Node Query[Q];
    Query[0].L = 2, Query[0].R = 5, Query[0].K = 2;
    Query[1].L = 3, Query[1].R = 5, Query[1].K = 5;
    kMultipleSum(arr, Query, N, Q);
}


Java
// Java program to implement
// the above appoach
import java.util.*;
  
class GFG{
  
// Structure of a Query
static class Node
{
    int L;
    int R;
    int K;
};
  
// Function to calculate the sum of array
// elements at indices from range [L, R]
// which are multiples of K for each query
static void kMultipleSum(int arr[], Node Query[], 
                         int N, int Q)
{
      
    // Stores Prefix Sum
    int prefixSum[][] = new int[N + 1][N];
  
    // prefixSum[i][j] : Stores the sum from
    // indices [0, j] which are multiples of i
    for(int i = 1; i <= N; i++)
    {
        prefixSum[i][0] = arr[0];
        for(int j = 0; j < N; j++)
        {
              
            // If index j is a multiple of i
            if (j % i == 0)
            {
                  
                // Compute prefix sum
                if (j != 0)
                    prefixSum[i][j] = arr[j] + 
                                prefixSum[i][j - 1];
            }
  
            // Otherwise
            else
            {
                prefixSum[i][j] = prefixSum[i][j - 1];
            }
        }
    }
  
    // Traverse each query
    for(int i = 0; i < Q; i++)
    {
          
        // Sum of all indices upto R which
        // are a multiple of K
        int last = prefixSum[Query[i].K][Query[i].R];
        int first;
  
        // Sum of all indices upto L - 1 which
        // are a multiple of K
        if (Query[i].L == 0)
        {
            first = prefixSum[Query[i].K][Query[i].L];
        }
        else 
        {
            first = prefixSum[Query[i].K][Query[i].L - 1];
        }
  
        // Calculate the difference
        System.out.print(last - first + "\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = arr.length;
    int Q = 2;
      
    Node Query[] = new Node[Q];
    for(int i = 0; i < Q; i++)
        Query[i] = new Node();
          
    Query[0].L = 2;
    Query[0].R = 5;
    Query[0].K = 2;
    Query[1].L = 3;
    Query[1].R = 5;
    Query[1].K = 5;
      
    kMultipleSum(arr, Query, N, Q);
}
}
  
// This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above appoach
using System;
  
class GFG{
  
// Structure of a Query
class Node
{
    public int L;
    public int R;
    public int K;
};
  
// Function to calculate the sum of array
// elements at indices from range [L, R]
// which are multiples of K for each query
static void kMultipleSum(int []arr, Node []Query, 
                         int N, int Q)
{
      
    // Stores Prefix Sum
    int [,]prefixSum = new int[N + 1, N];
      
    // prefixSum[i,j] : Stores the sum from
    // indices [0, j] which are multiples of i
    for(int i = 1; i <= N; i++)
    {
        prefixSum[i, 0] = arr[0];
        for(int j = 0; j < N; j++)
        {
              
            // If index j is a multiple of i
            if (j % i == 0)
            {
                  
                // Compute prefix sum
                if (j != 0)
                    prefixSum[i, j] = arr[j] + 
                                prefixSum[i, j - 1];
            }
  
            // Otherwise
            else
            {
                prefixSum[i, j] = prefixSum[i, j - 1];
            }
        }
    }
  
    // Traverse each query
    for(int i = 0; i < Q; i++)
    {
          
        // Sum of all indices upto R which
        // are a multiple of K
        int last = prefixSum[Query[i].K,Query[i].R];
        int first;
  
        // Sum of all indices upto L - 1 which
        // are a multiple of K
        if (Query[i].L == 0)
        {
            first = prefixSum[Query[i].K,Query[i].L];
        }
        else
        {
            first = prefixSum[Query[i].K,Query[i].L - 1];
        }
  
        // Calculate the difference
        Console.Write(last - first + "\n");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5, 6 };
    int N = arr.Length;
    int Q = 2;
      
    Node []Query = new Node[Q];
    for(int i = 0; i < Q; i++)
        Query[i] = new Node();
          
    Query[0].L = 2;
    Query[0].R = 5;
    Query[0].K = 2;
    Query[1].L = 3;
    Query[1].R = 5;
    Query[1].K = 5;
      
    kMultipleSum(arr, Query, N, Q);
}
}
  
// This code is contributed by 29AjayKumar


输出:
8
6

时间复杂度: O(N 2 + O(Q)),计算前缀和数组需要O(N 2 )计算复杂度,每个查询都需要O(1)计算复杂度。
辅助空间: O(N 2 )