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. }
  14. }
  15.  
  16. public class Main {
  17. public static void main(String[] args) throws IOException {
  18. // Use BufferedReader for fast input
  19.  
  20. int N = Integer.parseInt(br.readLine().trim());
  21. long[] prefix = new long[N + 1]; // prefix[k] = sum of first k elements
  22.  
  23. st = new StringTokenizer(br.readLine());
  24. for (int i = 0; i < N; i++) {
  25. long val = Long.parseLong(st.nextToken());
  26. prefix[i + 1] = prefix[i] + val;
  27. }
  28.  
  29. int Q = Integer.parseInt(br.readLine().trim());
  30. StringBuilder sb = new StringBuilder();
  31.  
  32. for (int q = 0; q < Q; q++) {
  33. st = new StringTokenizer(br.readLine());
  34. int i = Integer.parseInt(st.nextToken());
  35. int j = Integer.parseInt(st.nextToken());
  36.  
  37. if (i > j) { // handle case if i > j
  38. int temp = i;
  39. i = j;
  40. j = temp;
  41. }
  42.  
  43. long sum = prefix[j + 1] - prefix[i];
  44. sb.append(sum).append("\n");
  45. }
  46.  
  47. System.out.print(sb.toString());
  48. }
  49. }
  50.  
Success #stdin #stdout 0.07s 52592KB
stdin
Standard input is empty
stdout
Standard output is empty