Array queries

2.6

27 votes
Basic Programming, Arrays, Implementation, 1-D, Data Structures
Problem

You are given 2 arrays and of length N and M respectivelyYou define F(A, B) as follows:

                                                    F(A,B)=Ni=1Mj=1A[i]×B[j]×(i+j)

You are also given an integer Q denoting the Q queries of the following types:

  • 1 i j: Swap A[i] and B[j], that is you replace the ith element in A with B[j] and jth element in B with A[i]
  • 2 i j: Swap A[i] and A[j], as described above
  • 3 i j: Swap B[i] and B[j], as described above

Task

Given queries in form of array queries, you need to output Q + 1 integers. The initial value of F(A, B) and the values after each query. The value of F(A, B) can be very large so, output the values modulo 998244353.

Notes

  • Assume 1-based indexing
  • Queries are dependent.

Example

Assumptions

  • T = 1
  • N = 2
  • M = 2
  • A = [2, 4]
  • B = [1, 5]
  • Q = 1
  • queries = [ [1, 2, 1] ]

Approach

  • You first evaluate F(A, B) = A[1]*B[1]*1 + 1 ) + A[1]*B[2]*1 + 2 ) + B[1]*A[2]*1 + 2 ) + B[2]*A[2]*2 + 2 ) = 2*1*2 + 2*5*3 + 1*4*3 + 5*4*4 = 4 + 30 + 12 + 80 = 126.
  • You swap A[2] and B[1], therefore becomes [2, 1] and becomes [4, 5], F(A, B) = A[1]*B[1]*1 + 1 ) + A[1]*B[2]*1 + 2 ) + B[1]*A[2]*1 + 2 ) + B[2]*A[2]*2 + 2 ) = 2*4*2 + 2*5*3 + 4*1*3 + 1*5*4 = 16 + 30 + 12 + 20 = 78.
  • You, therefore, output "126 78" in a single line.

Function description

Complete the array_queries function provided in the editor. This function takes the following 6 parameters and returns a vector/array denoting the values of F(A, B) for all queries:

  • N: Represents an integer denoting the length of array A
  • M: Represents an integer denoting the length of array B
  • A: Represents the integer array A
  • B: Represents the integer array B
  • Q: Represents an integer denoting the number of queries
  • queries: Represents a 2D array/vector denoting queries of the type "1 i j" or "2 i j" or "3 i j". Therefore, the size of the queries array is Q*3.

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains a single integer T, which denotes the number of test cases. also specifies the number of times you have to run the array_queries function on a different set of inputs.
  • For each test case:
    • The first line contains an integer denoting the length of the array A.
    • The second line contains an integer denoting the length of the array B.
    • The third line contains space-separated integers denoting array A.
    • The fourth line contains space-separated integers denoting array B.
    • The fifth line contains an integer denoting the number of queries.
    • The next lines follow. For each line:
      • The first line contains three space-separated integers denoting tp, i and j, where tp = 1, 2 or 3.

Output format

For each test case in a new line, print Q + 1 space-separated integers denoting the initial value of F(A, B) and F(A, B) after each query. Do not forget to take modulo 998244353.

Constraints

1T101N,M1051A[i],B[i]1061Q105If query is of type 1: 1iN,1jMIf query is of type 2: 1i,jNIf query is of type 3: 1i,jM

Code snippets (also called starter code/boilerplate code) 

This question has code snippets for C, CPP, Java, and Python.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

The first line contains the number of test cases, T = 2.

The first test case

The first test case is the same as the example. Please refer to that.

The second test case

Assumptions

  • T = 1
  • N = 3
  • M = 2
  • A = [3, 5, 1]
  • B = [4, 1]
  • Q = 3
  • queries = [ [1, 1, 2], [2, 1, 3], [3, 1, 2] ]

Approach

  • You first evaluate F(A, B) = A[1]*B[1]*1 + 1 ) + A[1]*B[2]*1 + 2 ) + A[2]*B[1]*( 2 + 1 ) + A[2]*B[2]*2 + 2 ) + A[3]*B[1]*( 3 + 1 ) + A[3]*B[2]*( 3 + 2 ) = 3*4*2 + 3*1*3 + 5*4*3 + 5*1*4 + 1*4*4 + 1*1*5 = 24 + 9 + 60 + 20 + 16 + 5 = 134.
  • You swap A[1] and B[2], therefore becomes [1, 5, 1] and becomes [4, 3], F(A, B) = 1*4*2 + 1*3*3 + 5*4*3 + 5*3*4 + 1*4*4 + 1*3*5 = 8 + 9 + 60 + 60 + 16 + 15 = 168.
  • You swap A[1] and A[3], therefore becomes [1, 5, 1] and B remains the sameRecall that queries are dependent. The value of F(A, B) comes out to be 168.
  • You swap B[1] and B[2], therefore A remained the same and B becomes [3, 4]Recall that queries are dependent. The value of F(A, B) comes out to be 175.
  • You, therefore, output "134 168 168 175" in a single line.
Editor Image

?