|
| 1 | +```java |
| 2 | +class Main { |
| 3 | + |
| 4 | + |
| 5 | + static int size; |
| 6 | + static int N; |
| 7 | + static long[] segments; |
| 8 | + public static void main(String[] args) throws Exception { |
| 9 | + |
| 10 | + N = read(); |
| 11 | + |
| 12 | + size = 1; |
| 13 | + |
| 14 | + while(size < N) size <<= 1; |
| 15 | + |
| 16 | + segments = new long[(size << 1) + 1]; |
| 17 | + |
| 18 | + for(int i = size + 1; i < size + N + 1; i++) segments[i] = read(); |
| 19 | + |
| 20 | + int segmentSize = segments.length - 1; |
| 21 | + |
| 22 | + while(segmentSize >= 2) { |
| 23 | + segments[segmentSize >> 1] = segments[segmentSize] + segments[segmentSize - 1]; |
| 24 | + segmentSize -= 2; |
| 25 | + } |
| 26 | + |
| 27 | + int Q = read(); |
| 28 | + StringBuilder sb = new StringBuilder(); |
| 29 | + |
| 30 | + while(Q --> 0) { |
| 31 | + char c = (char) System.in.read(); |
| 32 | + System.in.read(); |
| 33 | + int a = read(); |
| 34 | + int b = read(); |
| 35 | + |
| 36 | + if(c == 'R') sb.append(query(a, b, 2, 1, size)).append("\n"); |
| 37 | + else update(a, b); |
| 38 | + } |
| 39 | + System.out.println(sb); |
| 40 | + } |
| 41 | + |
| 42 | + public static void update(int idx, int val) { |
| 43 | + |
| 44 | + idx += size; |
| 45 | + |
| 46 | + while(idx >= 2) { |
| 47 | + segments[idx] += val; |
| 48 | + idx = (idx + 1) >> 1; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static long query(int left, int right, int node, int start, int end) { |
| 53 | + |
| 54 | + if(end < left || right < start) return 0L; |
| 55 | + |
| 56 | + if(left <= start && end <= right) return segments[node]; |
| 57 | + |
| 58 | + int mid = (start + end) >> 1; |
| 59 | + |
| 60 | + return query(left, right, (node << 1) - 1, start, mid) |
| 61 | + + query(left, right, (node << 1), mid + 1, end); |
| 62 | + } |
| 63 | + |
| 64 | + private static int read() throws Exception { |
| 65 | + int d, o; |
| 66 | + d = System.in.read(); |
| 67 | + |
| 68 | + o = d & 15; |
| 69 | + while (true) { |
| 70 | + d = System.in.read(); |
| 71 | + if (d == -1 || Character.isWhitespace(d)) break; |
| 72 | + o = (o << 3) + (o << 1) + (d & 15); |
| 73 | + } |
| 74 | + |
| 75 | + return o; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +} |
| 80 | +``` |
0 commit comments