CRAN02 - Roommate Agreement

Leonard was always sickened by how Sheldon considered himself better than him. To decide once and for all who is better among them they decided to ask each other a puzzle. Sheldon pointed out that according to Roommate Agreement Sheldon will ask first. Leonard seeing an opportunity decided that the winner will get to rewrite the Roommate Agreement.

Sheldon thought for a moment then agreed to the terms thinking that Leonard will never be able to answer right. For Leonard, Sheldon thought of a puzzle which is as follows. He gave Leonard n numbers, which can be both positive and negative. Leonard had to find the number of continuous sequence of numbers such that their sum is zero.

For example if the sequence is- 5, 2, -2, 5, -5, 9

There are 3 such sequences

2, -2

5, -5

2, -2, 5, -5

Since this is a golden opportunity for Leonard to rewrite the Roommate Agreement and get rid of Sheldon's ridiculous clauses, he can't afford to lose. So he turns to you for help. Don't let him down.

Input

First line contains T - number of test cases

Second line contains n - the number of elements in a particular test case.

Next line contain n elements, ai  (1<=i<= n) separated by spaces.

Output

The number of such sequences whose sum if zero.

Constraints

1<=t<=5

1<=n<=10^6

-10<= ai <= 10

Example

Input:

2

4

0 1 -1 0

6

5 2 -2 5 -5 9

Output:

6
3

题意

给你一个序列,里面n(10^6)个数字,问这些数字相加为0的区间有多少个

思路

看样例解释我们可以知道,可以利用前缀和来计算,a[i]=a[i]+a[i-1]这样,然后用map来存a[i]出现的次数,如果有x个a[i]出现,则说明其中存在序列和为0的情况,

并且可能的情况为1~x-1种,如果a[i]刚好=0,则还要加上当前这个。

 /*
Name: hello world.cpp
Author: AA
Description: 唯代码与你不可辜负
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
LL a[n];
map<LL, LL> cnt;
cin >> a[];
cnt[a[]]++;
for(int i = ; i < n; i++) {
cin >> a[i];
a[i] += a[i - ];
cnt[a[i]]++;
}
map<LL, LL>::iterator it;
LL ans = ;
for(it = cnt.begin(); it != cnt.end(); it++) {
if(it->first == )
ans += it->second + it->second * (it->second - ) / ;
else
ans += it->second * (it->second - ) / ;
}
cout << ans << endl;
}
return ;
}

SPOJ-CRAN02 - Roommate Agreement(前缀和)的更多相关文章

  1. SPOJ Time Limit Exceeded(高维前缀和)

    [题目链接] http://www.spoj.com/problems/TLE/en/ [题目大意] 给出n个数字c,求非负整数序列a,满足a<2^m 并且有a[i]&a[i+1]=0, ...

  2. SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)

    题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...

  3. SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)

    Problem description. The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1 ...

  4. [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)

    题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0​(n) be the number of positive diviso ...

  5. SPOJ 7258 SUBLEX 后缀数组 + 二分答案 + 前缀和

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. SPOJ REPEATS 后缀数组

    题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...

  8. SPOJ DISUBSTR 后缀数组

    题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...

  9. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

随机推荐

  1. 常用rides命令

    rides使用步骤 1.源代码构建安装 1.下载,Linux下命令wget http://redis.io/download下载redis的包 2.解归档Linux下命令tar -xvf redis- ...

  2. 9.简单理解ES分布式

    主要知识点:     1.Elasticsearch对复杂分布式机制的透明隐藏特性 2.Elasticsearch的垂直扩容与水平扩容 3.增减或减少节点时的数据rebalance 4.master节 ...

  3. 学习EXTJS6(5)基本功能-进度条组件

    Ext.ProgressBar有二种模式:手动和自动:手动:自己控制显示.进度.更新.清除.自动只需要调用Wait方法即可. 配置项:   配置项 类型 说明 renderTo String 指定页面 ...

  4. poj 3177&&3352 求边双联通分量,先求桥,然后求分量( 临界表代码)

    /*这道题是没有重边的,求加几条边构成双联通,求边联通分量,先求出桥然后缩点,成一个棵树 找叶子节点的个数*/ #include<stdio.h>//用容器写在3177这个题上会超内存,但 ...

  5. servlet理解

    可得到一个结论:该JSP页面中的每个字符都由test1_jsp.java文件的输出流生成. 根据上面的JSP页面工作原理图,可以得到如下四个结论: — JSP文件必须在JSP服务器内运行. — JSP ...

  6. Yarn的工作流程

    http://study.163.com/course/courseLearn.htm?courseId=1002887002#/learn/video?lessonId=1003346099& ...

  7. 【微信小程序】:小程序,新场景

    前言: 我们频繁进入的地方,是场景. 手机.是场景:浏览器.是场景.事实上,微信,也是场景-- 微信要做的是占领很多其它用户时间.占领很多其它应用场景.占领很多其它服务入口.这是商业本质想去垄断要做的 ...

  8. APP漏洞自动化扫描专业评测报告(上篇)

    一.前言 随着Android操作系统的快速发展,运行于Android之上的APP如雨后春笋般涌现.由于一些APP的开发者只注重APP业务功能的实现,对APP可能出现安全问题不够重视,使得APP存在较多 ...

  9. Codeforces 115A- Party(DFS)

    A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...

  10. luogu1890 gcd区间

    题目大意:给定一行n个正整数a[1]..a[n].m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 因为gcd满足交换律和结合律,所以用线段树维护区间上的gcd值即可. ...