fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n , k;
  7. cin>>n;
  8. cin>>k;
  9. int arr[n];
  10. for(int i = 0; i<n ; i++){
  11. cin>>arr[i];
  12. }
  13. unordered_map<int,int> mpp;
  14. mpp[0] = 1;
  15.  
  16. for(int i = 1; i< n ; i++){
  17. arr[i] = arr[i-1] + arr[i];
  18. }
  19. int count = 0;
  20. for(int j = 0; j<n ; j++){
  21. int r = arr[j] - k;
  22. count += mpp[r];
  23. mpp[arr[j]] += 1;
  24.  
  25. }
  26. cout<<count<<endl;
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5316KB
stdin
10
8
2 2 3 5 2 2 3 2 2 1
stdout
2