Minimize a product

1.8

6 votes
Advanced Data Structures, Segment Trees, C++, Data Structures
Problem

You are given an array A of N integers. 

Also, you are given Q queries of the following type:

  • 1 x v: Change the value of the element at xth index to v i.e. set A[x]=v.
  • 2 l r: Determine the number of pairs (i,j) such that:
    • li<jr
    • A[i]×A[j]is minimum possible among all such possible pairs of elements

Your task is to determine the sum of answers for queries of Type 2 over all Q queries.

Note

  • Assume 1-based indexing.
  • The sum can be very large, print the output in modulo 109+7

Input format

  • The first line contains an integer T that denotes the number of test cases.
  • For each test case:
    • The first line contains two space-separated integers that denotes N Q.
    • The next line contains N space-separated integers that denotes the array A.
    • The next Q lines contain queries.

Output format

For each test case, print an integer denoting the sum of the answer for all the queries of Type 2 in a new line.

Constraints

1T101N,Q1051l<rN1xN1v,A[i]106

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

For Query 1:

  • Pair (2,3) satisfy the required condition as A[2]×A[3]=6 is the minimum possible product that can be achieved.

After Query 2:

  • A[4]=3

For Query 3:

  • Pair (3,4),(4,5),(3,5) satisfy the required condition as A[3]×A[4]=A[3]×A[5]=A[4]×A[5]=9 is the minimum possible product that can be achieved.

Hence, the required answer is 1 + 3 = 4.

 

Editor Image

?