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
#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