-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicRangeSumQueries.java
More file actions
86 lines (71 loc) · 1.98 KB
/
Copy pathDynamicRangeSumQueries.java
File metadata and controls
86 lines (71 loc) · 1.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
import java.io.*;
public class dynamicrangesumqueries {
//Kattio adapted from USACO Guide
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
public static int [] arr;
public static long [] bit;
public static int m;
public static int n;
public static void main(String[] args) throws IOException{
Kattio s = new Kattio();
n = s.nextInt();
m = s.nextInt();
arr = new int[n + 1];
bit = new long[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = s.nextInt();
add(i, arr[i]);
}
for (int i = 0; i < m; i++) {
int type = s.nextInt();
if (type == 1) {
int k = s.nextInt();
int u = s.nextInt();
add(k, -arr[k]);
arr[k] = u;
add(k, u);
}
else {
int a = s.nextInt();
int b = s.nextInt();
System.out.println(query(b) - query(a - 1));
}
}
}
public static void add(int j, long k) {
for (; j <= n; j += j & -j) {
bit[j] += k;
}
}
public static long query(int j) {
long res = 0;
for (; j > 0; j -= j & -j) {
res += bit[j];
}
return res;
}
}