C. Eugene and an array

Eugene likes working with arrays. And today he needs your help in solving one challenging task.

An array cc is a subarray of an array bb if cc can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array [1,2,3][−1,2,−3] is good, as all arrays [1][−1], [1,2][−1,2], [1,2,3][−1,2,−3], [2][2], [2,3][2,−3], [3][−3] have nonzero sums of elements. However, array [1,2,1,3][−1,2,−1,−3] isn't good, as his subarray [1,2,1][−1,2,−1] has sum of elements equal to 00.

Help Eugene to calculate the number of nonempty good subarrays of a given array aa.

Input

The first line of the input contains a single integer nn (1n2×1051≤n≤2×105)  — the length of array aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (109ai109−109≤ai≤109)  — the elements of aa.

Output

Output a single integer  — the number of good subarrays of aa.

Examples
input
Copy
3
1 2 -3
output
Copy
5
input
Copy
3
41 -41 41
output
Copy
3
Note

In the first sample, the following subarrays are good: [1][1], [1,2][1,2], [2][2], [2,3][2,−3], [3][−3]. However, the subarray [1,2,3][1,2,−3] isn't good, as its subarray [1,2,3][1,2,−3] has sum of elements equal to 00.

In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray [41,41,41][41,−41,41] isn't good, as its subarray [41,41][41,−41] has sum of elements equal to 00.

 

若前缀和数组中出现相同元素,则说明这两个相同元素之间的数组总和为0

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 30005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int main()
{
    int n;
    cin >> n;
    vector<ll> pre(n+1,0);
    map<ll, int> mp;
    ll ans = 0;
    int pos = -1;
    mp[0] = 0;
    rep(i, 1, n)
    {
        cin >> pre[i];
        pre[i] += pre[i - 1];
        if (mp.count(pre[i]))
        {
            pos = max(pos, mp[pre[i]]);
        }
        ans += i - (pos + 1);
        mp[pre[i]] = i;
    }
    cout << ans << endl;
    return 0;
}

 

原文地址:https://www.cnblogs.com/dealer/p/12763541.html