fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. int n, m;
  10. int[] a, summ;
  11. Scanner in = new Scanner (System.in);
  12. n = in.nextInt();
  13. a = new int[n];
  14. for(int i = 0; i < n; i++){
  15. a[i] = in.nextInt();
  16. }
  17. summ = new int[n + 1];
  18. summ[0] = 0;
  19. for(int i = 1; i < n + 1; i++){
  20. summ[i] = summ[i - 1] + a[i - 1];
  21. }
  22. m = in.nextInt();
  23. for(int i = 0; i < m; i++){
  24. int l, r;
  25. l = in.nextInt();
  26. r = in.nextInt();
  27. System.out.println(summ[r] - summ[l - 1]);
  28. }
  29. }
  30. }
Success #stdin #stdout 0.07s 2184192KB
stdin
5
1 2 3 4 5
5
1 5
2 3
3 4
2 5
1 4
stdout
15
5
7
14
10