|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class JM_코드트리_메신저 { |
| 8 | + static class Room { |
| 9 | + int c; |
| 10 | + boolean alert; |
| 11 | + int authority; |
| 12 | + |
| 13 | + int parent; |
| 14 | + int leftChild; |
| 15 | + int rightChild; |
| 16 | + |
| 17 | + int[] effect; |
| 18 | + |
| 19 | + public Room(int c) { |
| 20 | + this.c = c; |
| 21 | + this.alert = true; |
| 22 | + this.parent = -1; |
| 23 | + this.leftChild = -1; |
| 24 | + this.rightChild = -1; |
| 25 | + this.effect = new int[DEPTH + 1]; |
| 26 | + } |
| 27 | + |
| 28 | + public void toggleAlert() { |
| 29 | + this.alert = !this.alert; |
| 30 | + } |
| 31 | + } |
| 32 | + static int N; |
| 33 | + static int Q; |
| 34 | + static Room[] rooms; |
| 35 | + static final int DEPTH = 20; |
| 36 | + |
| 37 | + private static void update(int c) { |
| 38 | + while (c != 0) { |
| 39 | + Room curr = rooms[c]; |
| 40 | + Arrays.fill(curr.effect, 0); |
| 41 | + for (int i = 0; i <= curr.authority; i++) { |
| 42 | + rooms[c].effect[i]++; |
| 43 | + } |
| 44 | + |
| 45 | + if(curr.leftChild != -1 && rooms[curr.leftChild].alert) { |
| 46 | + for (int i = 1; i <= DEPTH; i++) { |
| 47 | + curr.effect[i - 1] += rooms[curr.leftChild].effect[i]; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if(curr.rightChild != -1 && rooms[curr.rightChild].alert) { |
| 52 | + for (int i = 1; i <= DEPTH; i++) { |
| 53 | + curr.effect[i - 1] += rooms[curr.rightChild].effect[i]; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + c = rooms[c].parent; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void swapParent(int c1, int c2) { |
| 62 | + int p1 = rooms[c1].parent; |
| 63 | + int p2 = rooms[c2].parent; |
| 64 | + |
| 65 | + if(rooms[p1].leftChild == c1) { |
| 66 | + rooms[p1].leftChild = c2; |
| 67 | + } else if(rooms[p1].rightChild == c1) { |
| 68 | + rooms[p1].rightChild = c2; |
| 69 | + } |
| 70 | + |
| 71 | + if(rooms[p2].leftChild == c2) { |
| 72 | + rooms[p2].leftChild = c1; |
| 73 | + } else if(rooms[p2].rightChild == c2) { |
| 74 | + rooms[p2].rightChild = c1; |
| 75 | + } |
| 76 | + |
| 77 | + rooms[c1].parent = p2; |
| 78 | + rooms[c2].parent = p1; |
| 79 | + |
| 80 | + update(c1); |
| 81 | + update(c2); |
| 82 | + } |
| 83 | + |
| 84 | + private static void changeAuthority(int c, int power) { |
| 85 | + rooms[c].authority = power > DEPTH ? DEPTH : power; |
| 86 | + update(c); |
| 87 | + } |
| 88 | + |
| 89 | + private static void toggleNotification(int c) { |
| 90 | + rooms[c].toggleAlert(); |
| 91 | + update(c); |
| 92 | + } |
| 93 | + |
| 94 | + private static void initEffect(int c) { |
| 95 | + if(c == -1) return; |
| 96 | + |
| 97 | + Room curr = rooms[c]; |
| 98 | + for (int i = 0; i <= curr.authority; i++) { |
| 99 | + rooms[c].effect[i]++; |
| 100 | + } |
| 101 | + |
| 102 | + initEffect(curr.leftChild); |
| 103 | + initEffect(curr.rightChild); |
| 104 | + |
| 105 | + if(curr.leftChild != -1) { |
| 106 | + for (int i = 1; i <= DEPTH; i++) { |
| 107 | + curr.effect[i - 1] += rooms[curr.leftChild].effect[i]; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if(curr.rightChild != -1) { |
| 112 | + for (int i = 1; i <= DEPTH; i++) { |
| 113 | + curr.effect[i - 1] += rooms[curr.rightChild].effect[i]; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static void init(StringTokenizer st) { |
| 119 | + rooms = new Room[N + 1]; |
| 120 | + for (int i = 0; i <= N; i++) { |
| 121 | + rooms[i] = new Room(i); |
| 122 | + } |
| 123 | + |
| 124 | + for (int i = 1; i <= N; i++) { |
| 125 | + int pIdx = Integer.parseInt(st.nextToken()); |
| 126 | + rooms[i].parent = pIdx; |
| 127 | + if(rooms[pIdx].leftChild == -1) rooms[pIdx].leftChild = i; |
| 128 | + else rooms[pIdx].rightChild = i; |
| 129 | + } |
| 130 | + |
| 131 | + for (int i = 1; i <= N; i++) { |
| 132 | + rooms[i].authority = Integer.parseInt(st.nextToken()); |
| 133 | + if(rooms[i].authority > DEPTH) rooms[i].authority = DEPTH; |
| 134 | + } |
| 135 | + |
| 136 | + initEffect(0); |
| 137 | + } |
| 138 | + |
| 139 | + public static void main(String[] args) throws IOException { |
| 140 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 141 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 142 | + N = Integer.parseInt(st.nextToken()); |
| 143 | + Q = Integer.parseInt(st.nextToken()); |
| 144 | + |
| 145 | + StringBuilder sb = new StringBuilder(); |
| 146 | + int c; |
| 147 | + while (Q-- > 0) { |
| 148 | + st = new StringTokenizer(br.readLine()); |
| 149 | + int command = Integer.parseInt(st.nextToken()); |
| 150 | + switch (command) { |
| 151 | + case 100: // 사내 메신저 준비 |
| 152 | + init(st); |
| 153 | + break; |
| 154 | + case 200: // 알림망 설정 (ON/OFF) |
| 155 | + c = Integer.parseInt(st.nextToken()); |
| 156 | + toggleNotification(c); |
| 157 | + break; |
| 158 | + case 300: // 권한 세기 변경 |
| 159 | + c = Integer.parseInt(st.nextToken()); |
| 160 | + int power = Integer.parseInt(st.nextToken()); |
| 161 | + changeAuthority(c, power); |
| 162 | + break; |
| 163 | + case 400: // 부모 채팅방 교환 |
| 164 | + int c1 = Integer.parseInt(st.nextToken()); |
| 165 | + int c2 = Integer.parseInt(st.nextToken()); |
| 166 | + swapParent(c1, c2); |
| 167 | + break; |
| 168 | + case 500: // 알림을 받을 수 있는 채팅방 수 조회 |
| 169 | + c = Integer.parseInt(st.nextToken()); |
| 170 | + sb.append(rooms[c].effect[0] - 1).append("\n"); |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + System.out.println(sb); |
| 175 | + } |
| 176 | +} |
0 commit comments