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. 我理解的数据结构(三)—— 队列(Queue)

    我理解的数据结构(三)-- 队列(Queue) 一.队列 队列是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另一端(队首)取出元素 队列是一种先进先出的数据结 ...

  2. What identity values you get with the @@IDENTITY and SCOPE_IDENTITY functions

    --测试表及数据 CREATE TABLE TZ (   Z_id  int IDENTITY(1,1)PRIMARY KEY,   Z_name varchar(20) NOT NULL) INSE ...

  3. Automatic Tuning of Memory Management

    4.2.2 Automatic Tuning of Memory Management Two memory management initialization parameters, MEMORY_ ...

  4. MySQL用Load Data local infile 导入部分数据后中文乱码

    今天在两台MySQL服务器之间导数据,因为另一个MySQL服务器是测试用的,差一个月的数据,从现有MySQL服务器select到一个文件,具体语句是: select * from news where ...

  5. Porting from Oracle to MySQL

    A potential customer asked my about porting her application from Oracle Database to MySQL. I always ...

  6. 论文WAN Optimized Replication of Backup Datasets Using Stream-Informed Delta Compression

         这是EMC的备份小组发表在FAST12上的论文,主要是结合重删和差量数据压缩的方法,达到更高的数据压缩率.并且作者使用了一种基于数据流的差量数据压缩,消除了对索引的需求.通过测试达到的压缩效 ...

  7. mybatis中useGeneratedKeys和keyProperty的使用

    领域模型主键属性是shopId,使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型shopId属性中,配置参考如下:<insert id=& ...

  8. 拒绝switch,程序加速之函数指针数组

    先看一个使用switch语句的程序: #include <stdio.h> #include <time.h> //加法 int add(int a,int b) { retu ...

  9. CF D. Beautiful numbers (数位dp)

    http://codeforces.com/problemset/problem/55/D Beautiful Numbers : 这个数能整除它的全部位上非零整数.问[l,r]之间的Beautifu ...

  10. 【翻译自mos文章】Oracle GoldenGate 怎么在源头的传输进程和目的端的server/collector进程之间分配 port?

    Oracle GoldenGate 怎么在源头的传输进程和目的端的server/collector进程之间分配 port? 来源于: How Does GoldenGate Allocates Por ...