RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0406 파일 입출력

Farmer John Solves 3SUM

Platinum III 플래티넘 III
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

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!

Problem credits: Dhruv Rohatgi

제약

SCORING

  • Test cases 2-4 satisfy \(N\le 500.\)
  • Test cases 5-7 satisfy \(N\le 2000.\)
  • Test cases 8-15 satisfy no additional constraints.

Problem credits: Dhruv Rohatgi

입력 형식

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. **

파일 입력 / 출력
이 문제는 표준 입출력을 사용하지 않습니다. 프로그램은 다음 파일을 읽고 써야 합니다:
입력을 읽을 파일 threesum.in
출력을 쓸 파일 threesum.out
예제 1
입력
7 3
2 0 -1 1 -2 3 3
1 5
2 4
1 7
출력
2
1
4
설명

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

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2019-2020 > January > Gold

태그

평가 및 의견

Farmer John Solves 3SUM

개요
출제자 난이도 Platinum III 플래티넘 III 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Farmer John Solves 3SUM

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8