Gym 100247C Victor's Research(有多少区间之和为S)

https://vjudge.net/problem/Gym-100247C

题意:

给出一串数,求有多少个区间的和正好等于S。

思路:
计算处前缀和,并且用map维护一下每个前缀和出现的次数。这样接下来枚举左端点即可,确定了左端点,就可以得出右端点的前缀和应该为多少。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 const int maxn = 200000+5;
 8 typedef long long ll;
 9 
10 int n;
11 ll s, a[maxn], sum[maxn];
12 map<ll,int> mp;
13 
14 int main()
15 {
16     //freopen("in.txt","r",stdin);
17     sum[0] = 0;
18     mp.clear();
19     scanf("%d%lld",&n,&s);
20     for(int i=1;i<=n;i++)
21     {
22         scanf("%lld",&a[i]);
23         sum[i] = sum[i-1]+a[i];
24         mp[sum[i]]++;
25     }
26     ll ans = 0;
27     for(int i=1;i<=n;i++)
28     {
29         ans += mp[s+sum[i-1]];
30         mp[sum[i]]--;
31     }
32     printf("%lld
",ans);
33     return 0;
34 }
原文地址:https://www.cnblogs.com/zyb993963526/p/8047488.html