|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_12837_가계부_Hard { |
| 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; |
| 13 | + private static int Q; |
| 14 | + |
| 15 | + private static long[] tree; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + init(); |
| 19 | + sol(); |
| 20 | + } |
| 21 | + |
| 22 | + private static void init() throws IOException { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + |
| 25 | + N = Integer.parseInt(st.nextToken()); |
| 26 | + Q = Integer.parseInt(st.nextToken()); |
| 27 | + |
| 28 | + tree = new long[N * 4]; |
| 29 | + } |
| 30 | + |
| 31 | + private static void sol() throws IOException { |
| 32 | + while (Q-- > 0) { |
| 33 | + st = new StringTokenizer(br.readLine()); |
| 34 | + |
| 35 | + int cmd = Integer.parseInt(st.nextToken()); |
| 36 | + int a = Integer.parseInt(st.nextToken()); |
| 37 | + int b = Integer.parseInt(st.nextToken()); |
| 38 | + |
| 39 | + if (cmd == 1) { |
| 40 | + update(1, 1, N, a, b); |
| 41 | + } else { |
| 42 | + bw.write(partialSum(1, 1, N, a, b) + "\n"); |
| 43 | + } |
| 44 | + } |
| 45 | + bw.flush(); |
| 46 | + bw.close(); |
| 47 | + br.close(); |
| 48 | + } |
| 49 | + |
| 50 | + private static void update(int node, int start, int end, int idx, long diff) { |
| 51 | + // OOB |
| 52 | + if (idx < start || end < idx) { |
| 53 | + return; |
| 54 | + } |
| 55 | + // update |
| 56 | + tree[node] += diff; |
| 57 | + |
| 58 | + // leaf node |
| 59 | + if (start == end) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + int mid = (start + end) / 2; |
| 64 | + update(node * 2, start, mid, idx, diff); |
| 65 | + update(node * 2 + 1, mid + 1, end, idx, diff); |
| 66 | + } |
| 67 | + |
| 68 | + private static long partialSum(int node, int start, int end, int left, int right) { |
| 69 | + // OOB |
| 70 | + if (end < left || right < start) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + // in range |
| 75 | + if (left <= start && end <= right) { |
| 76 | + return tree[node]; |
| 77 | + } |
| 78 | + |
| 79 | + int mid = (start + end) / 2; |
| 80 | + |
| 81 | + return partialSum(node * 2, start, mid, left, right) |
| 82 | + + partialSum(node * 2 + 1, mid + 1, end, left, right); |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | +``` |
0 commit comments