给定一个由N 个整数组成的数组arr[]和一个由M对表示{X, Y}类型查询的数组Q[] ,任务是执行以下类型的查询:
- Query(0, X):将整数X添加到每个数组元素。
- Query(1, Y):将每个数组元素乘以Y 。
- Query(2, i):打印索引i处的值。
例子:
Input: arr[] = {5, 1, 2, 3, 9}, Q[][] = {{0, 5}, {2, 4}, {1, 4}, {0, 2}, {2, 3}}
Output: 14 34
Explanation:
Below are the results after performing each query:
- Query(0, 5): Adding 5 to every array elements, modifies the array as arr[] = {10, 6, 7, 8, 14}.
- Query(2, 4): Print the array element at index 4, i.e., arr[4] = 14.
- Query(1, 4): Multiplying every array elements with 4, modifies the array as arr[] = {40, 24, 28, 32, 56}
- Query(0, 2): Adding 2 to every array elements, modifies the array as arr[] = {42, 26, 30, 34, 58}
- Query(2, 3): Print the element at index 4, i.e., arr[3] = 34.
Input: arr[] = {3, 1, 23, 45, 100}, Q[][] = {{1, 2}, {0, 10}, {2, 3}, {1, 5}, {2, 4}}
Output: 100 1050
朴素方法:解决给定问题的最简单方法是通过遍历给定数组来执行每个查询,并为每个查询打印相应的结果。
时间复杂度: O(N * M)
辅助空间: O(1)
高效的方法:上述方法可以基于以下观察进行优化:
- 假设A是一个元素,执行以下操作:
- 加入A1至A,则A的值变为A = A + A1。
- 将A乘以b1 ,则A的值变为A = b1 * A + b1 * a1 。
- A2添加到A,则A的值变为A = B1 * A + B1 * A1 + A2。
- 将A乘以b2 ,则A的值变为, A = b1 * b2 * A + b1 * b2 * a1 + a2 * b2
- 假设Mul是类型1查询的所有整数的乘积,而Add存储所有类型 0查询的值,并且类型 1以从0开始的给定顺序执行。那么从上面可以看出,任何数组元素arr[i] 都修改为(arr[i] * Mul + Add) 。
请按照以下步骤解决问题:
- 初始化两个变量,比如Mul为1和Add为0,存储类型2查询中所有整数的乘法,并存储在数字0上按给定顺序执行类型1和2查询后得到的值。
- 遍历给定的查询数组Q[][2]并执行以下步骤:
- 如果Q[i][0]为0 ,则将Add的值增加Q[i][1] 。
- 否则,如果Q[i][0] 的值为1,则将Mul和Add乘以Q[i][1] 。
- 否则,打印值(arr[Q[i][1]] * Mul + Add)作为类型 2查询的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to modify the array
// by performing given queries
void Query(int arr[], int N,
vector > Q)
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.size(); i++) {
// Query of type 0
if (Q[i][0] == 0) {
// Update the value of add
add = add + Q[i][1];
}
// Query of type 1
else if (Q[i][0] == 1) {
// Update the value of mul
mul = mul * Q[i][1];
// Update the value of add
add = add * Q[i][1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i][1]] * mul + add;
// Print the result for
// the current query
cout << ans << " ";
}
}
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 23, 45, 100 };
int N = sizeof(arr) / sizeof(arr[0]);
vector > Q = {
{ 1, 2 }, { 0, 10 },
{ 2, 3 }, { 1, 5 }, { 2, 4 }
};
Query(arr, N, Q);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG
{
// Function to modify the array
// by performing given queries
static void Query(int arr[], int N, int Q[][])
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.length; i++) {
// Query of type 0
if (Q[i][0] == 0) {
// Update the value of add
add = add + Q[i][1];
}
// Query of type 1
else if (Q[i][0] == 1) {
// Update the value of mul
mul = mul * Q[i][1];
// Update the value of add
add = add * Q[i][1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i][1]] * mul + add;
// Print the result for
// the current query
System.out.print(ans + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 1, 23, 45, 100 };
int N = arr.length;
int Q[][] = { { 1, 2 },
{ 0, 10 },
{ 2, 3 },
{ 1, 5 },
{ 2, 4 } };
Query(arr, N, Q);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to modify the array
# by performing given queries
def Query(arr, N, Q):
# Stores the multiplication
# of all integers of type 1
mul = 1
# Stores the value obtained after
# performing queries of type 1 & 2
add = 0
# Iterate over the queries
for i in range(len(Q)):
# Query of type 0
if (Q[i][0] == 0):
# Update the value of add
add = add + Q[i][1]
# Query of type 1
elif (Q[i][0] == 1):
# Update the value of mul
mul = mul * Q[i][1]
# Update the value of add
add = add * Q[i][1]
# Otherwise
else:
# Store the element at index Q[i][1]
ans = arr[Q[i][1]] * mul + add
# Prthe result for
# the current query
print(ans,end=" ")
# Driver Code
if __name__ == '__main__':
arr = [3, 1, 23, 45, 100]
N = len(arr)
Q = [
[ 1, 2 ], [ 0, 10 ],
[ 2, 3 ], [ 1, 5 ], [ 2, 4 ]
]
Query(arr, N, Q)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to modify the array
// by performing given queries
static void Query(int[] arr, int N, int[, ] Q)
{
// Stores the multiplication
// of all integers of type 1
int mul = 1;
// Stores the value obtained after
// performing queries of type 1 & 2
int add = 0;
// Iterate over the queries
for (int i = 0; i < Q.GetLength(0); i++) {
// Query of type 0
if (Q[i, 0] == 0) {
// Update the value of add
add = add + Q[i, 1];
}
// Query of type 1
else if (Q[i, 0] == 1) {
// Update the value of mul
mul = mul * Q[i, 1];
// Update the value of add
add = add * Q[i, 1];
}
// Otherwise
else {
// Store the element at index Q[i][1]
int ans = arr[Q[i, 1]] * mul + add;
// Print the result for
// the current query
Console.Write(ans + " ");
}
}
}
// Driver Code
public static void Main()
{
int[] arr = { 3, 1, 23, 45, 100 };
int N = arr.Length;
int[, ] Q = { { 1, 2 },
{ 0, 10 },
{ 2, 3 },
{ 1, 5 },
{ 2, 4 } };
Query(arr, N, Q);
}
}
// This code is contributed by ukasp.
Javascript
输出:
100 1050
时间复杂度: O(M)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live