This page is still under construction.

Parts of this page are still being built. What you see may change.

Triple Products

Time limit5sMemory limit128 MB

Summary
Support point updates on an array and report, for each queried interval, the sum of products over all triples of distinct positions.
Level

Medium6 of 10

Topics
Segment tree, Math, Combinatorics
Solved
No attempts yet

Problem

A teacher wants to test the class's arithmetic fluency. Define a triple product as the product of three numbers taken from three distinct positions.

The teacher writes a row of nn natural numbers on the board. Over time the teacher either replaces one of the numbers with another natural number, or asks for the sum of all triple products that can be formed from the numbers in a given segment of the board. What makes a choice distinct is position, not value: two chosen numbers may be equal in value, but they must sit at different positions.

Answer every such query.

Input

The first line contains the number of test cases ZZ (here Z=1Z = 1).

Each test case is given as follows.

  • A line with a natural number nn (1≤n≤2000001 \le n \le 200000).
  • A line with nn natural numbers separated by spaces, the initial numbers on the board.
  • A line with a natural number qq (1≤q≤2000001 \le q \le 200000), the number of operations.
  • qq lines, each describing one operation:
    • Z a b (1≤a≤n1 \le a \le n, b>0b > 0): replace the number at position aa with the natural number bb.
    • Q a b (1≤a≤b≤n1 \le a \le b \le n): report the sum of all triple products formed from the numbers at positions in the range [a,b][a, b].

At every moment the sum of all numbers on the board does not exceed one million (10610^6).

Output

For each Q operation, print on its own line a single natural number: the sum of all triple products that can be formed by choosing three distinct positions in the queried range.

Note

For the board [1,3,2,2][1, 3, 2, 2] the first query over [1,4][1, 4] is 1⋅3⋅2+1⋅3⋅2+1⋅2⋅2+3⋅2⋅2=28.1\cdot3\cdot2 + 1\cdot3\cdot2 + 1\cdot2\cdot2 + 3\cdot2\cdot2 = 28. The second query covers only two numbers, so no triple can be formed and the answer is 00. After the number at position 11 is replaced by 22, the board becomes [2,3,2,2][2, 3, 2, 2] and the third query over [1,3][1, 3] is 2⋅3⋅2=122\cdot3\cdot2 = 12.

Examples1

  1. Example 1

    Input
    1
    4
    1 3 2 2
    4
    Q 1 4
    Q 1 2
    Z 1 2
    Q 1 3
    
    Expected output
    28
    0
    12