A13806 | Non-zero Segments
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Kolya got an integer array $a_1, a_2, \dots, a_n$ . The array can contain both positive and negative integers, but Kolya doesn't like $0$ , so the array doesn't contain any zeros.
Kolya doesn't like that the sum of some subsegments of his array can be $0$ . The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$ . To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$ , any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$ .
Kolya doesn't like that the sum of some subsegments of his array can be $0$ . The subsegment is some consecutive segment of elements of the array.
You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $0$ . To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $0$ , any by absolute value, even such a huge that they can't be represented in most standard programming languages).
Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$ .
输入格式
The first line of the input contains one integer $n$ ( $2 \le n \le 200\,000$ ) — the number of elements in Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^{9} \le a_i \le 10^{9}, a_i \neq 0$ ) — the description of Kolya's array.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^{9} \le a_i \le 10^{9}, a_i \neq 0$ ) — the description of Kolya's array.
输出格式
Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $0$ .
输入输出样例
输入 #1
4 1 -5 3 2
输出 #1
1
输入 #2
5 4 -2 3 -9 2
输出 #2
0
输入 #3
9 -1 1 -1 1 -1 1 1 -1 -1
输出 #3
6
输入 #4
8 16 -5 -11 -15 10 5 4 -4
输出 #4
3
Consider the first example. There is only one subsegment with the sum $0$ . It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $1$ between second and third elements of the array.
There are no subsegments having sum $0$ in the second example so you don't need to do anything.
There are no subsegments having sum $0$ in the second example so you don't need to do anything.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted