CF 2013-2014 Samara SAU ACM ICPC Quarterfinal Qualification Contest C.Victor's Research

C. Victor's Research
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Unacknowledged scientist Victor conducts a pseudoscientific research of the relation between integers that cross his mind and the integer that comes into his assistant's mind. He wrote the integers a1, ..., an which had crossed his mind. Then it turned up that the integer s had come into his assistant's mind. Victor wants to determine how many consecutive non-empty sets of integers al, al + 1, ..., ar (l ≤ r) have the sum al + al + 1 + ... + ar = s.

Input

The first line contains two integers separated by space: n and s (1 ≤ n ≤ 200000,  - 2·1014 ≤ s ≤ 2·1014) — the number of integers which crossed Victor's mind and the integer that came into his assistant's mind.

The second line contains n integers separated by space: ai ( - 109 ≤ ai ≤ 109) — the integers which crossed Victor's mind.

Output

Output the only integer — the number of consecutive non-empty sets of integers which have the sum s.

Sample test(s)
Input
5 2
-1 1 2 -1 1
Output
5
Input
6 3
3 -2 1 -1 1 2
Output
3

解析

给一串数,求区间和为k的子区间个数

动态规划的思想。首先求一个前缀和,当Prefix_Sum[R] - Prefix_Sum[L] = K时,区间(L,R]为满足条件的区间。

等式变形Prefix_Sum[L] = Prefix_Sum[R] - K 。那么R以前,前缀和恰为Prefix_Sum[R] - K的Prefix_Sum[L]就可以和R凑成一个合法的区间。

也就说记录R前Prefix_Sum[L]出现的次数就可以了。

#include<cstdio>
#include<map>

using namespace std;

typedef long long LL;

LL Prefix_Sum[200100];
map<LL,LL> Map;
LL N,K;

int main()
{
	scanf("%I64d%I64d",&N,&K);
	for(int i=1;i<=N;i++)
	{
		scanf("%I64d",&Prefix_Sum[i]);
		Prefix_Sum[i]+=Prefix_Sum[i-1];
	}

	LL ans=0; Map[0]=1;
	for(int i=1;i<=N;i++)//(L,R]
	{
		ans+=Map[(Prefix_Sum[i]-K)];
		Map[Prefix_Sum[i]]++;
	}
	printf("%I64d",ans);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值