Triple Products
Time limit5sMemory limit128 MB
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 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 (here ).
Each test case is given as follows.
- A line with a natural number ().
- A line with natural numbers separated by spaces, the initial numbers on the board.
- A line with a natural number (), the number of operations.
- lines, each describing one operation:
Z a b(, ): replace the number at position with the natural number .Q a b(): report the sum of all triple products formed from the numbers at positions in the range .
At every moment the sum of all numbers on the board does not exceed one million ().
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 the first query over is The second query covers only two numbers, so no triple can be formed and the answer is . After the number at position is replaced by , the board becomes and the third query over is .