fork(14) download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4.  
  5. int GetTotal(std::vector<int> arr, int x, int y) {
  6. std::vector<int> x_y_diffs;
  7. x_y_diffs.push_back(0);
  8. for (int i = 0; i < arr.size(); i++) {
  9. int val = x_y_diffs[i];
  10. if (arr[i] == x) val++;
  11. else if (arr[i] == y) val--;
  12. x_y_diffs.push_back(val);
  13. }
  14.  
  15. std::map<int, int> histogram;
  16. int result = 0;
  17. for (int x : x_y_diffs) {
  18. result += histogram[x];
  19. histogram[x]++;
  20. }
  21. return result;
  22. }
  23.  
  24. int main(void) {
  25. std::vector<int> arr = {1, 0 , 2, 1};
  26. std::cout << GetTotal(arr, 1, 0);
  27. }
  28.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
4