#P6006. [USACO20JAN] Farmer John Solves 3SUM G
[USACO20JAN] Farmer John Solves 3SUM G
Description
Farmer John believes he has made a major breakthrough in algorithm design: he claims to have found a near-linear-time algorithm for the 3SUM problem, a famous algorithmic problem for which no solution significantly faster than quadratic time has been found so far. One version of the 3SUM problem is: given an integer array , compute the number of unordered triples of distinct indices such that (, , and are all different).
To test Farmer John’s claim, Bessie provides an array of integers (). Bessie will also ask queries (), where each query consists of two indices . For each query, Farmer John must solve the 3SUM problem on the subarray .
Unfortunately, Farmer John has just discovered a bug in his algorithm. He is confident he can fix it, but in the meantime, he asks you to help him pass Bessie’s tests first.
Input Format
The first line contains two space-separated integers and .
The second line contains the elements of , namely , separated by spaces.
Each of the next lines contains two space-separated integers and , describing a query.
It is guaranteed that for each array element , we have .
Output Format
Output lines. The -th line contains one integer, the answer to the -th query. Note that you need to use a 64-bit integer type to avoid overflow.
7 3
2 0 -1 1 -2 3 3
1 5
2 4
1 7
2
1
4
Hint
Sample Explanation
For the first query, all valid triples are and .
Subtasks
- Test cases satisfy .
- Test cases satisfy .
- Test cases have no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号