여러 부분 배열 질의마다 세 값의 합이 0이 되는 서로 다른 세 인덱스 조합의 개수를 센다.
어려움8동적 계획법누적 합해시맵완전 탐색아직 제출이 없습니다시간 제한2초메모리 제한512 MBFarmer 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 s_1,…,s_m of integers, count the number of unordered triples of distinct indices i,j,k such that s_i+s_j+s_k=0.
To test Farmer John's claim, Bessie has provided an array A of N integers (1≤N≤5000). Bessie also asks Q queries (1≤Q≤105), each of which consists of two indices 1≤a_i≤b_i≤N. For each query, Farmer John must solve the 3SUM problem on the subarray A\[a_i…b_i].
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!
The first line contains two space-separated integers N and Q. The second line contains the space-separated elements A_1,…,A_N of array A. Each of the subsequent Q lines contains two space-separated integers a_i and b_i, representing a query.
It is guaranteed that −106≤A_i≤106 for every array element A_i.
The output should consist of Q lines, with each line i containing a single integer---the answer to the i-th query. Note that you should use 64-bit integers to avoid overflow.
For the first query, the possible triples are (A_1,A_2,A_5) and (A_2,A_3,A_4).