From 6f38223d80b8423dc18bed4cf1194101acd22b93 Mon Sep 17 00:00:00 2001 From: HeeEul Shin <83682424+ShinHeeEul@users.noreply.github.com> Date: Tue, 18 Mar 2025 11:17:31 +0900 Subject: [PATCH] =?UTF-8?q?[20250318]=20BOJ=20/=20G1=20/=20Beads=20/=20?= =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=9D=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ShinHeeEul/202503/18 BOJ G1 Beads.md | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ShinHeeEul/202503/18 BOJ G1 Beads.md diff --git a/ShinHeeEul/202503/18 BOJ G1 Beads.md b/ShinHeeEul/202503/18 BOJ G1 Beads.md new file mode 100644 index 00000000..9a788a13 --- /dev/null +++ b/ShinHeeEul/202503/18 BOJ G1 Beads.md @@ -0,0 +1,78 @@ +```java + +import java.io.*; +import java.util.*; + +public class Main { + + + static int[] segments; + static int size; + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + private static int read() throws Exception { + return Integer.parseInt(br.readLine().trim()); + } + + public static void main(String[] args) throws Exception { + int T = read(); // 테스트 케이스 개수 + + StringBuilder sb = new StringBuilder(); + + for (int t = 0; t < T; t++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int B = Integer.parseInt(st.nextToken()); + int P = Integer.parseInt(st.nextToken()); + int Q = Integer.parseInt(st.nextToken()); + + size = 1; + while (size < B) { + size <<= 1; + } + + segments = new int[(size << 1) + 1]; + + for (int q = 0; q < P + Q; q++) { + st = new StringTokenizer(br.readLine()); + char c = st.nextToken().charAt(0); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + if (c == 'P') { + update(a, b); + } else { + sb.append(query(a, b, 2, 1, size)).append("\n"); + } + } + } + + System.out.print(sb); // 출력 최적화 + } + + public static void update(int idx, int val) { + + idx += size; + + while(idx > 2) { + segments[idx] += val; + idx = (idx + 1) >> 1; + } + + } + + public static int query(int left, int right, int node, int start, int end) { + if(left > end || right < start) return 0; + + if(left <= start && end <= right) { + return segments[node]; + } + + int mid = (start + end) >> 1; + + return query(left, right, (node << 1) - 1, start, mid) + query(left, right, node << 1, mid + 1, end); + } + + +} + +```