|
| 1 | +// Authored by: beberiche |
| 2 | +// Co-authored by: - |
| 3 | +// Link: http://boj.kr/232b374fbc5843168b5c39b49d8ba7c9 |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.io.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + public static void main(String[] args) { |
| 10 | + FastReader rd = new FastReader(); |
| 11 | + |
| 12 | + while (rd.hasMoreLine()) { |
| 13 | + int N = rd.nextInt(); |
| 14 | + int M = rd.nextInt(); |
| 15 | + |
| 16 | + int a[] = new int[N + 1]; |
| 17 | + for (int i = 1; i <= N; i++) { |
| 18 | + int num = rd.nextInt(); |
| 19 | + if (num > 0) a[i] = 1; |
| 20 | + else if (num < 0) a[i] = -1; |
| 21 | + } |
| 22 | + |
| 23 | + int h = (int) Math.ceil((Math.log(N) / Math.log(2))); |
| 24 | + int tree[] = new int[1 << (h + 1)]; |
| 25 | + init(tree, a, 1, 1, N); |
| 26 | + |
| 27 | + for (int i = 0; i < M; i++) { |
| 28 | + String cmd = rd.next(); |
| 29 | + int n1 = rd.nextInt(); |
| 30 | + int n2 = rd.nextInt(); |
| 31 | + |
| 32 | + if (cmd.equals("C")) { |
| 33 | + if (n2 > 0) n2 = 1; |
| 34 | + else if (n2 < 0) n2 = -1; |
| 35 | + else n2 = 0; |
| 36 | + update(tree, 1, 1, N, n1, n2); |
| 37 | + } else { |
| 38 | + int ret = query(tree, 1, 1, N, n1, n2); |
| 39 | + if (ret < 0) { |
| 40 | + System.out.print("-"); |
| 41 | + } else if (ret == 0) { |
| 42 | + System.out.print(0); |
| 43 | + } else { |
| 44 | + System.out.print("+"); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + System.out.println(); |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + private static int query(int[] tree, int idx, int st, int ed, int l, int r) { |
| 54 | + if (r < st || l > ed) { |
| 55 | + return 1; |
| 56 | + } |
| 57 | + |
| 58 | + if (l <= st && ed <= r) { |
| 59 | + return tree[idx]; |
| 60 | + } |
| 61 | + |
| 62 | + int mid = (st + ed) >> 1; |
| 63 | + int left = query(tree, idx * 2, st, mid, l, r); |
| 64 | + int right = query(tree, idx * 2 + 1, mid + 1, ed, l, r); |
| 65 | + return left * right; |
| 66 | + } |
| 67 | + |
| 68 | + private static void update(int[] tree, int idx, int st, int ed, int curr_idx, int val) { |
| 69 | + if (curr_idx < st || ed < curr_idx) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (st == ed) { |
| 74 | + tree[idx] = val; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + int mid = (st + ed) >> 1; |
| 79 | + update(tree, idx * 2, st, mid, curr_idx, val); |
| 80 | + update(tree, idx * 2 + 1, mid + 1, ed, curr_idx, val); |
| 81 | + tree[idx] = tree[idx * 2] * tree[idx * 2 + 1]; |
| 82 | + } |
| 83 | + |
| 84 | + private static void init(int[] tree, int[] a, int idx, int st, int ed) { |
| 85 | + if (st == ed) { |
| 86 | + tree[idx] = a[st]; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + int mid = (st + ed) >> 1; |
| 91 | + init(tree, a, idx * 2, st, mid); |
| 92 | + init(tree, a, idx * 2 + 1, mid + 1, ed); |
| 93 | + tree[idx] = tree[idx * 2] * tree[idx * 2 + 1]; |
| 94 | + } |
| 95 | + |
| 96 | + static class FastReader { |
| 97 | + BufferedReader br; |
| 98 | + StringTokenizer st; |
| 99 | + |
| 100 | + public FastReader() { |
| 101 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 102 | + } |
| 103 | + |
| 104 | + boolean hasMoreLine() { |
| 105 | + String str; |
| 106 | + try { |
| 107 | + if ((str = br.readLine()) != null) { |
| 108 | + st = new StringTokenizer(str); |
| 109 | + return true; |
| 110 | + } |
| 111 | + throw new IOException(); |
| 112 | + } catch (IOException e) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + String next() { |
| 118 | + while (st == null || !st.hasMoreElements()) { |
| 119 | + try { |
| 120 | + st = new StringTokenizer(br.readLine()); |
| 121 | + } catch (IOException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + } |
| 125 | + return st.nextToken(); |
| 126 | + } |
| 127 | + |
| 128 | + int nextInt() { |
| 129 | + return Integer.parseInt(next()); |
| 130 | + } |
| 131 | + |
| 132 | + long nextLong() { |
| 133 | + return Long.parseLong(next()); |
| 134 | + } |
| 135 | + |
| 136 | + double nextDouble() { |
| 137 | + return Double.parseDouble(next()); |
| 138 | + } |
| 139 | + |
| 140 | + String nextLine() { |
| 141 | + String str = ""; |
| 142 | + try { |
| 143 | + str = br.readLine(); |
| 144 | + } catch (IOException e) { |
| 145 | + e.printStackTrace(); |
| 146 | + } |
| 147 | + return str; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/* Solution Description |
| 153 | +
|
| 154 | +1. 세그먼트 트리 응용문제. 출력값을 기준으로 산출과정을 어떻게 도출할 것인지 고민이 필요하다. |
| 155 | +
|
| 156 | +2. 이 문제의 경우, 입력값을 기준으로 이루어지는 최대 곱은 $100^{100000}$ 이므로, 단순 곱셈 연산으로는 문제를 해결하기 어렵다. |
| 157 | +
|
| 158 | +3. 출력값을 미루어볼 때 해당 수의 크기와는 상관없이 양수인지, 음수인지, `0` 인지만 판별하면 된다. |
| 159 | + 따라서, 주어진 값을 `-1, 0, 1` 로 치환하여 계산하면 타입의 최대를 넘지 않은 체 문제를 해결할 수 있다. |
| 160 | +
|
| 161 | + */ |
| 162 | + |
0 commit comments