📌  相关文章
📜  执行每个查询后找到数组的总和

📅  最后修改于: 2021-05-17 03:12:39             🧑  作者: Mango

给定大小为NQ个查询的数组arr [] ,其中每个查询包含两个整数XY ,任务是在执行每个Q查询之后查找数组的和,以便对于每个查询,数组arr [中的元素]的值X更新为Y。在每个查询之后找到数组的总和。

例子:

幼稚的方法:幼稚的方法是遍历每个查询,并通过遍历每个查询,将数组arr []中的每个元素更新为值X到Y。执行每个查询后,打印arr []中所有元素的总和。
时间复杂度: O(N * Q)
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是计算数组中所有元素的总和(例如sum ) arr []并将所有元素的频率存储在unordered_map(Say M )中。对于每个查询(X,Y),请执行以下操作:

  1. 从unordered_map M中找到X的频率。
  2. 将总和减少X * M [X] ,以排除X的总和。
  3. 将总和增加Y * M [X] ,以排除Y的总和。
  4. 将地图中Y的频率增加M [X]
  5. 最后,打印总和并从映射M中删除X。

下面是上述方法的实现

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function that solves the given queries
void solve(int ar[], int n, int b[],
           int c[], int q)
{
    // This map will store the
    // frequency of each element
    unordered_map mp;
 
    // sum stores the sum of
    // all elements of array
    int sum = 0;
 
    for (int x = 0; x < n; x++) {
        sum += ar[x];
        mp[ar[x]]++;
    }
 
    // Process the queries
    for (int x = 0; x < q; x++) {
 
        // Find occurrence of
        // b[x] from map
        int occur1 = mp[b[x]];
 
        // Decrease sum
        sum = sum - occur1 * b[x];
 
        // Erase b[x] from map
        mp.erase(b[x]);
 
        // Increase sum
        sum = sum + occur1 * c[x];
 
        // Increase frequency
        // of c[x] in map
        mp] += occur1;
 
        // Print sum
        cout << sum << " ";
    }
}
 
// Driver Code
int main()
{
    // Given arr[]
    int ar[] = { 1, 2, 3, 4 };
    int n = 4;
 
    // Given Queries
    int q = 3;
    int b[] = { 1, 3, 2 };
    int c[] = { 2, 4, 4 };
 
    // Function Call
    solve(ar, n, b, c, q);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function that solves the given queries
static void solve(int ar[], int n, int b[],
                   int c[], int q)
{
     
    // This map will store the
    // frequency of each element
    Map mp = new HashMap<>();
 
    // sum stores the sum of
    // all elements of array
    int sum = 0;
 
    for(int x = 0; x < n; x++)
    {
        sum += ar[x];
        mp.put(ar[x], mp.getOrDefault(ar[x], 0) + 1);
    }
     
    // Process the queries
    for(int x = 0; x < q; x++)
    {
         
        // Find occurrence of
        // b[x] from map
        int occur1 = mp.get(b[x]);
 
        // Decrease sum
        sum = sum - occur1 * b[x];
 
        // Erase b[x] from map
        mp.remove(b[x]);
 
        // Increase sum
        sum = sum + occur1 * c[x];
 
        // Increase frequency
        // of c[x] in map
        mp.put(c[x], mp.get(c[x]) + occur1);
         
        // Print sum
        System.out.print(sum + " ");
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given arr[]
    int ar[] = { 1, 2, 3, 4 };
    int n = 4;
     
    // Given queries
    int q = 3;
    int b[] = { 1, 3, 2 };
    int c[] = { 2, 4, 4 };
     
    // Function call
    solve(ar, n, b, c, q);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function that solves the given queries
def solve(ar, n, b, c, q):
   
    # This map will store the
    # frequency of each element
    mp = {}
     
    # sum stores the sum of
    # all elements of array
    sum = 0
 
    for x in range(n):
        sum += ar[x]
        mp[ar[x]] = mp.get(ar[x], 0) + 1
 
    # Process the queries
    for x in range(q):
       
        # Find occurrence of
        # b[x] from map
        occur1 = mp[b[x]]
 
        # Decrease sum
        sum = sum - occur1 * b[x]
 
        # Erase b[x] from map
        del mp[b[x]]
 
        # Increase sum
        sum = sum + occur1 * c[x]
 
        # Increase frequency
        # of c[x] in map
        mp] += occur1
 
        # Print sum
        print(sum, end = " ")
         
# Driver Code
if __name__ == '__main__':
   
    # Given arr[]
    ar = [ 1, 2, 3, 4 ]
    n = 4
 
    # Given Queries
    q = 3
    b = [ 1, 3, 2 ]
    c = [ 2, 4, 4 ]
 
    # Function Call
    solve(ar, n, b, c, q)
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Function that solves
// the given queries
static void solve(int []ar, int n,
                  int []b, int []c,
                  int q)
{   
  // This map will store the
  // frequency of each element
  Dictionary mp = new Dictionary();
 
  // sum stores the sum of
  // all elements of array
  int sum = 0;
 
  for(int x = 0; x < n; x++)
  {
    sum += ar[x];
    if(mp.ContainsKey(ar[x]))
      mp[ar[x]] = mp[ar[x]] + 1;
    else
      mp.Add(ar[x], 1);
  }
 
  // Process the queries
  for(int x = 0; x < q; x++)
  {
    // Find occurrence of
    // b[x] from map
    int occur1 = mp[b[x]];
 
    // Decrease sum
    sum = sum - occur1 * b[x];
 
    // Erase b[x] from map
    mp.Remove(b[x]);
 
    // Increase sum
    sum = sum + occur1 * c[x];
 
    // Increase frequency
    // of c[x] in map
    if(mp.ContainsKey(c[x]))
      mp] = mp] + occur1;
 
    // Print sum
    Console.Write(sum + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Given []arr
  int []ar = {1, 2, 3, 4};
  int n = 4;
 
  // Given queries
  int q = 3;
  int []b = {1, 3, 2};
  int []c = {2, 4, 4};
 
  // Function call
  solve(ar, n, b, c, q);
}
}
 
// This code is contributed by Princi Singh


输出:
11 12 16


时间复杂度: O(N + Q)
辅助空间: O(N)