查找 Q 查询添加 AP 项后得到的 Array
给定一个由N个整数和Q个查询组成的数组A[] ,假设Query[][]的形式为{L, R, a, d} ,这样每个查询都表示无限 AP,其中第一项为a ,公差为d .任务是在执行给定查询后打印更新后的数组,以便对于每个查询{L, R, a, d}将等差数列的第 ( i – L + 1)项的值添加到A[i]中[L, R]范围内的每个索引i 。
例子:
Input: A[]= {5, 4, 2, 8}, Q = 2, Query[][] = {{1, 2, 1, 3}, {1, 4, 4, 1}}
Output: 10 13 8 15
Explanation:
Following are the queries performed:
Query 1: The arithmetic progression is {1, 4, 7, …}. After adding the first term of the progression to index 1 and the second term of the progression to index 2, the array modifies to {6, 8, 2, 8}.
Query 2: The arithmetic progression is {4, 5, 6, 7, 8, …}. After adding 4 to A[1], 5 to A[2], 6 to A[3] and 7 to A[4] the array modifies to {10, 13, 8 15}.
Therefore, the resultant array is {10, 13, 8 15}.
Input: A[] = {1, 2, 3, 4, 5}, Q = 3, Query[][] = {{1, 2, 1, 3}, {1, 3, 4, 1}, {1, 4, 1, 2}}
Output: 7 14 14 11 5
方法:给定问题可以通过在每个操作中迭代范围[L, R]并在每个索引i处添加给定算术级数的相应项来解决。请按照以下步骤解决问题:
- 使用变量i遍历数组Query[][]并对每个查询{L, R, a, d}执行以下步骤:
- 使用变量j遍历数组A[],在[L – 1, R – 1]范围内
- 将a的值与A[j]的值相加。
- 将a的值增加d 。
- 使用变量j遍历数组A[],在[L – 1, R – 1]范围内
- 完成上述步骤后,将数组A[]打印为结果更新后的数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find array after performing
// the given query to the array elements
void addAP(int A[], int Q, int operations[2][4])
{
// Traverse the given query
for (int j = 0; j < 2; ++j)
{
int L = operations[j][0], R = operations[j][1], a = operations[j][2], d = operations[j][3];
int curr = a;
// Traverse the given array
for(int i = L - 1; i < R; i++){
// Update the value of A[i]
A[i] += curr;
// Update the value of curr
curr += d;
}
}
// Print the array elements
for (int i = 0; i < 4; ++i)
cout << A[i] << " ";
}
// Driver Code
int main() {
int A[] = {5, 4, 2, 8};
int Q = 2;
int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
// Function Call
addAP(A, Q, Query);
return 0;
}
// This code is contributed by shubhamsingh10.
C
// C program for the above approach
#include
// Function to find array after performing
// the given query to the array elements
void addAP(int A[], int Q, int operations[2][4])
{
// Traverse the given query
for (int j = 0; j < 2; ++j)
{
int L = operations[j][0], R = operations[j][1], a = operations[j][2], d = operations[j][3];
int curr = a;
// Traverse the given array
for(int i = L - 1; i < R; i++){
// Update the value of A[i]
A[i] += curr;
// Update the value of curr
curr += d;
}
}
// Print the array elements
for (int i = 0; i < 4; ++i)
printf("%d ", A[i]);
}
// Driver Code
int main() {
int A[] = {5, 4, 2, 8};
int Q = 2;
int Query[2][4] = {{1, 2, 1, 3}, {1, 4, 4, 1}};
// Function Call
addAP(A, Q, Query);
return 0;
}
// This code is contributed by shubhamsingh10.
Java
// Java program for the above approach
class GFG {
// Function to find array after performing
// the given query to the array elements
public static void addAP(int A[], int Q, int[][] operations)
{
// Traverse the given query
for (int j = 0; j < 2; ++j) {
int L = operations[j][0], R = operations[j][1],
a = operations[j][2], d = operations[j][3];
int curr = a;
// Traverse the given array
for (int i = L - 1; i < R; i++) {
// Update the value of A[i]
A[i] += curr;
// Update the value of curr
curr += d;
}
}
// Print the array elements
for (int i = 0; i < 4; ++i)
System.out.print(A[i] + " ");
}
// Driver Code
public static void main(String args[])
{
int A[] = { 5, 4, 2, 8 };
int Q = 2;
int query[][] = { { 1, 2, 1, 3 }, { 1, 4, 4, 1 } };
// Function Call
addAP(A, Q, query);
}
}
// This code is contributed by _saurabh_jaiswal
Python3
# Python program for the above approach
# Function to find array after performing
# the given query to the array elements
def addAP(A, Q, operations):
# Traverse the given query
for L, R, a, d in operations:
curr = a
# Traverse the given array
for i in range(L-1, R):
# Update the value of A[i]
A[i] += curr
# Update the value of curr
curr += d
# Print the array elements
for i in A:
print(i, end =' ')
# Driver Code
A = [5, 4, 2, 8]
Q = 2
Query = [(1, 2, 1, 3), (1, 4, 4, 1)]
# Function Call
addAP(A, Q, Query)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find array after performing
// the given query to the array elements
public static void addAP(int[] A, int Q,
int[,] operations)
{
// Traverse the given query
for(int j = 0; j < 2; ++j)
{
int L = operations[j, 0], R = operations[j, 1],
a = operations[j, 2], d = operations[j, 3];
int curr = a;
// Traverse the given array
for(int i = L - 1; i < R; i++)
{
// Update the value of A[i]
A[i] += curr;
// Update the value of curr
curr += d;
}
}
// Print the array elements
for(int i = 0; i < 4; ++i)
Console.Write(A[i] + " ");
}
// Driver code
public static void Main(string[] args)
{
int[] A = { 5, 4, 2, 8 };
int Q = 2;
int[,] query = { { 1, 2, 1, 3 },
{ 1, 4, 4, 1 } };
// Function Call
addAP(A, Q, query);
}
}
// This code is contributed by avijitmondal1998
Javascript
10 13 8 15
时间复杂度: O(N*Q)
辅助空间: O(1)