Codeforces Round #632 (Div. 2)点这
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
inputCopy
3
1 2 -3
outputCopy
5
inputCopy
3
41 -41 41
outputCopy
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,巧用map记录位置,每一次循环,any加的是以i为尾的子串 符合条件的数量。

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
using namespace std;
typedef long long  ll;
typedef unsigned long long  ull;
int dx[4]= {-1,1,0,0},dy[4]= {0,0,1,-1};
const ll mod=10001;
const ll N=1e6+10;
map<ll,ll> p;
int a[N];
int main()
{iosll last=-1;ll any=0;ll sum=0;ll n;cin>>n;p[0]=0;for(int i=1;i<=n;i++){cin>>a[i];sum+=a[i];if(p.count(sum)) last=max(last,p[sum]);any+=(i-last-1);p[sum]=i;}cout<<any;return 0;
}

Codeforces Round #632 (Div. 2)巧用小技巧相关推荐

  1. Codeforces Round #632 (Div. 2) E. Road to 1600 构造好题

    传送门 文章目录 题意: 思路 题意: 直接白嫖 思路 首先不难发现,n≤2n\le2n≤2的时候是无解的. 现在我们来构造n=3n=3n=3的情况,通过打表可以发现如下矩阵是符合题目要求的: 179 ...

  2. Codeforces Round #632 (Div. 2) F. Kate and imperfection 数论 + 贪心

    传送门 文章目录 题意: 思路: 题意: n≤5e5n\le5e5n≤5e5 思路: 首先有个显然的结论:当往集合中加入一个数xxx的时候,如果存在d∣xd|xd∣x且ddd不在集合中,那么加入ddd ...

  3. Codeforces Round #632 (Div. 2) C. Eugene and an array 思维 + 前缀和

    传送门 文章目录 题意: 思路: 题意: 给定一个长度为nnn的序列aaa,定义一段区间为好区间是这段区间的所有连续子区间的和都不为000,求好区间的个数. 思路: 套路题,定义aia_iai​的前缀 ...

  4. Codeforces Round #632 (Div. 2) C和D和F

    C. Eugene and an array 题目大意就是说给你以1个数组,数组内连续的子序列的子序列中如果没有一个和为0则这就是好序列,求所有的好序列数量 解题思路:总的子串数量为(n + 1) n ...

  5. Codeforces Round #735 (Div. 2)(A-D)没有B

    Codeforces Round #735 (Div. 2) A 一道小思维题 #include <bits/stdc++.h> using namespace std; #define ...

  6. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  7. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  8. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  9. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

最新文章

  1. 理解 neutron(15):Neutron Linux Bridge + VLAN/VXLAN 虚拟网络
  2. Cisco IOS防火墙的安全规则和配置方案
  3. php strtoup,PHP 7 的几处函数安全小变化
  4. 大数据 -- Hadoop集群搭建
  5. anaconda中python的位置_科学网—查询Anaconda安装路径、安装包位置及授予访问权限 - 张伟的博文...
  6. java pdf转为png_java-使用icepdf实现pdf转换成png
  7. 从零开始学前端:中括号代替点操作,获取对象,自定义标签属性 --- 今天你学习了吗?(JS:Day3)
  8. 如何通过页面静态化提升论坛性能
  9. Maven创建servlet项目演示(三)
  10. 四叶草启动linux黑屏,四叶草剧场黑屏进不去解决方法一览
  11. rk3399_android7.1调试USB接口的TP记录
  12. 循环数组实现队列的四种方式
  13. 利用Aspose.PDF for .NET实现pdf转word
  14. cocos2dx资料汇总 - 持续更新
  15. pdf文件转换成word转换器
  16. moTzxx-CMS —— [一个基于PHP代码的后台管理系统(ThinkPHP5.1.40)]
  17. 启发式路径搜索算法介绍
  18. Datawhale--组队学习第12期--python爬虫基础学习---task0/task1环境配置和网页请求基础
  19. 你觉得作为一个开发人员应该具备哪些素质呢?
  20. UG NX中的一条曲线的点集进行导出时顺序是乱的,如何进行排序或按照导出点集

热门文章

  1. 18张难以置信的照片,封面这张你就没见过
  2. PL/SQL Developer使用技巧总结
  3. python基金会主席入门教程_Python基金会-文件IO操作,python,基础
  4. python的缩进机制是其缺点之一_Python 的缩进是不是反人类的设计?
  5. python小甲鱼爬虫妹子_【Python学习日记】B站小甲鱼:爬虫
  6. 2021年度最全面JVM虚拟机,类加载过程与类加载器
  7. android 获取当前画布,Android硬件位图填坑之获取硬件画布
  8. 苹果手机运行python_iPhone是卖的最好的手机?用Python照样把他玩弄鼓掌之间!
  9. uc3842开关电源电路图_UC3842 的原理及应用详解 (上)
  10. The Last Non-zero Digit POJ - 1150(n!mod p)