|
| 1 | +```java |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + |
| 9 | + static int[] segments; |
| 10 | + static int size; |
| 11 | + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + |
| 13 | + private static int read() throws Exception { |
| 14 | + return Integer.parseInt(br.readLine().trim()); |
| 15 | + } |
| 16 | + |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + int T = read(); // 테스트 케이스 개수 |
| 19 | + |
| 20 | + StringBuilder sb = new StringBuilder(); |
| 21 | + |
| 22 | + for (int t = 0; t < T; t++) { |
| 23 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 24 | + int B = Integer.parseInt(st.nextToken()); |
| 25 | + int P = Integer.parseInt(st.nextToken()); |
| 26 | + int Q = Integer.parseInt(st.nextToken()); |
| 27 | + |
| 28 | + size = 1; |
| 29 | + while (size < B) { |
| 30 | + size <<= 1; |
| 31 | + } |
| 32 | + |
| 33 | + segments = new int[(size << 1) + 1]; |
| 34 | + |
| 35 | + for (int q = 0; q < P + Q; q++) { |
| 36 | + st = new StringTokenizer(br.readLine()); |
| 37 | + char c = st.nextToken().charAt(0); |
| 38 | + int a = Integer.parseInt(st.nextToken()); |
| 39 | + int b = Integer.parseInt(st.nextToken()); |
| 40 | + |
| 41 | + if (c == 'P') { |
| 42 | + update(a, b); |
| 43 | + } else { |
| 44 | + sb.append(query(a, b, 2, 1, size)).append("\n"); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + System.out.print(sb); // 출력 최적화 |
| 50 | + } |
| 51 | + |
| 52 | + public static void update(int idx, int val) { |
| 53 | + |
| 54 | + idx += size; |
| 55 | + |
| 56 | + while(idx > 2) { |
| 57 | + segments[idx] += val; |
| 58 | + idx = (idx + 1) >> 1; |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public static int query(int left, int right, int node, int start, int end) { |
| 64 | + if(left > end || right < start) return 0; |
| 65 | + |
| 66 | + if(left <= start && end <= right) { |
| 67 | + return segments[node]; |
| 68 | + } |
| 69 | + |
| 70 | + int mid = (start + end) >> 1; |
| 71 | + |
| 72 | + return query(left, right, (node << 1) - 1, start, mid) + query(left, right, node << 1, mid + 1, end); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +``` |
0 commit comments