Logo
(追記) (追記ここまで)

18317번 - Farmer John Solves 3SUM 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB74823417829.966%

문제

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 $s_1,\dots,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ドル \leq N \leq 5000$). Bessie also asks $Q$ queries (1ドル \leq Q \leq 10^5$), each of which consists of two indices 1ドル \leq a_i \leq b_i \leq N$. For each query, Farmer John must solve the 3SUM problem on the subarray $A[a_i \dots 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,\dots,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 $-10^6 \leq A_i \leq 10^6$ 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.

제한

예제 입력 1

7 3
2 0 -1 1 -2 3 3
1 5
2 4
1 7

예제 출력 1

2
1
4

힌트

For the first query, the possible triples are $(A_1,A_2,A_5)$ and $(A_2,A_3,A_4).$

출처

Olympiad > USA Computing Olympiad > 2019-2020 Season > USACO 2020 January Contest > Gold 2번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /