码迷,mamicode.com
首页 > 其他好文 > 详细

cf C. Eugene and an array

时间:2020-04-12 12:31:11      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:ade   property   题意   head   sum   strong   理解   byte   targe   

C. Eugene and an array
ps:又是一道我理解错题意的题
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 b 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] 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 00 .

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 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,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 .

#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,int> pos;
ll ans,s;
int n,x;
int main() {
    scanf("%d",&n);
    pos[0]=0;
    int q=-1;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        s+=x;
        //printf("s=%lld\n",s);
        //printf("pos.count(s)=%d\n",pos.count(s));
        if (pos.count(s)) q=max(q,pos[s]);
        //cout<<"q="<<q<<endl;
        ans+=i-q-1;
        //printf("ans=%lld\n",ans);
        pos[s]=i;
        //printf("pos[s]=%d\n\n",i);
    }
    printf("%lld\n",ans);
}

 

 

cf C. Eugene and an array

标签:ade   property   题意   head   sum   strong   理解   byte   targe   

原文地址:https://www.cnblogs.com/asunayi/p/12684469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!