fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Main
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. new Main().run();
  14. }
  15.  
  16.  
  17. class Item implements Comparable<Item> {
  18. int x;
  19. int ind;
  20. Item(int x, int ind) {
  21. this.x = x;
  22. this.ind = ind;
  23. }
  24.  
  25. public int compareTo(Item o) {
  26. if (x < o.x)
  27. return -1;
  28. if (x > o.x)
  29. return 1;
  30. return ind - o.ind;
  31. }
  32. }
  33.  
  34.  
  35. void run() {
  36. try {
  37. int n = nextInt();
  38. TreeSet<Item> tree = new TreeSet<Item>();
  39. int[] a = new int[n];
  40. for (int i = 0; i < n; i++) {
  41. a[i] = nextInt();
  42. tree.add(new Item(a[i], i));
  43. }
  44. int m = nextInt();
  45. for (int i = 0; i < m; i++) {
  46. int q = nextInt();
  47. if (q == 1) {
  48. int x = nextInt();
  49. Item toSearch = new Item(x, -1);
  50. Item ceil = tree.ceiling(toSearch);
  51. if (ceil == null || ceil.x != x)
  52. out.println("Error");
  53. else {
  54. out.printf("%d\n", ceil.ind + 1);
  55. }
  56. } else {
  57. int pos = nextInt() - 1;
  58. int x = nextInt();
  59. Item toDel = new Item(a[pos], pos);
  60. tree.remove(toDel);
  61. a[pos] = x;
  62. Item toAdd = new Item(a[pos], pos);
  63. tree.add(toAdd);
  64. }
  65. }
  66. out.close();
  67. } catch (Exception e) {
  68. e.printStackTrace(out);
  69. out.flush();
  70. out.close();
  71. //while (true);
  72. }
  73. }
  74.  
  75. int nextInt() throws Exception {
  76. st.nextToken();
  77. return (int) st.nval;
  78. }
  79.  
  80. }
Success #stdin #stdout 0.08s 380288KB
stdin
5
1 2 3 4 5
5
1 2
1 5
2 1 7
1 3
1 6
stdout
2
5
3
Error