fork download
  1. import java.util.*;
  2. class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. int n = sc.nextInt();
  6.  
  7. int[] arr = new int[n+1];
  8. int[] prefix = new int[n +1];
  9. for (int i = 1; i <= n; i++) {
  10. arr[i] = sc.nextInt();
  11. prefix[i] = prefix[i - 1] + arr[i];
  12. }
  13.  
  14. int c = 0;
  15. HashMap<Integer, Integer> map = new HashMap<>();
  16. map.put(0, 1);
  17.  
  18. for (int j = 1; j <= n; j++) {
  19. int RHS= prefix[j] - j;
  20. c += map.getOrDefault(RHS, 0);
  21. map.put(RHS, map.getOrDefault(RHS, 0) + 1);
  22. }
  23.  
  24. System.out.println(c);
  25. }
  26. }
  27.  
Success #stdin #stdout 0.15s 56412KB
stdin
4
0 2 0 2
stdout
4