Eugene and an array(思維)

Eugene and an array

time limit per test:1.5 seconds
memory limit per test:256 megabytes
input:standard input
outpu:tstandard output

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

An array c is a subarray of an array b if c can be obtained from b 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] is good, as all arrays [−1], [−1,2], [−1,2,−3], [2], [2,−3], [−3] have nonzero sums of elements. However, array [−1,2,−1,−3] isn’t good, as his subarray [−1,2,−1] has sum of elements equal to 0.

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

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

The second line of the input contains n integers a1,a2,…,an (−109≤ai≤109) — the elements of a.

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

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

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

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

題意:如果一個數組的所有子數組,每個子數組數組元素和不爲0,那麼這是一個好數組,給你一個數組,計算它的子數組有多少是好數組

先求數組的前綴和,遍歷前綴和,對於每一位sum[i],找它之前和他最近的sum[ j ] ( j<i ),結果就加上i-j,複雜度 O(n*log n)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+9;
const int mod = 1e9+7;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;
const double eps = 1e-7;
#define inf 0x3f3f3f3f3f3f3f3f
//isdigit(char)
//isalpha(char)//Uppercase:1,Lowercase:2
//isalnum(char) //isdigit||isalpha
//str_not_find string::nops = unsigned int(-1)


template<typename T>
void output(T *arr,int length,int END = 0)
{
    for(int i = 0; i<length; i++) cout<<arr[i]<<((END|(i==length-1))?'\n':' ');
}

template<typename T>
void input(T *arr,int length)
{
    for(int i = 0; i<length; i++) cin>>arr[i];
}

template<class T>
class compare
{
public:
    bool operator () (T a,T b)const
    {
        return a>b;
    }
};//ex: compare<int>,compare<long long>//sort by rule(now from big to small)

long long a[maxn],sum[maxn];
map<long long,long long>mp;

int main()
{
    long long i,j,m,n,ans,anss,num = 2e14,pre;
    scanf("%lld",&n);
    for(i = 1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        sum[i] = sum[i-1]+a[i];
    }
    ans = 0;
    pre = 1;
    mp[2e14] = 1;

    for(i = 1; i<=n; i++)
    {
        if(a[i]==0)
        {
            pre = i+1;
            continue;
        }
        if(mp.count(sum[i]+2e14))
        {
            num = mp[sum[i]+2e14];
            int pos = max(pre,num+1);
            ans += i-pos+1;
        //    cout<<i-pos+1<<endl;
            pre = max(pre,num+1);
        }
        else
        {
            ans += i-pre+1;
          //  cout<<i-pre+1<<endl;
        }

        // cout<<(i-pre+2)*(i-pre+1)/2<<endl;
        mp[sum[i]+2e14] = i+1;
    }
    printf("%lld\n",ans);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章