[Link] https://www.acmicpc.net/problem/1275
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
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
static long[] seg;
static int n;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
sb = new StringBuilder();
int[] rl = getArr();
int[] arr;
n = rl[0];
int q = rl[1];
seg = new long[2*n];
arr = getArr();
init(arr);
for(int i = 0; i < q; i++) {
rl = getArr();
int x = rl[0], y = rl[1], a = rl[2] - 1, b = rl[3];
if(x > y) {
int tmp = x;
x = y;
y = tmp;
}
sb.append(get(x - 1, y)).append("\n");
update(a, b);
}
print(sb);
}
static void init(int[] arr) {
for(int i=0; i<n; i++) seg[i+n]=arr[i];
for(int i=n-1; i>0; i--) seg[i]=seg[i<<1]+seg[i<<1|1];
}
static long get(int l, int r) {
long sum = 0l;
for(l+=n, r+=n; l<r; l>>=1, r>>=1) {
if((l & 1)!=0) sum+=seg[l++];
if((r & 1)!=0) sum+=seg[--r];
}
return sum;
}
static void update(int pos, int v) {
pos+=n;
seg[pos]=v;
for(; pos>1; pos>>=1) {
seg[pos>>1]=seg[pos]+seg[pos^1];
}
}
static int toi(String s) { return Integer.parseInt(s); }
static long tol(String s) { return Long.parseLong(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}