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. int n = Integer.parseInt(br.readLine());
  13. String[] in = br.readLine().split(" ");
  14. int[] a = new int[n];
  15. for (int i = 0; i < n; i++) {
  16. a[i] = Integer.parseInt(in[i]);
  17. }
  18. int m = Integer.parseInt(br.readLine());
  19. String[] s = br.readLine().split(" ");
  20. int[] b = new int[n];
  21. for (int i = 0; i < m; i++) {
  22. b[i] = Integer.parseInt(s[i]);
  23. }
  24. for (int i = 1; i < n; i++) {
  25. a[i] = a[i - 1] + a[i];
  26. }
  27. for (int i = 0; i < m; i++) {
  28. b[i] = getIndex(a, n, b[i]) + 1;
  29. }
  30. for (int i = 0; i < m; i++) {
  31. pw.print(b[i] + " ");
  32. pw.println();
  33. pw.flush();
  34. }
  35. }
  36.  
  37. private static int getIndex(int[] a, int n, int k) {
  38. int l = 0;
  39. int r = n - 1;
  40. while (l < r) {
  41. int m = l + (( r - l) / 2);
  42. if (a[m] == k) return m;
  43. if (a[m] > k) r = m - 1;
  44. else l = m + 1;
  45. }
  46. if (l == r && l > 0 && r > 0 && l < n && r < n) return l;
  47. if (r == -1 || l == -1) return 0;
  48. if (r > n || l > n) return n - 1;
  49.  
  50. return -1;
  51. }
  52. }
Success #stdin #stdout 0.11s 48212KB
stdin
5
2 7 3 4 9
3
1 25 11
stdout
1 
5 
2