|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.PriorityQueue; |
| 4 | +import java.util.Queue; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class BJ_14427_수열과_쿼리_15 { |
| 8 | + |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static final StringBuilder sb = new StringBuilder(); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int N, M; |
| 15 | + private static Node[] nodes; |
| 16 | + private static Queue<Node> pq; |
| 17 | + |
| 18 | + private static class Node implements Comparable<Node> { |
| 19 | + int idx; |
| 20 | + int val; |
| 21 | + |
| 22 | + Node(int idx, int val) { |
| 23 | + this.idx = idx; |
| 24 | + this.val = val; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public int compareTo(Node o) { |
| 29 | + if (this.val == o.val) { |
| 30 | + return Integer.compare(this.idx, o.idx); |
| 31 | + } |
| 32 | + return Integer.compare(this.val, o.val); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String toString() { |
| 37 | + return "[" + idx + " " + val + "]"; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static void main(String[] args) throws IOException { |
| 42 | + init(); |
| 43 | + sol(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void init() throws IOException { |
| 47 | + N = Integer.parseInt(br.readLine()); |
| 48 | + |
| 49 | + nodes = new Node[N + 1]; |
| 50 | + pq = new PriorityQueue<>(); |
| 51 | + st = new StringTokenizer(br.readLine()); |
| 52 | + for (int i = 1; i <= N; i++) { |
| 53 | + nodes[i] = new Node(i, Integer.parseInt(st.nextToken())); |
| 54 | + pq.offer(nodes[i]); |
| 55 | + } |
| 56 | + |
| 57 | + M = Integer.parseInt(br.readLine()); |
| 58 | + } |
| 59 | + |
| 60 | + private static void sol() throws IOException { |
| 61 | + while (M-- > 0) { |
| 62 | + st = new StringTokenizer(br.readLine()); |
| 63 | + |
| 64 | + int cmd = Integer.parseInt(st.nextToken()); |
| 65 | + |
| 66 | + if (cmd == 1) { |
| 67 | + int a = Integer.parseInt(st.nextToken()); |
| 68 | + int b = Integer.parseInt(st.nextToken()); |
| 69 | + |
| 70 | + nodes[a] = new Node(a, b); // 객체 참조 변경 시 pq 순서 꼬일 수 있음,, |
| 71 | + pq.offer(nodes[a]); // priority queue는 내부 객체가 변경되어도 재정렬 되지 않음 |
| 72 | + } else if (cmd == 2) { |
| 73 | + // 업데이트 되기 이전 값은 poll() |
| 74 | + while (!pq.isEmpty() && pq.peek().val != nodes[pq.peek().idx].val) { |
| 75 | + pq.poll(); |
| 76 | + } |
| 77 | + sb.append(pq.peek().idx).append("\n"); |
| 78 | + } |
| 79 | + } |
| 80 | + bw.write(sb.toString()); |
| 81 | + bw.flush(); |
| 82 | + bw.close(); |
| 83 | + br.close(); |
| 84 | + } |
| 85 | + |
| 86 | +} |
| 87 | +``` |
0 commit comments