Zero-Sum Ranges

6771: Zero-Sum Ranges

时间限制: 1 Sec  内存限制: 128 MB
提交: 155  解决: 51
[提交] [状态] [命题人:admin]

题目描述
We have an integer sequence A, whose length is N.
Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints
1≤N≤2×10 5
−10 9≤Ai≤10 9
All values in input are integers.

 

输入
Input is given from Standard Input in the following format:
N
A1 A2 … AN

 

输出
Find the number of the non-empty contiguous subsequences of A whose sum is 0.

 

样例输入

复制样例数据

6
1 3 -4 2 2 -2
样例输出
3

 

提示

There are three contiguous subsequences whose sums are 0: (1,3,−4), (−4,2,2) and (2,−2).

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=2e5+6;
int n;
ll d[maxn],ans;
map<ll,int>p;
int main()
{
    cin>>n;
    p[0]++;
    for(int i=1;i<=n;i++)cin>>d[i];
    for(int i=1;i<=n;i++){
        d[i]=d[i]+d[i-1];
        ans+=p[d[i]];
        p[d[i]]++;
    }
    cout<<ans<<endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/czy-power/p/10583625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值