반응형
https://www.acmicpc.net/problem/2042
세그먼트 트리의 초기화 변경 부분합을 구현하는 기초적인 문제.
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
87
88
89
|
package D0227;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B2042 {
static int N, M, K;
static int arr[];
static long node[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new int[N + 1];
node = new long[N * 4];
for (int n = 1; n <= N; n++) {
arr[n] = Integer.parseInt(br.readLine());
}
Init(1, N, 1);
for (int m = 0; m < M + K; m++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (a == 1) {
int dif = c - arr[b];
arr[b] = c;
change(1,N,b,dif,1);
} else {
System.out.println(sum(1,N,b,c,1));
}
}
}
public static long Init(int start, int end, int n) {
if (start == end) {
return node[n] = arr[start];
}
int mid = (start + end) / 2;
return node[n] = Init(start, mid, n * 2) + Init(mid + 1, end, n * 2 + 1);
}
public static long sum(int start, int end, int i, int j, int n) {
if (j < start || end < i)
return 0;
if (i <= start && j >= end)
return node[n];
int mid = (start + end) / 2;
return sum(start, mid, i, j, n * 2) + sum(mid + 1, end, i, j, n * 2 + 1);
}
public static void change(int start, int end, int b, int dif, int n) {
// b번째 수를 c로 바꾼다
if (start > b || end < b)
return;
if(b==start&&b==end) {
node[n]+=dif;
return;
}
if (b >= start && b <= end) {
int mid = (start + end) / 2;
change(start, mid, b, dif, n * 2);
change(mid + 1, end, b, dif, n * 2 + 1);
node[n] += dif;
return;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'ProgramSoliving' 카테고리의 다른 글
백준 : 1395 (JAVA) (0) | 2020.02.28 |
---|---|
백준 : 109999 (JAVA) (0) | 2020.02.27 |
백준 : 1517 (0) | 2020.02.26 |
백준 : 2617 (0) | 2020.02.26 |
백준 : 2251 (0) | 2020.02.25 |