|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int N; |
| 7 | + static int Q; |
| 8 | + static long[] tree; |
| 9 | + public static void main(String[] args) throws Exception { |
| 10 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 13 | + N = Integer.parseInt(st.nextToken()); |
| 14 | + Q = Integer.parseInt(st.nextToken()); |
| 15 | + tree = new long[N*4]; |
| 16 | + for(int i=0;i<Q;i++){ |
| 17 | + st = new StringTokenizer(br.readLine()); |
| 18 | + int option = Integer.parseInt(st.nextToken()); |
| 19 | + int arg1 = Integer.parseInt(st.nextToken()); |
| 20 | + long arg2 = Integer.parseInt(st.nextToken()); |
| 21 | + if(option == 1){ //수정 |
| 22 | + update(1,1,N,arg1,arg2); |
| 23 | + }else if(option == 2){//구간 출력 |
| 24 | + bw.write(query(1,1,N,arg1,(int)arg2)+"\n"); |
| 25 | + } |
| 26 | + } |
| 27 | + bw.close(); |
| 28 | + } |
| 29 | + public static void update(int cur, int start, int end, int idx, long diff){ |
| 30 | + if(idx<start || idx>end) return; |
| 31 | + tree[cur] += diff; |
| 32 | + if(start != end){ |
| 33 | + update(cur*2,start,(start+end)/2,idx,diff); |
| 34 | + update(cur*2 + 1,(start+end)/2 + 1,end,idx,diff); |
| 35 | + } |
| 36 | + } |
| 37 | + public static long query(int cur, int start, int end, int left, int right){ |
| 38 | + if(left>end || right<start) return 0; |
| 39 | + if(left<=start && right>=end) return tree[cur]; |
| 40 | + |
| 41 | + long l = query(cur*2,start,(start+end)/2,left,right); |
| 42 | + long r = query(cur*2+1,(start+end)/2 + 1, end,left,right); |
| 43 | + return l+r; |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
0 commit comments