/
BOJ_2042_0base_fenwick.java
56 lines (49 loc) · 1.7 KB
/
BOJ_2042_0base_fenwick.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private long[] bit, arr;
private int n;
private long query(int b, int c) {
return getPrefixSum(c) - getPrefixSum(b-1);
}
private long getPrefixSum(int idx) {
long answer = 0l;
while (idx >= 0) {
answer += bit[idx];
idx = (idx&(idx+1))-1;
}
return answer;
}
private void update(int idx, long val) {
long diff = val - arr[idx];
arr[idx] = val;
while (idx < n) {
bit[idx] += diff;
idx |= idx+1;
}
}
private void solution() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
int mk = Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken());
arr = new long[n];
bit = new long[n];
for (int i = 0; i < n; i++) {
update(i, Long.parseLong(br.readLine()));
}
StringBuilder sb = new StringBuilder();
while (mk-->0) {
st = new StringTokenizer(br.readLine());
switch (Integer.parseInt(st.nextToken())) {
case 1: update(Integer.parseInt(st.nextToken())-1, Long.parseLong(st.nextToken())); break;
case 2: sb.append(query(Integer.parseInt(st.nextToken())-1, Integer.parseInt(st.nextToken())-1)).append('\n'); break;
}
}
System.out.print(sb);
}
public static void main(String[] args) throws Exception {
new Main().solution();
}
}