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. [NOIP2017 TG D1T2]时间复杂度

    题目大意:略 题解:模拟 卡点:1.数组忘清空 (考场代码风格独特...) C++ Code: #include<cstdio> #include<cstring> #incl ...

  2. JAX-WS 注解

    一.概述 “基于 XML 的 Web Service 的 Java API”(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注 ...

  3. [04] css 选择器

    1.元素选择器 常见的html标签元素 h1 { color: red; } body { background: red; } 2.分组选择器 例如body和h2标签的字体颜色都是red,使用逗号将 ...

  4. POJ 2456 Aggressive cows---二分搜索法

    ///3.最大化最小值 /** POJ 2456 Aggressive cows Q:一排牛舍有N (2 <= N <= 100,000) 个,位置为x1,...,xN (0 <= ...

  5. RPC-Thrift(三)

    TProtocol TProtocol定义了消息怎么进行序列化和反序列化的. TProtocol的类结构图如下: TBinaryProtocol:二进制编码格式: TCompactProtocol:高 ...

  6. 【BZOJ】5010: [Fjoi2017]矩阵填数

    [算法]离散化+容斥原理 [题意]给定大矩阵,可以每格都可以任意填1~m,给定n个子矩阵,要求满足子矩阵内的最大值为vi,求方案数. n<=10,h,w<=1w. [题解] 此题重点之一在 ...

  7. 【洛谷 P3629】 [APIO2010]巡逻 (树的直径)

    题目链接 容易发现,当加一条边时,树上会形成一个环,这个环上的每个点都是只要走一次的,也就是说我们的答案减少了这个环上点的个数,要使答案最小,即要使环上的点最多,求出直径\(L\),则答案为\(2(n ...

  8. [bzoj1031][JSOI2007]字符加密Cipher——后缀数组

    Brief Description 给定一个长度为n的字符串,你需要对其进行加密. 把字符串围成一个环 显然从任意一个位置开始都可以有一个长度为n的串 把产生的n个串按字典序排序,把这n个串的最后一个 ...

  9. linux system函数分析

    system函数是在应用编程里面想调用外部命令时最方便的方式了,除非想要获取命令行执行的输出信息, 那system就不行了,需要用popen.但是system内部具体怎么实现及怎么处理它的返回值经常被 ...

  10. UVA 10471 Gift Exchanging

    题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用 ...