[CF1333C]Eugene and an array

摘要:
[原始问题]Eugene lakes使用阵列。今天我们需要我们的帮助来解决一个挑战性的任务。通过从开始时删除几个(可能是零个或全部)元素,可以从bbc中获得阵列阵列和阵列阵列

【原题】

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 goodif for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array −1,2,−3is good, as all arrays −1, −1,2, −1,2,−3, 2, 2,−3, −3have nonzero sums of elements. However, array −1,2,−1,−3isn't good, as his subarray −1,2,−1has sum of elements equal to 00.

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

Input

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

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

Output

Output a single integer — the number of goodsubarrays of a.

Examples

input

Copy

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,−3isn't good, as its subarray 1,2,−3has sum of elements equal to 0.

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

【思路】

前缀和, map记录前缀和是否出现过及其最后出现位置, num记录最后不可行位置。

1 #include <algorithm>
2 #include <cmath>
3 #include <cstdio>
4 #include <cstring>
5 #include <list>
6 #include <map>
7 #include <iostream>
8 #include <queue>
9 #include <set>
10 #include <stack>
11 #include <string>
12 #include <vector>
13 #include <iomanip>
14 #define LL long long
15 #define inf 0x3f3f3f3f
16 #define INF 0x3f3f3f3f3f3f
17 #define PI 3.1415926535898
18 #define F first
19 #define S second
20 #define lson  rt << 1
21 #define rson  rt << 1 | 1
22 using namespacestd;
23 
24 const int maxn = 2e5 + 7;
25 const int maxm = 1e4 + 7;
26 intn, m;
27 LL a[maxn], sum[maxn];
28 map<LL, int>mp;
29 
30 LL read()
31 {
32     LL x = 0, f = 1;char ch =getchar();
33     while (ch < '0' || ch>'9') { if (ch == '-')f = -1;ch =getchar(); }
34     while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0';ch =getchar(); }
35     return x *f;
36 }
37 
38 intmain()
39 {
40     ios::sync_with_stdio(false);
41     cin.tie(0);
42     n =read();
43     for (int i = 1; i <= n; i++)
44 {
45         a[i] =read();
46         sum[i] = sum[i - 1] +a[i];
47 }
48     LL cnt = 0;
49     int num = 0;
50     mp[0] = 0;
51     for (int i = 1; i <= n; i++)
52 {
53         if (mp.count(sum[i])) num = max(mp[sum[i]] + 1, num);
54         mp[sum[i]] =i;
55         cnt += i -num;
56 }
57     cout << cnt <<endl;
58 }

免责声明:文章转载自《[CF1333C]Eugene and an array》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ajax页面刷新小错误(提交按钮type必须为button,而不能是submit)MusicXML 3.0 (9) 小节线、反复线、终止线下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JS-Array-新增方法

1. filter( )    var arr = [5,4,3,2,1];   newarr = arr.filter((item)=>{ return item<3 }) ;  // => [2,1] 2. every( ) 和 some( )   var arr = [5,4,3,2,1];   newarr = arr.every...

Qt中gb2312/GBK的URL编解码函数

编码函数: QByteArray encodeURI(QString str) { QByteArray array; QTextCodec *codec=QTextCodec::codecForName("GBK"); QByteArray tmpArray; tmpArray = codec->fromUnicod...

Scala学习——模式匹配

scala模式匹配 1.基础match case(类似java里switch case,但功能强大些) object MatchApp { def main(args: Array[String]): Unit = { val is = Array("a","b","c","d") val i = is(Random.nextInt...

echarts 加单位 加数字单位

formatter: function (array) { return array.seriesName + "<br/>" + array.marker + array.name + "平均体重" + array.value + "KG" } 完整代码: <div id="Cha...

Numpy 基本除法运算和模运算

基本算术运算符+、-和*隐式关联着通用函数add、subtract和multiply 在数组的除法运算中涉及三个通用函数divide、true_divide和floor_division,以及两个对应的运算符/和// 1. 数组的除法运算 import numpy as np # divide函数在整数和浮点数除法中均只保留整数部分(python3中的np...

JS 的map和array集合组合返回JSON字符串

使用map 和array 返回自定义对象的JSON字符串: function getObjectJSON() {   var array = new Array();   for (var i = 0; i < 5; i++) {     var map = {};     map[1] = "张三";     map[2] = "李四";...