문제
Farmer John believes he has made a major breakthrough in algorithm design: he claims to have found a nearly linear time algorithm for the 3SUM problem, an algorithmic problem famous for the fact that no known solution exists running in substantially better than quadratic time. One formulation of the 3SUM problem is the following: given an array of integers, count the number of unordered triples of distinct indices such that .
To test Farmer John's claim, Bessie has provided an array of integers (). Bessie also asks queries (), each of which consists of two indices . For each query, Farmer John must solve the 3SUM problem on the subarray .
Unfortunately, Farmer John has just discovered a flaw in his algorithm. He is confident that he can fix the algorithm, but in the meantime, he asks that you help him pass Bessie's test!
SCORING:
Test cases 2-4 satisfy Test cases 5-7 satisfy Test cases 8-15 satisfy no additional constraints.
입력
The first line contains two space-separated integers and . The second line contains the space-separated elements of array . Each of the subsequent lines contains two space-separated integers and , representing a query.
It is guaranteed that for every array element .
출력
The output should consist of lines, with each line containing a single integer---the answer to the -th query. Note that you should use 64-bit integers to avoid overflow.
예제 입력 1
7 3
2 0 -1 1 -2 3 3
1 5
2 4
1 7
예제 출력 1
2
1
4
코드를 제출하려면 로그인이 필요합니다.
로그인