A838 | Farmer John Solves 3SUM--Gold
来源USACO
时间限制1s
内存限制128MB
通过 / 提交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!
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$.
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.**
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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted