fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. Scanner sc = new Scanner(System.in);
  9.  
  10. int n = sc.nextInt();
  11.  
  12. long[] prefix = new long[n + 1];
  13.  
  14. for (int i = 0; i < n; i++) {
  15.  
  16. long x = sc.nextLong();
  17.  
  18. prefix[i + 1] = prefix[i] + x;
  19. }
  20.  
  21. int q = sc.nextInt();
  22.  
  23. while (q-- > 0) {
  24.  
  25. int l = sc.nextInt();
  26. int r = sc.nextInt();
  27.  
  28. long answer = prefix[r + 1] - prefix[l];
  29.  
  30. System.out.println(answer);
  31. }
  32.  
  33. sc.close();
  34. }
  35. }
Success #stdin #stdout 0.12s 56484KB
stdin
3
1 4 1
3
1 1
1 2
0 2
stdout
4
5
6