fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6. static class FastScanner {
  7. private final InputStream in;
  8. private final byte[] buffer = new byte[1 << 16];
  9. private int ptr = 0, len = 0;
  10.  
  11. FastScanner(InputStream is) {
  12. in = is;
  13. }
  14.  
  15. private int read() {
  16. if (ptr >= len) {
  17. ptr = 0;
  18. try {
  19. len = in.read(buffer);
  20. } catch (IOException e) {
  21. return -1;
  22. }
  23. if (len <= 0) return -1;
  24. }
  25. return buffer[ptr++];
  26. }
  27.  
  28. int nextInt() {
  29. int c;
  30. while ((c = read()) <= ' ') {
  31. if (c == -1) return -1;
  32. }
  33. int sign = 1;
  34. if (c == '-') {
  35. sign = -1;
  36. c = read();
  37. }
  38. int val = c - '0';
  39. while ((c = read()) > ' ') {
  40. val = val * 10 + (c - '0');
  41. }
  42. return val * sign;
  43. }
  44. }
  45.  
  46. public static void main(String[] args) {
  47. FastScanner fs = new FastScanner(System.in);
  48. StringBuilder out = new StringBuilder();
  49.  
  50. int Q = fs.nextInt();
  51.  
  52. ArrayDeque<Integer> q1 = new ArrayDeque<>();
  53. PriorityQueue<Integer> q2 = new PriorityQueue<>();
  54.  
  55. while (Q-- > 0) {
  56. int type = fs.nextInt();
  57.  
  58. if (type == 1) {
  59. int x = fs.nextInt();
  60. q1.addLast(x);
  61. }
  62. else if (type == 2) {
  63. if (!q2.isEmpty()) {
  64. out.append(q2.poll()).append('\n');
  65. } else {
  66. out.append(q1.pollFirst()).append('\n');
  67. }
  68. }
  69. else { // type == 3
  70. while (!q1.isEmpty()) {
  71. q2.add(q1.pollFirst());
  72. }
  73. }
  74. }
  75.  
  76. System.out.print(out);
  77. }
  78. }
  79.  
Success #stdin #stdout 0.07s 52664KB
stdin
Standard input is empty
stdout
Standard output is empty