AC_code
1 /*map维护每一个前缀和,这样接下来枚举左端点即可,确定左端点,s+左端点就是右端点*/ 2 #include <iostream> 3 #include <cstdio> 4 #include <cmath> 5 #include <map> 6 #include <ctime> 7 #include <algorithm> 8 #include <functional> 9 #include <cstring> 10 using namespace std; 11 typedef long long ll; 12 typedef unsigned long long ull; 13 const int maxn=2e5+20; 14 15 int n; 16 ll s, a[maxn],sum[maxn]; 17 map<ll,int>mp;///map维护每个前缀和出现的次数 18 19 int main() 20 { 21 sum[0]=0; 22 mp.clear(); 23 scanf("%d%lld",&n,&s); 24 for(int i=1;i<=n;i++) 25 { 26 scanf("%lld",&a[i]); 27 sum[i]=sum[i-1]+a[i]; 28 mp[sum[i]]++; 29 } 30 ll ans=0; 31 for(int i=1;i<=n;i++) 32 { 33 ans+=mp[s+sum[i-1]]; 34 mp[sum[i]]--; 35 } 36 printf("%lld ",ans); 37 return 0; 38 }