fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner in = new Scanner(System.in);
  14.  
  15. int n = in.nextInt();
  16. int[] arr = new int[n + 1];
  17.  
  18. int[] prefix = new int[n + 1];
  19.  
  20. for(int i=1;i<=n;i++){
  21. arr[i] = in.nextInt();
  22. }
  23.  
  24. for(int i=1;i<=n;i++){
  25. prefix[i] = prefix[i - 1] + arr[i];
  26. }
  27.  
  28.  
  29. int q = in.nextInt();
  30.  
  31. for(int i=0;i<q;i++){
  32. int l = in.nextInt();
  33. int r = in.nextInt();
  34.  
  35. System.out.println(prefix[r + 1] - prefix[l]);
  36.  
  37.  
  38. }
  39. }
  40. }
Success #stdin #stdout 0.13s 54532KB
stdin
3
1 4 1
3
1 1
1 2
0 2
stdout
4
5
6