fork download
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class Main {
  8. public static void main(String[] args) throws Exception{
  9. Scanner sc = new Scanner(System.in);
  10. int m = sc.nextInt();
  11. int n = sc.nextInt();
  12. int[] nums = new int[m];
  13. for(int i = 0; i < m; i++){
  14. nums[i] = sc.nextInt();
  15. }
  16. TreeMap<Integer, Integer> blackBox = new TreeMap<>();
  17. blackBox.put(Integer.MAX_VALUE, 1);
  18. int size = 1;
  19. int curKey = Integer.MAX_VALUE;
  20. int curValue = 1;
  21. int curPosition = 0;
  22. for(int i = 0; i < n; i++){
  23. int req = sc.nextInt();
  24. for(int j = size - 1; j < req; j++){
  25. if (blackBox.containsKey(nums[j])) {
  26. blackBox.put(nums[j], blackBox.get(nums[j]) + 1);
  27. if (curKey == nums[j]) {
  28. curValue++;
  29. }
  30. }
  31. else {
  32. blackBox.put(nums[j], 1);
  33. }
  34. if(nums[j] < curKey) {
  35. if (curPosition == 0) {
  36. Map.Entry<Integer, Integer> curEnrty= blackBox.lowerEntry(curKey);
  37. curKey = curEnrty.getKey();
  38. curValue = curEnrty.getValue();
  39. curPosition = curValue - 1;
  40. }
  41. else {
  42. curPosition--;
  43. }
  44. }
  45. }
  46. size = req + 1;
  47. System.out.println(curKey);
  48. if (curPosition == curValue - 1) {
  49. Map.Entry<Integer, Integer> curEnrty = blackBox.higherEntry(curKey);
  50. curKey = curEnrty.getKey();
  51. curValue = curEnrty.getValue();
  52. curPosition = 0;
  53. }
  54. else {
  55. curPosition++;
  56. }
  57. }
  58. }
  59. }
Runtime error #stdin #stdout #stderr 0.12s 2184192KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:862)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at Main.main(Main.java:10)