Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

  • 1≤N≤2×105
  • −109≤Ai≤109

All values in input are integers.

https://agc023.contest.atcoder.jp/tasks/agc023_a

题意:给一个数组求出数组中连续字串合为0的个数

Solution

n的平方算法会超时,一开始想着dp可是空间也不够;

首先看样例

6
1 3 -4 2 2 -2
我们计算前面的合为
1 4 0 2 4 2
我们可以发现相同的两个数之间之间经过的数的合一定是0;
例如第一个4 和第二个4 之间是 -4 + 2 +2=0所以我们发现规律只要找到相同的数的个数让他们之间两两连接就是个数
如果合为0代表从第一个开始到他的合就是0所以要额外加上0的个数
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=+;
ll num[maxn];
ll sum[maxn];
ll he(ll n){
if(n==)
return ;
if(n==)
return ;
return (n*(n-))/;
}
map<ll,int>mp;
int main()
{
//std::ios::sync_with_stdio(false);
int n;
scanf("%d",&n);
long long summ=;
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
scanf("%lld",&num[i]);
sum[i]=sum[i-]+num[i];
}
for(int i=;i<=n;i++){
if(mp[sum[i]]==)mp[sum[i]]++;
else{
mp[sum[i]]++;
} }
map<ll,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
//cout<<it->first<<" "<<it->second<<endl;
if(it->first==)
summ+=(he(it->second)+(it->second));
else if(it->second>){
if(it->first!=)
summ+=he(it->second); } }
printf("%lld\n",summ);
return ;
}

AtCoder Grand Contest 023 A - Zero-Sum Ranges的更多相关文章

  1. Atcoder Grand Contest 023 E - Inversions(线段树+扫描线)

    洛谷题面传送门 & Atcoder 题面传送门 毒瘤 jxd 作业-- 首先我们不能直接对所有排列计算贡献对吧,这样复杂度肯定吃不消,因此我们考虑对每两个位置 \(x,y(x<y)\), ...

  2. AtCoder Grand Contest 023 E - Inversions

    Description 给出长度为 \(n\) 序列 \(A_i\),求出所有长度为 \(n\) 的排列 \(P\),满足 \(P_i<=A_i\),求所有满足条件的 \(P\) 的逆序对数之和 ...

  3. AtCoder Grand Contest 023 C - Painting Machines

    Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...

  4. AtCoder Grand Contest 023 F - 01 on Tree

    Description 题面 Solution HNOI-day2-t2 复制上去,删点东西,即可 \(AC\) #include<bits/stdc++.h> using namespa ...

  5. Atcoder Grand Contest 023

    A 略 B 略 C(计数) 题意: 有n个白球排成一行,故有n-1个空隙,我可以给一个空隙对应的两个白球都涂黑.n-1个空隙的一个排列就对应着一个涂黑顺序,定义这个涂黑顺序的价值是“将所有n个球都涂黑 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  8. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

  9. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

随机推荐

  1. 【转】IBatis.Net项目数据库SqlServer迁移至Oracle

    转自:http://www.2cto.com/database/201312/265514.html 最近完成了一个(IBatis.Net+MVC)项目的数据库+代码迁移工作,可把我折腾得~~~ IB ...

  2. Problem Collection I 位运算

    XOR ARC 092B CF 959E xor-MST CF 959F

  3. [洛谷P2482][SDOI2010]猪国杀

    题目大意:猪国杀,又一道大模拟题 题解:模拟,对于一个没有玩过三国杀的人来说,一堆细节不知道,写的十分吃力 卡点:无数,不想说什么了,这告诉我要多玩游戏 C++ Code: #include < ...

  4. [Leetcode] Multiply strings 字符串对应数字相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. NOI2018 D1T1 [NOI2018]归程 解题报告

    P4768 [NOI2018]归程 题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 \(n\) 个节点.\(m\) 条边的无向连通图(节点的编号从 \ ...

  6. [学习笔记]扩展LUCAS定理

    可以先做这个题[SDOI2010]古代猪文 此算法和LUCAS定理没有半毛钱关系. [模板]扩展卢卡斯 不保证P是质数. $C_n^m=\frac{n!}{m!(n-m)!}$ 麻烦的是分母. 如果互 ...

  7. iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    === 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...

  8. 工作总结-js插件

    因最近工作需要,使用了一些js插件,感觉还不错,记录下来以便以后使用. 1.图片轮播插件: 扩展:梦想天空系列:http://www.cnblogs.com/lhb25/archive/2013/01 ...

  9. 使用eclipse插件创建一个web project

    使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...

  10. [05] call by sharing || 共享参数

    转: https://segmentfault.com/a/1190000005177386 众所周知,JavaScript中参数是按值传递的.与访问变量不同,基本类型和引用类型的参数在传递时都如同变 ...