fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int countOfSubarray(vector<int>& a , int x , int y , int z , int w , int d){
  5. int n = a.size();
  6. map<tuple<int,int,int,int>,int>freq;
  7. int ans = 0;
  8. freq[{0,0,0,0}] = 1;
  9. int cx = 0 , cy = 0 , cz=0 , cw = 0, cd = 0;
  10. for(int i=0;i<n;i++){
  11. if(a[i]==x)cx++;
  12. if(a[i]==y)cy++;
  13. if(a[i]==z)cz++;
  14. if(a[i]==w)cw++;
  15. if(a[i]==d)cd++;
  16.  
  17. int diff1 = cx-cy;
  18. int diff2 = cy-cz;
  19. int diff3 = cz-cw;
  20. int diff4 = cw-cd;
  21. ans += freq[{diff1,diff2,diff3,diff4}];
  22. freq[{diff1,diff2,diff3,diff4}]++;
  23. }
  24. return ans;
  25. }
  26.  
  27. int main() {
  28. int n;
  29. cin>>n;
  30. int x,y,z,w,d;
  31. cin>>x>>y>>z>>w>>d;
  32. vector<int>a(n);
  33. for(int i=0;i<n;i++){
  34. cin>>a[i];
  35. }
  36. cout<<countOfSubarray(a,x,y,z,w,d);
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5288KB
stdin
10
1 2 3 4 5
1 2 3 4 5 1 3 5 2 1
stdout
2