zoukankan      html  css  js  c++  java
  • 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×105
    −109≤Ai≤109
    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;
    }
  • 相关阅读:
    HTTP的OPTIONS请求方法
    K8s -- DaemonSet
    Nginx 变量漫谈(二)
    Nginx 变量漫谈(一)
    通俗地讲,Netty 能做什么?
    CSP AFO后可以公开的情报
    AT1219 歴史の研究
    LuoguP4165 [SCOI2007]组队
    CF708C Centroids(树形DP)
    CF208E Blood Cousins(DSU,倍增)
  • 原文地址:https://www.cnblogs.com/czy-power/p/10583625.html
Copyright © 2011-2022 走看看