|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_14428_수열과_쿼리_16 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static final StringBuilder sb = new StringBuilder(); |
| 10 | + private static StringTokenizer st; |
| 11 | + |
| 12 | + private static int N, M; |
| 13 | + private static int[] nums; |
| 14 | + private static Node[] segTree; |
| 15 | + |
| 16 | + private static class Node { |
| 17 | + int index; |
| 18 | + int value; |
| 19 | + |
| 20 | + Node(int index, int value) { |
| 21 | + this.index = index; |
| 22 | + this.value = value; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public String toString() { |
| 27 | + return "[ " + this.index + " : " + this.value + " ]"; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + init(); |
| 33 | + sol(); |
| 34 | + } |
| 35 | + |
| 36 | + private static void init() throws IOException { |
| 37 | + N = Integer.parseInt(br.readLine()); |
| 38 | + |
| 39 | + nums = new int[N + 1]; |
| 40 | + st = new StringTokenizer(br.readLine()); |
| 41 | + for (int i = 1; i <= N; i++) { |
| 42 | + nums[i] = Integer.parseInt(st.nextToken()); |
| 43 | + } |
| 44 | + M = Integer.parseInt(br.readLine()); |
| 45 | + |
| 46 | + segTree = new Node[N * 4]; |
| 47 | + initSegTree(1, N, 1); |
| 48 | + } |
| 49 | + |
| 50 | + private static Node initSegTree(int start, int end, int node) { |
| 51 | + if (start == end) { |
| 52 | + segTree[node] = new Node(start, nums[start]); |
| 53 | + return segTree[node]; |
| 54 | + } |
| 55 | + |
| 56 | + int mid = (start + end) / 2; |
| 57 | + |
| 58 | + return segTree[node] = compareNodes(initSegTree(start, mid, node * 2) |
| 59 | + , initSegTree(mid + 1, end, node * 2 + 1)); |
| 60 | + } |
| 61 | + |
| 62 | + private static void sol() throws IOException { |
| 63 | + while (M-- > 0) { |
| 64 | + st = new StringTokenizer(br.readLine()); |
| 65 | + |
| 66 | + int cmd = Integer.parseInt(st.nextToken()); |
| 67 | + int x = Integer.parseInt(st.nextToken()); |
| 68 | + int y = Integer.parseInt(st.nextToken()); |
| 69 | + |
| 70 | + if (cmd == 1) { |
| 71 | + update(1, 1, N, new Node(x, y)); |
| 72 | + } else if (cmd == 2) { |
| 73 | + Node target = getMinIndexNode(1, 1, N, x, y); |
| 74 | + sb.append(target.index).append("\n"); |
| 75 | + } |
| 76 | + } |
| 77 | + bw.write(sb.toString()); |
| 78 | + bw.flush(); |
| 79 | + bw.close(); |
| 80 | + br.close(); |
| 81 | + } |
| 82 | + |
| 83 | + private static void update(int node, int start, int end, Node val) { |
| 84 | + if (val.index < start || end < val.index) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (start == end) { |
| 89 | + segTree[node] = val; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + int mid = (start + end) / 2; |
| 94 | + |
| 95 | + update(node * 2, start, mid, val); |
| 96 | + update(node * 2 + 1, mid + 1, end, val); |
| 97 | + |
| 98 | + segTree[node] = compareNodes(segTree[node * 2], segTree[node * 2 + 1]); |
| 99 | + } |
| 100 | + |
| 101 | + private static Node getMinIndexNode(int node, int start, int end, int left, int right) { |
| 102 | + if (right < start || end < left) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + if (left <= start && end <= right) { |
| 107 | + return segTree[node]; |
| 108 | + } |
| 109 | + |
| 110 | + int mid = (start + end) / 2; |
| 111 | + |
| 112 | + return compareNodes(getMinIndexNode(node * 2, start, mid, left, right) |
| 113 | + , getMinIndexNode(node * 2 + 1, mid + 1, end, left, right)); |
| 114 | + } |
| 115 | + |
| 116 | + private static Node compareNodes(Node n1, Node n2) { |
| 117 | + if (n1 == null) return n2; |
| 118 | + if (n2 == null) return n1; |
| 119 | + |
| 120 | + if (n1.value == n2.value) { |
| 121 | + return n1.index < n2.index ? n1 : n2; |
| 122 | + } |
| 123 | + return n1.value < n2.value ? n1 : n2; |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | +``` |
0 commit comments