Problem · Array
Dynamic Pair Sum Queries
Learn this problemProblem statement
You are given two integer arrays a and b, and an array queries. Process the queries in order.
Every query has one of these forms:
[0, i, x]: assigna[i] = x.[1, x]: count the pairs of indices(i, j)such thata[i] + b[j] = x.
Return an array containing the results of the [1, x] queries in the order they occur.
Function
solution(a: int[], b: int[], queries: int[][]) → int[]Examples
Example 1
a = [3, 4]b = [1, 2, 3]queries = [[1, 5], [0, 0, 1], [1, 5]]return = [2, 1]Initially, sum 5 can be formed by a[0] + b[1] = 3 + 2 and a[1] + b[0] = 4 + 1, so the first result is 2.
After assigning a[0] = 1, only a[1] + b[0] = 4 + 1 forms 5, so the second result is 1.
Example 2
a = [2, 3]b = [1, 2, 2]queries = [[1, 4], [0, 0, 3], [1, 5]]return = [3, 4]Before the update, there are three index pairs with sum 4: two use a[0] = 2 with the two occurrences of 2 in b, and one uses a[1] = 3 with b[0] = 1.
After assigning a[0] = 3, both values in a are 3. Each can pair with either occurrence of 2 in b, producing four pairs with sum 5.