📌  相关文章
📜  在 Q 查询更新后打印大小为 N 的数组,其中包含 [0, M) 范围内的值

📅  最后修改于: 2022-05-13 01:57:51.202000             🧑  作者: Mango

在 Q 查询更新后打印大小为 N 的数组,其中包含 [0, M) 范围内的值

给定 大小为N的数组arr[]包含状态从0到 ( M – 1)的循环变量(即,当从M-1递增时,它变为0 )。任务是完成以下两种类型中的任何一种的Q查询:

  • 第一种类型:1 LRK – 循环增加索引范围内的所有值 [L, R],K 次。
  • 第二种类型:2 LR - 打印索引范围内的更新值 [L, R]。

例子:

方法:可以使用贪心方法解决问题。按照下面提到的步骤来实施该方法:

  • 当查询是第一种类型时,更新范围 [L, R] 中的所有值,K 次。
  • 当查询是第二种类型时,打印范围 [L, R] 中的值。

下面是上述方法的实现。

C++14
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to implement the queries
void update(int arr[], int N, int M, int Q,
            vector >& queries)
{
    // Loop to implement the queries
    for (int i = 0; i < Q; i++) {
        if (queries[i][0] == 1) {
            for (int j = queries[i][1];
                 j <= queries[i][2];
                 j++)
                arr[j] = (arr[j] +
                          queries[i][3]) % M;
        }
        else if (queries[i][0] == 2) {
            for (int j = queries[i][1];
                 j <= queries[i][2];
                 j++)
                cout << arr[j] << " ";
            cout << endl;
        }
    }
}
 
// Driver's code
int main()
{
    int N = 5, M = 8, Q = 5;
    int arr[] = { 2, 2, 7, 2, 5 };
    vector > queries(Q);
    queries[0] = { 1, 0, 3, 4 };
    queries[1] = { 1, 4, 4, 2 };
    queries[2] = { 1, 0, 0, 7 };
    queries[3] = { 2, 1, 3 };
    queries[4] = { 2, 3, 3 };
 
    update(arr, N, M, Q, queries);
    return 0;
}


Java
// Java code to implement the above approach
import java.io.*;
class GFG {
 
  // Function to implement the queries
  public static void update(int[] arr, int N, int M,
                            int Q, int[][] queries)
  {
 
    // Loop to implement the queries
    for (int i = 0; i < Q; i++) {
      if (queries[i][0] == 1) {
        for (int j = queries[i][1];
             j <= queries[i][2];
             j++)
          arr[j] = (arr[j] +
                    queries[i][3]) % M;
      }
      else if (queries[i][0] == 2) {
        for (int j = queries[i][1];
             j <= queries[i][2];
             j++)
          System.out.print(arr[j]+ " ");
        System.out.println();
      }
    }
  }
 
  // Driver's code
  public static void main (String[] args)
  {
    int N = 5, M = 8, Q = 5;
    int[] arr = { 2, 2, 7, 2, 5 };
    int[][] queries = new int[][]
    {
      new int[] { 1, 0, 3, 4 },
      new int[] { 1, 4, 4, 2 },
      new int[] { 1, 0, 0, 7 },
      new int[] { 2, 1, 3 },
      new int[] { 2, 3, 3 }
    };
    update(arr, N, M, Q, queries);
  }
}
 
// This code is contributed by Shubham Singh


Python3
# Python3 code to implement above approach
 
# Function to implement the queries
def update(arr, N, M,  Q, queries):
 
    # Loop to implement the queries
    for i in range(Q):
        if (queries[i][0] == 1):
            for j in range(queries[i][1],
                           queries[i][2] + 1):
 
                arr[j] = (arr[j] +
                          queries[i][3]) % M
 
        elif (queries[i][0] == 2):
            for j in range(queries[i][1],
                           queries[i][2] + 1):
 
                print(arr[j], end = " ")
                 
            print()
 
# Driver code
if __name__ == "__main__":
 
    N = 5
    M = 8
    Q = 5
    arr = [2, 2, 7, 2, 5]
    queries = []
     
    queries.append([1, 0, 3, 4])
    queries.append([1, 4, 4, 2])
    queries.append([1, 0, 0, 7])
    queries.append([2, 1, 3])
    queries.append([2, 3, 3])
 
    update(arr, N, M, Q, queries)
     
# This code is contributed by ukasp


C#
// C# code to implement the above approach
using System;
public class GFG{
 
  // Function to implement the queries
  public static void update(int[] arr, int N, int M, int Q, int[][] queries)
  {
    // Loop to implement the queries
    for (int i = 0; i < Q; i++) {
      if (queries[i][0] == 1) {
        for (int j = queries[i][1];
             j <= queries[i][2];
             j++)
          arr[j] = (arr[j] +
                    queries[i][3]) % M;
      }
      else if (queries[i][0] == 2) {
        for (int j = queries[i][1];
             j <= queries[i][2];
             j++)
          Console.Write(arr[j]+ " ");
        Console.WriteLine();
      }
    }
  }
 
  // Driver's code
  public static void Main()
  {
    int N = 5, M = 8, Q = 5;
    int[] arr = { 2, 2, 7, 2, 5 };
    int[][] queries = new int[][]
    {
      new int[] { 1, 0, 3, 4 },
      new int[] { 1, 4, 4, 2 },
      new int[] { 1, 0, 0, 7 },
      new int[] { 2, 1, 3 },
      new int[] { 2, 3, 3 }
    };
    update(arr, N, M, Q, queries);
  }
}
 
// This code is contributed by Shubham Singh


Javascript


输出
6 3 6 
6 

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