펜윅 트리 개요
펜윅트리 갱신
< 기본 펜윅트리 갱신 >
1
2
3
4
5
6
7
|
void update(int pos, int val) {
while(pos<=n) {
BIT[pos] += val;
pos += (pos & -pos);
}
}
|
cs |
< 2D 펜윅트리 갱신 >
1
2
3
4
5
6
7
8
9
10
11
12
|
void update(int x, int y, int val) {
int pos;
// 2D Fenwick Tree
while(x<=n) {
pos = y;
while(pos<=n) {
BIT[x][pos] += val;
pos += (pos & -pos);
}
x += (x & -x);
}
}
|
cs |
펜윅트리 탐색
< 기본 펜윅트리 탐색 >
1
2
3
4
5
6
7
|
int search(int pos, int y) {
while(pos>0) {
ret += BIT[pos];
pos -= (pos & -pos);
}
}
|
cs |
< 2D 펜윅트리 탐색 >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int search(int x, int y) {
int pos, ret;
// 2D Fenwick Tree
ret = 0;
while(x>0) {
pos = y;
while(pos>0) {
ret += BIT[x][pos];
pos -= (pos & -pos);
}
x -= (x & -x);
}
return ret;
}
|
cs |
>> (x1, y1) ~ (x2, y2) 구간의 누적합 = search(x2, y2) - ( search(x2, y1-1) + search(x1-1, y2) - search(x1-1, y1-2) )
참고 자료
- 펜윅트리의 기본 : www.acmicpc.net/blog/view/21
펜윅 트리 (바이너리 인덱스 트리)
블로그: 세그먼트 트리 (Segment Tree) 에서 풀어본 문제를 Fenwick Tree를 이용해서 풀어보겠습니다. Fenwick Tree는 Binary Indexed Tree라고도 하며, 줄여서 BIT라고 합니다. Fenwick Tree를 구현하려면, 어떤 수 X
www.acmicpc.net
- 2D 펜윅트리 구현 : www.quora.com/How-do-I-implement-a-2D-binary-indexed-tree-for-range-update-and-range-query-operations
How do I implement a 2D binary indexed tree for range update and range query operations?
Answer: I would be glad to answer this Question as i spent much of my time trying to find 2D Binary indexed tree (Fenwick) for range update and range Query operation, so that it can help others. There does not exist any such standard algorithm or it is ver
www.quora.com
'Study' 카테고리의 다른 글
모듈로 곱셈 역원_팩토리얼 계산( BOJ 13977 ) (0) | 2022.07.11 |
---|---|
정렬 시간 비교 (0) | 2022.04.09 |
소수 판별법(밀러-라빈 소수판별법) (0) | 2022.02.09 |
순열 알고리즘 (0) | 2021.10.01 |
Manacher's Algorithm (0) | 2021.05.27 |