给定数组arr []和Q个查询,其中每个查询包含三个整数(l,r,x),任务是在将每个元素与x乘以后打印范围[l,r]中的元素之和。
Query(l, r, x) =
arr[l]*x + arr[l+1]*x + .... arr[r-1]*x + arr[r]*x
注意:与x的乘积已完成以计算答案,并且在查询的任何步骤都不会在输入数组中对其进行更新。
例子:
Input: arr[] = {2, 3, 4, 2, 5, 1}
Q[] = {{2, 4, 5}, {1, 1, 4}}
Output: 55 12
Explanation:
Query1: l = 2 r = 4 x = 5
ans = arr[2]*5 + arr[3]*6 + ar[4]*7
= 4*5 + 2*5 + 5*5
= 55
Query2: l = 1 r = 1 x = 4
ans = arr[1]*4
= 3*4 = 12
Input: arr[] = {2, 3, 4, 2}
Q[] = {{1, 1, 8}}
Output: 16
Explanation:
Query1: l = 1 r = 1 x = 8
ans = arr[1]*8
= 2*8 = 16
天真的方法:想法是遍历数组的每个查询,并针对每个查询遍历[l,r]范围的元素,并找到每个元素的总和乘以x。
时间复杂度: O(Q * N)
高效方法:想法是预先计算数组的前缀和,然后为每个查询找到范围[l,r]的元素之和,然后乘以x来找到每个查询的答案。
以下是计算每个查询答案的公式:
// pre_sum[i] denotes the prefix sum of
// the array from the array 0 to i
answer = x * (pre_sum[r] - pre_sum[l-1])
下面是上述方法的实现:
C++
// C++ implementation to multiply
// the given subarray by the x
// for multiple queries of the Q
#include
using namespace std;
// Function to answer each query
int query(vector pre_sum, int n,
int l, int r, int val)
{
int ans;
int temp = 0;
if (l > 0) {
temp = pre_sum[l-1];
}
ans = val * (pre_sum[r] - temp);
return ans;
}
// Function to multiply the subarray
// by the x for multiple Queries
void multiplyArray(int arr[],
vector, int>> q,
int n){
vector pre_sum;
int s = 0;
// Loop to compute the prefix
// sum of the array
for (int i = 0; i < n; i++){
s += arr[i];
pre_sum.push_back(s);
}
// Loop to answer each query
for(auto i: q){
cout << query(pre_sum, n, i.first.first,
i.first.second, i.second) << endl;
}
}
// Driver Code
int main()
{
// Array
int arr[] = { 2, 3, 4, 2, 5, 1 };
int n = 6;
vector, int>> q;
q.push_back({{2, 4}, 5});
q.push_back({{1, 1}, 4});
q.push_back({{1, 3}, -2});
// Function Call
multiplyArray(arr, q, n);
return 0;
}
Java
/*package whatever //do not write package name here */
// Java implementation to multiply
// the given subarray by the x
// for multiple queries of the Q
import java.io.*;
import java.util.ArrayList;
class GFG
{
// Function to answer each query
public static int query(ArrayList pre_sum,
int n, int l, int r, int val)
{
int ans;
int temp = 0;
if (l > 0)
{
temp = pre_sum.get(l - 1);
}
ans = val * (pre_sum.get(r) - temp);
return ans;
}
// Function to multiply the subarray
// by the x for multiple Queries
public static void multiplyArray(int arr[],
ArrayList q,
int n)
{
ArrayList pre_sum = new ArrayList<>();
int s = 0;
// Loop to compute the prefix
// sum of the array
for (int i = 0; i < n; i++)
{
s += arr[i];
pre_sum.add(s);
}
// Loop to answer each query
for(int i = 0; i < q.size(); i++)
{
System.out.println(query(pre_sum, n,
q.get(i).pr.a,
q.get(i).pr.b,
q.get(i).second));
}
}
// Driver Code
public static void main(String [] args)
{
// Array
int arr[] = { 2, 3, 4, 2, 5, 1 };
int n = 6;
ArrayList q = new ArrayList<>();
q.add(new pair(new pair1(2, 4), 5));
q.add(new pair(new pair1(1, 1), 4));
q.add(new pair(new pair1(1, 3), -2));
// Function Call
multiplyArray(arr, q, n);
}
}
class pair
{
pair1 pr;
int second;
pair(pair1 pr, int second)
{
this.pr = pr;
this.second = second;
}
}
class pair1
{
int a;
int b;
pair1(int a, int b)
{
this.a = a;
this.b = b;
}
}
// This code is contributed by aditya7409
Python3
# Python3 implementation to multiply
# the given subarray by the x
# for multiple queries of the Q
# Function to answer each query
def query(pre_sum, n, l, r, val):
ans = 0
temp = 0;
if (l > 0):
temp = pre_sum[l - 1];
ans = val * (pre_sum[r] - temp);
return ans;
# Function to multiply the subarray
# by the x for multiple Queries
def multiplyArray(arr, q, n):
pre_sum = []
s = 0;
# Loop to compute the prefix
# sum of the array
for i in range(n):
s += arr[i];
pre_sum.append(s);
# Loop to answer each query
for i in q:
print(query(pre_sum, n, i[0][0], i[0][1], i[1]))
# Driver Code
if __name__=='__main__':
# Array
arr = [ 2, 3, 4, 2, 5, 1 ]
n = 6;
q = []
q.append([[2, 4], 5])
q.append([[1, 1], 4])
q.append([[1, 3], -2])
# Function Call
multiplyArray(arr, q, n);
# this code is contribute by rutvik_56
C#
// C# implementation to multiply
// the given subarray by the x
// for multiple queries of the Q
using System;
using System.Collections.Generic;
class GFG
{
// Function to answer each query
static int query(List pre_sum, int n,
int l, int r, int val)
{
int ans;
int temp = 0;
if (l > 0)
{
temp = pre_sum[l - 1];
}
ans = val * (pre_sum[r] - temp);
return ans;
}
// Function to multiply the subarray
// by the x for multiple Queries
static void multiplyArray(int[] arr, List, int>> q, int n)
{
List pre_sum = new List();
int s = 0;
// Loop to compute the prefix
// sum of the array
for (int i = 0; i < n; i++)
{
s += arr[i];
pre_sum.Add(s);
}
// Loop to answer each query
foreach(Tuple, int> i in q)
{
Console.WriteLine(query(pre_sum, n, i.Item1.Item1,
i.Item1.Item2, i.Item2));
}
}
// Driver code
static void Main()
{
// Array
int[] arr = { 2, 3, 4, 2, 5, 1 };
int n = 6;
List, int>> q = new List, int>>();
q.Add(new Tuple, int>(new Tuple(2,4), 5));
q.Add(new Tuple, int>(new Tuple(1,1), 4));
q.Add(new Tuple, int>(new Tuple(1,3), -2));
// Function Call
multiplyArray(arr, q, n);
}
}
// This code is contributed by divyeshrabadiya07
输出:
55
12
-18