8496: DOS Card

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

DOS is a new single-player game that Kayzin came up with. At the beginning of the game you will be given n cards in a row, each with the number of value ai.

In each "matching" operation you can choose any two cards (we assume that the subscripts of these two cards are i,j (i < j)Notice that i is less than j), and you will get a score of (ai+aj)×(aiaj).

Kayzin will ask you mm times. In the k-th query, you need to select four cards from the cards with subscripts Lk to Rk, and "match" these four cards into two pairs (i.e., two matching operations, but the subscripts of the cards selected in the two matching operations must be different from each other. That is, a card can only be matched at most once. e.g., if you select four tickets with subscripts a  bc , and d , matching a with b and c with d, or matching a with c and b with d are legal, but matching a with b and b with c is not legal), please calculate the maximum value of the sum of the two matching scores.

Note that the queries are independent of each other.

输入格式

The first line contains an integer T(T≤100). Then T test cases follow. For one case,

The first line contains two integer (4n2×105) and m (1≤m≤10^5) , nn denotes the total number of cards , mm denotes the number of times Kayzin queries.

The second line contains n integers a1,a2,,an (1ai10^8), denotes the value of each card.

The next mm lines contain Kayzin's queries. The kth line has two numbers, Lk and Rk (1LkRkn), the input guarantees that RkLk3.

It is guaranteed that the sum of n over all test cases doesn't exceed 2×10^5 and the sum of m over all test cases doesn't exceed 2×10^5.

输出格式

Print m integer for each case, indicating the maximum scores that can be obtained by selecting four cards (two matching pairs)

输入样例 复制

1
5 3
5 3 2 8 4
1 5
1 4
2 5

输出样例 复制

69
-34
53