C题

区间和等于S的个数
本文探讨了一个算法问题,即在给定一系列整数的情况下,如何找出所有连续子序列的和等于特定目标值S的方法。通过使用前缀和与哈希映射,文章提供了一种高效解决方案。

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 a 1, …, a n 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 a l, a l + 1, …, a r ( l ≤ r) have the sum a l + a l + 1 + … + a r = 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: a i ( - 109 ≤ a i ≤ 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.

Examples
Input
5 2
-1 1 2 -1 1
Output
5
Input
6 3
3 -2 1 -1 1 2
Output
3

题意:给出n个数,求有多少个区间的和正好等于S。

int main()
{
    sum[0] = 0;
    mp.clear();
    scanf("%d%lld",&n,&s);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        sum[i] = sum[i-1]+a[i];
        mp[sum[i]]++;
    }
    ans = 0;
    for(int i=1;i<=n;i++)
    {
        ans += mp[s+sum[i-1]];
        mp[sum[i]]--;
    }
    printf("%lld\n",ans);
    return 0;
}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值