際際滷

際際滷Share a Scribd company logo
Segment Tree
(瑚係襾狩 碁Μ)
1. Top-Down
2022.08.02 蟾覺
Segment Tree (瑚係襾狩 碁Μ)
Segment Tree 覦一伎 蟲螳 一一 觜襯願 螻壱  .
螳覲旧°: (log2 )
(蟲螳 , 蟲螳 螻, 蟲螳 豕螳, 蟲螳 豕螳)
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Segment Tree 
( 蠏碁殊 語   襷)
Segment Tree れ 蟲
( 碁Μ覈 襯 覦一企 蟲 蟆企)
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一
Segment Tree 
 覦一伎 蠍郁 N朱 Segment Tree 蠍磯 2N-1企.
Segment Tree 覦一伎 蠍
 覦一 蠍: 2

 覦一 蠍: 3
 覦一 蠍: 3

 覦一 蠍: 5
 覦一 蠍: 4

 覦一 蠍: 7
 覦一 蠍: 5

 覦一 蠍: 9
 覦一 蠍: 6

 覦一 蠍: 11
 覦一 蠍: 7

 覦一 蠍: 13
 覦一 蠍: 8

 覦一 蠍: 15
 覦一 蠍: 9

 覦一 蠍: 17
45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一 Segment Tree
 覦一伎 蠍郁 N朱 N-1覿 襴碁襯 豈  .
( 覦一伎 9螳企襦 Index 8覿 襴碁襦 豈  .)
Segment Tree 襴碁 豈郁鍵
Segment Tree 
Segment Tree 襴碁  襷蟆 豈郁鍵
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
襴碁( 覦一) 蟾(depth)螳 るゼ  .
るジ 蟆曙一 螳  襯 襾殊 豈譴.
2 log2 21
 1 pow(2, floor(log2(2*N-1))) - 1
 豺覿 Segment Tree 蟾讌 豈郁,
(C/C++ code)
N-1 覿 襾語襯 豈磯 .
Segment Tree 襾語 蟲螳 豈郁鍵
轟 Index襯 蠍一朱,
Left Child Index螻 Right Child Index  朱 蟲  .
 = 2  + 1  1
 = 2  + 1
N-2 覿 0 蟾讌 企 Index Left Node Right Node襯 伎 ロ.
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一
Segment Tree 
Python/C++ Code
from typing import List
import math
def make_stree(arr: List) -> List:
stree_size = len(arr) * 2 - 1
stree = [0] * stree_size
j = 2 ** math.floor(math.log2(stree_size)) - 1
i = 0
while i != len(arr):
stree[j] = arr[i]
j += 1
i += 1
if j == len(stree):
j = len(arr) - 1
for k in reversed(range(0, len(arr) - 1)):
left = (k + 1) * 2 - 1
right = (k + 1) * 2
stree[k] = stree[left] + stree[right]
return stree
arr = [2, 7, 5, 1, 4, 8, 3, 6, 9]
stree = make_stree(arr)
print(stree)
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
using lld = long long;
vector<lld> make_stree(const vector<lld> &arr) {
vector<lld> stree(arr.size() * 2 - 1, 0);
int j = pow(2, floor(log2(stree.size()))) - 1;
int i = 0;
while (i != arr.size()) {
stree[j++] = arr[i++];
if (j == stree.size()) {
j = arr.size() - 1;
}
}
for (int k = arr.size() - 2; k >= 0; k--) {
int left = (k + 1) * 2 - 1;
int right = (k + 1) * 2;
stree[k] = stree[left] + stree[right];
}
return stree;
}
int main() {
vector<lld> A = {2, 7, 5, 1, 4, 8, 3, 6, 9};
vector<lld> stree = make_stree(A);
for (auto &e : stree) {
cout << e << ", ";
}
cout << endl;
return 0;
}
Python
C++
Segment Tree 一
Non recursive
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
3覯讌 覿 7覯讌 蟾讌 蟲螳  蟲螻 矩る  蠏碁殊 觜螳 碁  蟲覃 .
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一
Segment Tree 一
伎碁Μ 襭碁碁襯 蠍一朱 殊, るジ讓  螳 覦 .
Segment Tree るジ讓  螳
殊  螳:1
るジ讓  螳:1
殊  螳:2
るジ讓  螳:1
殊  螳:2
るジ讓  螳:2
殊  螳:3
るジ讓  螳:2
殊  螳:4
るジ讓  螳:2
殊  螳:4
るジ讓  螳:3
殊  螳:4
るジ讓  螳:4
殊  螳:5
るジ讓  螳:4
一危
螳
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
殊
螳
1 2 2 3 4 4 4 5 6 7 8 8 8 8 8 9
るジ讓
螳
1 1 2 2 2 3 4 4 4 4 4 5 6 7 8 8
  = 鐃署 

2
,   
 + 2
2
  , 
, ゐゐゐゐゐゐゐゐ:  = 2 log2 
,      
  = min   ,    
 朱 るジ讓  螳襯 蟲  .
Segment Tree 一
Non recursive
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
[0,8]
螻壱螻  (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
[0,8] [2,6] 讌 .
[0,4], [5,8] 蟲螳朱 伎 螻壱.
0 8
6
2
襭 碁 覯 [0,N-1] .
殊  碁 覯 [覿覈 碁  蟲螳,覿覈 碁 蟲螳 譴螳]
るジ讓  碁 覯[覿覈 碁 蟲螳 譴螳+1,覿覈 碁  蟲螳]
Segment Tree 一
Non recursive
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
[0,8]
[0,4] [2,6] 讌 .
[0,2], [3,4] 蟲螳朱 伎 螻壱. [0,4] [5,8]
[5,8] [2,6] 讌 .
[5,6], [7,8] 蟲螳朱 伎 螻壱.
蠏碁磯 [7,8] [2,6]螻 蟆轟 蟲螳 .
磯殊, [7,8] 螻壱讌 .
0 4
6
2
5 8
螻壱螻  (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
Segment Tree 一
Non recursive
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
[0,8]
[3,4] [2,6] ! [0,4] [5,8] [5,6] [2,6] !
3 4
6
2
5
[3,4]
[0,2]
[0,2] [2,6] 讌 .
[0,1], [2] 蟲螳朱 伎 螻壱.
蠏碁磯 [0,1] [2,6]螻 蟆轟讌 朱襦,
螻壱讌 .
0 2 6
螻壱螻  (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
Segment Tree 一
Non recursive
9 5 1 4 8 3 6 9
14 5 11 15
19 26
45
2 7
[0,8]
[2] [2,6] ! [0,4] [5,8]
6
2
[3,4]
[0,2]
2
螻壱螻  (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
[2]
Segment Tree 一
Python/C++ Code
def calc_stree(stree: List, L: int, R: int):
length = (len(stree) + 1) // 2
ret = 0
q = [(0, 0, length - 1)]
while len(q) != 0:
idx, left, right = q[0]
q = q[1:]
if L <= left and right <= R:
ret += stree[idx]
else:
left_idx = (idx + 1) * 2 - 1
right_idx = (idx + 1) * 2
n = right - left + 1
k = 2 ** math.floor(math.log2(n))
other = n - k // 2 if n <= (k + k * 2) // 2 else n - k
right_cnt = min(other, n - other)
mid = right - right_cnt
if L <= mid:
q.append((left_idx, left, mid))
if mid + 1 <= R:
q.append((right_idx, mid + 1, right))
return ret
lld calc_stree(const vector<lld>& stree, const int L, const int R) {
const lld length = (stree.size() + 1) / 2;
lld ret = 0;
queue<tuple<lld, lld, lld>> q;
q.push(make_tuple(0, 0, length - 1));
while (!q.empty()) {
lld idx, left, right;
std::tie(idx, left, right) = q.front();
q.pop();
if (L <= left && right <= R) {
ret += stree[idx];
}
else {
int left_idx = (idx + 1) * 2 - 1;
int right_idx = (idx + 1) * 2;
int n = right - left+1;
int k = (int)pow(2, floor(log2(n)));
int other = (n <= (k + k * 2) / 2) ? n - k / 2 : n - k;
int right_cnt = min(other, n - other);
int mid = right - right_cnt;
if (L <= mid) {
q.push(make_tuple(left_idx, left, mid));
}
if (mid + 1 <= R) {
q.push(make_tuple(right_idx, mid + 1, right));
}
}
}
return ret;
}
Python
C++
Segment Tree 一危
る 襴碁 Index 蟲蠍
 覦一伎 0覯讌 Index襯 Segment Tree 谿場 ,
る 螻褐 Index 襷 伎.
 = 鐃
2 log2 21
 1 + ,   < 2  1
2 log2 21
 1 +   , 
企 Leaf Node 覿 Root Node 蟾讌 蟲螳 螳煙蠍
Parent Node Index  朱 蟲  .
_ =
  1
2
Leaf Node Parent Node 覿 Root Node 蟾讌 Left Node Right Node  螳煙覃 .
襴碁 蟾願 るゼ襯 螻ろ  朱 螻壱  .
9 5 3 4 8 3 6 9
14 7 11 15
21 26
47
2 7
45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
47 21 26 14 7 11 15 9 5 3 4 8 3 6 9 2 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
2 7 5 3 4 8 3 6 9
0 1 2 3 4 5 6 7 8
蠍一ヾ 覦一伎 4覯讌 螳1 3朱 覲蟆渚 
(蠍一ヾ 覦一伎 覲蟆渚蟆 , Segment Tree 覲蟆渚.)
Segment Tree 一危
Python/C++ Code
void update_stree(vector<lld>& stree, lld idx, lld data) {
const lld length = (stree.size() + 1) / 2;
lld sidx = pow(2, floor(log2(stree.size()))) - 1 + idx;
if (sidx >= stree.size()) {
sidx -= length;
}
stree[sidx] = data;
while (sidx != 0) {
sidx = (sidx - 1) / 2;
lld left = (sidx + 1) * 2 - 1;
lld right = (sidx + 1) * 2;
stree[sidx] = stree[left] + stree[right];
}
}
C++
def update_stree(stree: List, idx: int, data):
length = (len(stree) + 1) // 2
sidx = 2 ** math.floor(math.log2(len(stree)))  1 + idx
if sidx >= len(stree):
sidx -= length
stree[sidx] = data
while sidx != 0:
sidx = (sidx  1) // 2
left = (sidx + 1) * 2  1
right = (sidx + 1) * 2
stree[sidx] = stree[left] + stree[right]
Python
Segment Tree
(瑚係襾狩 碁Μ)
2. Bottom-Up
Segment Tree 
 覦一伎 蠍郁 N朱 Segment Tree 蠍磯 2N-1企.
Segment Tree 覦一伎 蠍
 覦一 蠍: 2

 覦一 蠍: 3
 覦一 蠍: 3

 覦一 蠍: 5
 覦一 蠍: 4

 覦一 蠍: 7
 覦一 蠍: 5

 覦一 蠍: 9
 覦一 蠍: 6

 覦一 蠍: 11
 覦一 蠍: 7

 覦一 蠍: 13
 覦一 蠍: 8

 覦一 蠍: 15
 覦一 蠍: 9

 覦一 蠍: 17
0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一 Segment Tree
 覦一伎 蠍郁 N朱 N覿 襴碁襯 豈  .
(0覯讌 Index 讌 , 磯殊 Segment Tree 蠍磯ゼ 2N朱 豐蠍壱)
( 覦一伎 9螳企襦 Index 9覿 襴碁襦 豈  .)
Segment Tree 襴碁 豈郁鍵
Segment Tree 
Segment Tree 襴碁  襷蟆 豈郁鍵
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
襴碁( 覦一) 蟾(depth)螳 るジ 蟆螻 蟯 
N 覿 谿襦襦 豈譴.
Segment Tree 襾語 蟲螳 豈郁鍵
轟 Index襯 蠍一朱,
Left Child Index螻 Right Child Index  朱 蟲  .
 = 2
 = 2 + 1
N-1覿 1 蟾讌 企 Index Left Node Right Node襯 伎 ロ.
0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一
Segment Tree 
Python/C++ Code
from typing import List
def make_stree(arr: List) -> List:
stree_size = len(arr) * 2
stree = [0] * stree_size
j = len(arr)
i = 0
while i != len(arr):
stree[j] = arr[i]
j += 1
i += 1
for k in reversed(range(1, len(arr))):
stree[k] = stree[2 * k] + stree[2 * k + 1]
return stree
arr = [2, 7, 5, 1, 4, 8, 3, 6, 9]
stree = make_stree(arr)
print(stree)
#include<iostream>
#include<vector>
using namespace std;
using lld = long long;
vector<lld> make_stree(const vector<lld> &arr) {
vector<lld> stree(arr.size() * 2, 0);
int j = arr.size();
int i = 0;
while (i != arr.size()) {
stree[j++] = arr[i++];
}
for (int k = arr.size() - 1; k > 0; k--) {
stree[k] = stree[2 * k] + stree[2 * k + 1];
}
return stree;
}
int main() {
vector<lld> A = {2, 7, 5, 1, 4, 8, 3, 6, 9};
vector<lld> stree = make_stree(A);
for (auto &e : stree) {
cout << e << ", ";
}
cout << endl;
return 0;
}
Python
C++
Segment Tree 一
Non recursive
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
3覯讌 覿 7覯讌 蟾讌 蟲螳  蟲螻 矩る  蠏碁殊 觜螳 碁  蟲覃 .
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
 覦一
Segment Tree 一
Non recursive
蟲螳 殊  : (1)るジ讓 企 螻 Index襯 るジ讓曙朱  豺 企, (2)殊 企 覿覈 碁襦 手.
蟲螳 るジ讓  : (1)殊 企 螻 Index襯 殊曙朱  豺 企, (2)るジ讓 企 覿覈 碁襦 手.
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
蟲螳 殊  るジ讓  磯 るジ 伎
蟲伎  螳 蟲螳 殊  るジ讓   螳れ 願鍵 覓語
殊   るジ讓 企 覿覈 碁襦 手  .
(覿覈 碁 殊 螻 るジ讓  )
磯殊 企 螳 螻, Index襯 るジ讓曙朱  豺 企 
覿覈 碁襦 手.
るジ讓   殊 企 覿覈 碁襦 手  .
(覿覈 碁 殊 螻 るジ讓  )
磯殊 企 螳 螻, Index襯 殊曙朱  豺 企 
覿覈 碁襦 手.
6
2
殊  るジ讓
Segment Tree 一
Non recursive
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
殊 語 るジ讓 語  覦覯
殊  : Index螳 讌, るジ讓  : Index螳 
覿覈 碁襦 手 
Index = Index / 2
6
2
殊  るジ讓 
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
16 17
蟲螳 殊  : (1)るジ讓 企 螻 Index襯 るジ讓曙朱  豺 企, (2)殊 企 覿覈 碁襦 手.
蟲螳 るジ讓  : (1)殊 企 螻 Index襯 殊曙朱  豺 企, (2)るジ讓 企 覿覈 碁襦 手.
Segment Tree 一
Non recursive
6
2
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
5 るジ讓 願鍵 覓語
覿覈 碁襦 手  .
磯殊 5襯 .
蠏  るジ讓曙朱  豺 企螻
覿覈 碁襦 手.
3 るジ讓 願鍵 覓語
覿覈 碁襦 手  .
磯殊 讌 螻 覿覈 碁襦 手.
蟲螳 殊  : (1)るジ讓 企 螻 Index襯 るジ讓曙朱  豺 企, (2)殊 企 覿覈 碁襦 手.
蟲螳 るジ讓  : (1)殊 企 螻 Index襯 殊曙朱  豺 企, (2)るジ讓 企 覿覈 碁襦 手.
Segment Tree 一
Non recursive
6
2
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
5 殊 願鍵 覓語
覿覈 碁襦 手  .
磯殊 讌 螻 覿覈 碁襦 手.
11 るジ讓 願鍵 覓語
覿覈 碁襦 手  .
磯殊 讌 螻 覿覈 碁襦 手.
蟲螳 殊  : (1)るジ讓 企 螻 Index襯 るジ讓曙朱  豺 企, (2)殊 企 覿覈 碁襦 手.
蟲螳 るジ讓  : (1)殊 企 螻 Index襯 殊曙朱  豺 企, (2)るジ讓 企 覿覈 碁襦 手.
Segment Tree 一
Non recursive
6
2
15 2 7 5 1 4 8 3
17 12 5 11
29 16
45
6 9
16 るジ讓 企.
殊  蠍一朱 るジ讓 企
伎 覩襦 16 .
るジ讓  蠍一朱 るジ讓 企
覿覈 碁襦 手 讌襷
殊  るジ讓 螳 襷蠍 覓語 襷豺.
蟲螳 殊  : (1)るジ讓 企 螻 Index襯 るジ讓曙朱  豺 企, (2)殊 企 覿覈 碁襦 手.
蟲螳 るジ讓  : (1)殊 企 螻 Index襯 殊曙朱  豺 企, (2)るジ讓 企 覿覈 碁襦 手.
Segment Tree 一
Python/C++ Code
def calc_stree(stree: List, L: int, R: int):
length = len(stree) // 2
ret = 0
left = L  1 + length
right = R  1 + length
while left <= right:
if left % 2 == 1:
ret += stree[left]
left += 1
if right % 2 == 0:
ret += stree[right]
right -= 1
left //= 2
right //= 2
return ret
lld calc_stree(const vector<lld>& stree, const int L, const int R) {
const lld length = stree.size() / 2;
lld ret = 0;
lld left = L  1 + length;
lld right = R  1 + length;
for (left, right; left <= right; left /= 2, right /= 2) {
if (left % 2 == 1) {
ret += stree[left];
left++;
}
if (right % 2 == 0) {
ret += stree[right];
right--;
}
}
return ret;
}
Python
C++
Segment Tree 一危
る 襴碁 Index 蟲蠍
 覦一伎 0覯讌 Index襯 Segment Tree 谿場 ,
る 螻褐 Index 襷 伎.
 =  +   1
企 Leaf Node 覿 Root Node 蟾讌 蟲螳 螳煙蠍
Parent Node Index  朱 蟲  .
_ =

2
Leaf Node Parent Node 覿 Root Node 蟾讌 Left Node Right Node  螳煙覃 .
15 2 7 5 3 4 8 3
17 12 7 11
29 18
47
6 9
2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8
2 7 5 3 4 8 3 6 9
0 1 2 3 4 5 6 7 8
蠍一ヾ 覦一伎 4覯讌 螳1 3朱 覲蟆渚 
(蠍一ヾ 覦一伎 覲蟆渚蟆 , Segment Tree 覲蟆渚.)
0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 47 29 18 17 12 7 11 15 2 7 5 3 4 8 3 6 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
sidx螳 讌  - Left Node Index : sidx, Right Node Index : sidx + 1
sidx螳   - Left Node Index : sidx  1, Right Node Index : sidx
Segment Tree 一危
Python/C++ Code
void update_stree(vector<lld>& stree, lld idx, lld data) {
const lld length = stree.size() / 2;
lld sidx = length + idx - 1;
for (stree[sidx] = data; sidx > 1; sidx /= 2) {
if (sidx % 2 == 0) {
stree[sidx / 2] = stree[sidx] + stree[sidx + 1];
}
else {
stree[sidx / 2] = stree[sidx] + stree[sidx  1];
}
}
}
C++
def update_stree(stree: List, idx: int, data):
length = len(stree) // 2
sidx = length + idx - 1
stree[sidx] = data
while sidx > 1:
if sidx % 2 == 0:
stree[sidx // 2] = stree[sidx] + stree[sidx + 1]
else:
stree[sidx // 2] = stree[sidx] + stree[sidx  1]
sidx //= 2
Python

More Related Content

Similar to SegmentTree Datastructure description and implementation slides (20)

瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
Suhyun Park
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
kd19h
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
jinwookhong
Data Structure 2
Data Structure 2Data Structure 2
Data Structure 2
yonsei
R intro
R introR intro
R intro
譯殊
Sicp 2.2 螻豸 蟲譟 一危一 煙
Sicp 2.2 螻豸 蟲譟 一危一  煙Sicp 2.2 螻豸 蟲譟 一危一  煙
Sicp 2.2 螻豸 蟲譟 一危一 煙
aceigy6322
覓語矧(1)
覓語矧(1)覓語矧(1)
覓語矧(1)
襴(蟲襦讌碁讌3覯豢蟲 2覿蟇磯Μ)
螻襴讀
螻襴讀螻襴讀
螻襴讀
Kwang-Hyun Park
R 襦蠏碁 危伎 v1.1
R 襦蠏碁 危伎  v1.1R 襦蠏碁 危伎  v1.1
R 襦蠏碁 危伎 v1.1
happychallenge
Ch10
Ch10Ch10
Ch10
Hankyo
螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟
蠍 蟾
れ 焔
れ 焔 れ 焔
れ 焔
覩殊
3ds maxscript 襴_20151206_讌
3ds maxscript 襴_20151206_讌3ds maxscript 襴_20151206_讌
3ds maxscript 襴_20151206_讌
JinTaek Seo
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
MapReduce ろ (K-mer Counting, K-means Clustering)
MapReduce ろ  (K-mer Counting, K-means Clustering)MapReduce ろ  (K-mer Counting, K-means Clustering)
MapReduce ろ (K-mer Counting, K-means Clustering)
譯殊
Cpp 0x kimRyungee
Cpp 0x kimRyungeeCpp 0x kimRyungee
Cpp 0x kimRyungee
scor7910
Project#5 旧襷 蠍 谿剰鍵 Hwp
Project#5 旧襷 蠍 谿剰鍵 HwpProject#5 旧襷 蠍 谿剰鍵 Hwp
Project#5 旧襷 蠍 谿剰鍵 Hwp
Kimjeongmoo
5旧襷 蠍 谿剰鍵
5旧襷 蠍 谿剰鍵5旧襷 蠍 谿剰鍵
5旧襷 蠍 谿剰鍵
herojoon1378
伎一 C1 襦 4
伎一 C1 襦 4伎一 C1 襦 4
伎一 C1 襦 4
pkok15
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
瑚係襾狩 碁Μ 襴蟆 一危誤蠍 - Sogang ICPC Team, 2020 Winter
Suhyun Park
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
kd19h
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
jinwookhong
Data Structure 2
Data Structure 2Data Structure 2
Data Structure 2
yonsei
R intro
R introR intro
R intro
譯殊
Sicp 2.2 螻豸 蟲譟 一危一 煙
Sicp 2.2 螻豸 蟲譟 一危一  煙Sicp 2.2 螻豸 蟲譟 一危一  煙
Sicp 2.2 螻豸 蟲譟 一危一 煙
aceigy6322
R 襦蠏碁 危伎 v1.1
R 襦蠏碁 危伎  v1.1R 襦蠏碁 危伎  v1.1
R 襦蠏碁 危伎 v1.1
happychallenge
Ch10
Ch10Ch10
Ch10
Hankyo
螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟螻襴讀螻 襭蟲譟
螻襴讀螻 襭蟲譟
蠍 蟾
れ 焔
れ 焔 れ 焔
れ 焔
覩殊
3ds maxscript 襴_20151206_讌
3ds maxscript 襴_20151206_讌3ds maxscript 襴_20151206_讌
3ds maxscript 襴_20151206_讌
JinTaek Seo
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
MapReduce ろ (K-mer Counting, K-means Clustering)
MapReduce ろ  (K-mer Counting, K-means Clustering)MapReduce ろ  (K-mer Counting, K-means Clustering)
MapReduce ろ (K-mer Counting, K-means Clustering)
譯殊
Cpp 0x kimRyungee
Cpp 0x kimRyungeeCpp 0x kimRyungee
Cpp 0x kimRyungee
scor7910
Project#5 旧襷 蠍 谿剰鍵 Hwp
Project#5 旧襷 蠍 谿剰鍵 HwpProject#5 旧襷 蠍 谿剰鍵 Hwp
Project#5 旧襷 蠍 谿剰鍵 Hwp
Kimjeongmoo
5旧襷 蠍 谿剰鍵
5旧襷 蠍 谿剰鍵5旧襷 蠍 谿剰鍵
5旧襷 蠍 谿剰鍵
herojoon1378
伎一 C1 襦 4
伎一 C1 襦 4伎一 C1 襦 4
伎一 C1 襦 4
pkok15

More from Bomm Kim (6)

ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slideML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
Bomm Kim
[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding
Bomm Kim
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
Bomm Kim
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
Bomm Kim
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) AlgorithmBOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
Bomm Kim
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ codeBOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
Bomm Kim
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slideML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
Bomm Kim
[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding
Bomm Kim
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
Bomm Kim
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
Bomm Kim
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) AlgorithmBOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm
Bomm Kim
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ codeBOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
Bomm Kim

SegmentTree Datastructure description and implementation slides

  • 1. Segment Tree (瑚係襾狩 碁Μ) 1. Top-Down 2022.08.02 蟾覺
  • 2. Segment Tree (瑚係襾狩 碁Μ) Segment Tree 覦一伎 蟲螳 一一 觜襯願 螻壱 . 螳覲旧°: (log2 ) (蟲螳 , 蟲螳 螻, 蟲螳 豕螳, 蟲螳 豕螳) 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Segment Tree ( 蠏碁殊 語 襷) Segment Tree れ 蟲 ( 碁Μ覈 襯 覦一企 蟲 蟆企) 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一
  • 3. Segment Tree 覦一伎 蠍郁 N朱 Segment Tree 蠍磯 2N-1企. Segment Tree 覦一伎 蠍 覦一 蠍: 2 覦一 蠍: 3 覦一 蠍: 3 覦一 蠍: 5 覦一 蠍: 4 覦一 蠍: 7 覦一 蠍: 5 覦一 蠍: 9 覦一 蠍: 6 覦一 蠍: 11 覦一 蠍: 7 覦一 蠍: 13 覦一 蠍: 8 覦一 蠍: 15 覦一 蠍: 9 覦一 蠍: 17 45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一 Segment Tree 覦一伎 蠍郁 N朱 N-1覿 襴碁襯 豈 . ( 覦一伎 9螳企襦 Index 8覿 襴碁襦 豈 .) Segment Tree 襴碁 豈郁鍵
  • 4. Segment Tree Segment Tree 襴碁 襷蟆 豈郁鍵 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 襴碁( 覦一) 蟾(depth)螳 るゼ . るジ 蟆曙一 螳 襯 襾殊 豈譴. 2 log2 21 1 pow(2, floor(log2(2*N-1))) - 1 豺覿 Segment Tree 蟾讌 豈郁, (C/C++ code) N-1 覿 襾語襯 豈磯 . Segment Tree 襾語 蟲螳 豈郁鍵 轟 Index襯 蠍一朱, Left Child Index螻 Right Child Index 朱 蟲 . = 2 + 1 1 = 2 + 1 N-2 覿 0 蟾讌 企 Index Left Node Right Node襯 伎 ロ. 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一
  • 5. Segment Tree Python/C++ Code from typing import List import math def make_stree(arr: List) -> List: stree_size = len(arr) * 2 - 1 stree = [0] * stree_size j = 2 ** math.floor(math.log2(stree_size)) - 1 i = 0 while i != len(arr): stree[j] = arr[i] j += 1 i += 1 if j == len(stree): j = len(arr) - 1 for k in reversed(range(0, len(arr) - 1)): left = (k + 1) * 2 - 1 right = (k + 1) * 2 stree[k] = stree[left] + stree[right] return stree arr = [2, 7, 5, 1, 4, 8, 3, 6, 9] stree = make_stree(arr) print(stree) #include<iostream> #include<vector> #include<cmath> using namespace std; using lld = long long; vector<lld> make_stree(const vector<lld> &arr) { vector<lld> stree(arr.size() * 2 - 1, 0); int j = pow(2, floor(log2(stree.size()))) - 1; int i = 0; while (i != arr.size()) { stree[j++] = arr[i++]; if (j == stree.size()) { j = arr.size() - 1; } } for (int k = arr.size() - 2; k >= 0; k--) { int left = (k + 1) * 2 - 1; int right = (k + 1) * 2; stree[k] = stree[left] + stree[right]; } return stree; } int main() { vector<lld> A = {2, 7, 5, 1, 4, 8, 3, 6, 9}; vector<lld> stree = make_stree(A); for (auto &e : stree) { cout << e << ", "; } cout << endl; return 0; } Python C++
  • 6. Segment Tree 一 Non recursive 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 3覯讌 覿 7覯讌 蟾讌 蟲螳 蟲螻 矩る 蠏碁殊 觜螳 碁 蟲覃 . 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一
  • 7. Segment Tree 一 伎碁Μ 襭碁碁襯 蠍一朱 殊, るジ讓 螳 覦 . Segment Tree るジ讓 螳 殊 螳:1 るジ讓 螳:1 殊 螳:2 るジ讓 螳:1 殊 螳:2 るジ讓 螳:2 殊 螳:3 るジ讓 螳:2 殊 螳:4 るジ讓 螳:2 殊 螳:4 るジ讓 螳:3 殊 螳:4 るジ讓 螳:4 殊 螳:5 るジ讓 螳:4 一危 螳 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 殊 螳 1 2 2 3 4 4 4 5 6 7 8 8 8 8 8 9 るジ讓 螳 1 1 2 2 2 3 4 4 4 4 4 5 6 7 8 8 = 鐃署 2 , + 2 2 , , ゐゐゐゐゐゐゐゐ: = 2 log2 , = min , 朱 るジ讓 螳襯 蟲 .
  • 8. Segment Tree 一 Non recursive 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 [0,8] 螻壱螻 (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 . [0,8] [2,6] 讌 . [0,4], [5,8] 蟲螳朱 伎 螻壱. 0 8 6 2 襭 碁 覯 [0,N-1] . 殊 碁 覯 [覿覈 碁 蟲螳,覿覈 碁 蟲螳 譴螳] るジ讓 碁 覯[覿覈 碁 蟲螳 譴螳+1,覿覈 碁 蟲螳]
  • 9. Segment Tree 一 Non recursive 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 [0,8] [0,4] [2,6] 讌 . [0,2], [3,4] 蟲螳朱 伎 螻壱. [0,4] [5,8] [5,8] [2,6] 讌 . [5,6], [7,8] 蟲螳朱 伎 螻壱. 蠏碁磯 [7,8] [2,6]螻 蟆轟 蟲螳 . 磯殊, [7,8] 螻壱讌 . 0 4 6 2 5 8 螻壱螻 (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
  • 10. Segment Tree 一 Non recursive 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 [0,8] [3,4] [2,6] ! [0,4] [5,8] [5,6] [2,6] ! 3 4 6 2 5 [3,4] [0,2] [0,2] [2,6] 讌 . [0,1], [2] 蟲螳朱 伎 螻壱. 蠏碁磯 [0,1] [2,6]螻 蟆轟讌 朱襦, 螻壱讌 . 0 2 6 螻壱螻 (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 .
  • 11. Segment Tree 一 Non recursive 9 5 1 4 8 3 6 9 14 5 11 15 19 26 45 2 7 [0,8] [2] [2,6] ! [0,4] [5,8] 6 2 [3,4] [0,2] 2 螻壱螻 (1)蟲螳 覃 螻, (2)蟆轟覃 覿螻, (3)讌 朱 螻壱讌 . [2]
  • 12. Segment Tree 一 Python/C++ Code def calc_stree(stree: List, L: int, R: int): length = (len(stree) + 1) // 2 ret = 0 q = [(0, 0, length - 1)] while len(q) != 0: idx, left, right = q[0] q = q[1:] if L <= left and right <= R: ret += stree[idx] else: left_idx = (idx + 1) * 2 - 1 right_idx = (idx + 1) * 2 n = right - left + 1 k = 2 ** math.floor(math.log2(n)) other = n - k // 2 if n <= (k + k * 2) // 2 else n - k right_cnt = min(other, n - other) mid = right - right_cnt if L <= mid: q.append((left_idx, left, mid)) if mid + 1 <= R: q.append((right_idx, mid + 1, right)) return ret lld calc_stree(const vector<lld>& stree, const int L, const int R) { const lld length = (stree.size() + 1) / 2; lld ret = 0; queue<tuple<lld, lld, lld>> q; q.push(make_tuple(0, 0, length - 1)); while (!q.empty()) { lld idx, left, right; std::tie(idx, left, right) = q.front(); q.pop(); if (L <= left && right <= R) { ret += stree[idx]; } else { int left_idx = (idx + 1) * 2 - 1; int right_idx = (idx + 1) * 2; int n = right - left+1; int k = (int)pow(2, floor(log2(n))); int other = (n <= (k + k * 2) / 2) ? n - k / 2 : n - k; int right_cnt = min(other, n - other); int mid = right - right_cnt; if (L <= mid) { q.push(make_tuple(left_idx, left, mid)); } if (mid + 1 <= R) { q.push(make_tuple(right_idx, mid + 1, right)); } } } return ret; } Python C++
  • 13. Segment Tree 一危 る 襴碁 Index 蟲蠍 覦一伎 0覯讌 Index襯 Segment Tree 谿場 , る 螻褐 Index 襷 伎. = 鐃 2 log2 21 1 + , < 2 1 2 log2 21 1 + , 企 Leaf Node 覿 Root Node 蟾讌 蟲螳 螳煙蠍 Parent Node Index 朱 蟲 . _ = 1 2 Leaf Node Parent Node 覿 Root Node 蟾讌 Left Node Right Node 螳煙覃 . 襴碁 蟾願 るゼ襯 螻ろ 朱 螻壱 . 9 5 3 4 8 3 6 9 14 7 11 15 21 26 47 2 7 45 19 26 14 5 11 15 9 5 1 4 8 3 6 9 2 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 47 21 26 14 7 11 15 9 5 3 4 8 3 6 9 2 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 2 7 5 3 4 8 3 6 9 0 1 2 3 4 5 6 7 8 蠍一ヾ 覦一伎 4覯讌 螳1 3朱 覲蟆渚 (蠍一ヾ 覦一伎 覲蟆渚蟆 , Segment Tree 覲蟆渚.)
  • 14. Segment Tree 一危 Python/C++ Code void update_stree(vector<lld>& stree, lld idx, lld data) { const lld length = (stree.size() + 1) / 2; lld sidx = pow(2, floor(log2(stree.size()))) - 1 + idx; if (sidx >= stree.size()) { sidx -= length; } stree[sidx] = data; while (sidx != 0) { sidx = (sidx - 1) / 2; lld left = (sidx + 1) * 2 - 1; lld right = (sidx + 1) * 2; stree[sidx] = stree[left] + stree[right]; } } C++ def update_stree(stree: List, idx: int, data): length = (len(stree) + 1) // 2 sidx = 2 ** math.floor(math.log2(len(stree))) 1 + idx if sidx >= len(stree): sidx -= length stree[sidx] = data while sidx != 0: sidx = (sidx 1) // 2 left = (sidx + 1) * 2 1 right = (sidx + 1) * 2 stree[sidx] = stree[left] + stree[right] Python
  • 16. Segment Tree 覦一伎 蠍郁 N朱 Segment Tree 蠍磯 2N-1企. Segment Tree 覦一伎 蠍 覦一 蠍: 2 覦一 蠍: 3 覦一 蠍: 3 覦一 蠍: 5 覦一 蠍: 4 覦一 蠍: 7 覦一 蠍: 5 覦一 蠍: 9 覦一 蠍: 6 覦一 蠍: 11 覦一 蠍: 7 覦一 蠍: 13 覦一 蠍: 8 覦一 蠍: 15 覦一 蠍: 9 覦一 蠍: 17 0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一 Segment Tree 覦一伎 蠍郁 N朱 N覿 襴碁襯 豈 . (0覯讌 Index 讌 , 磯殊 Segment Tree 蠍磯ゼ 2N朱 豐蠍壱) ( 覦一伎 9螳企襦 Index 9覿 襴碁襦 豈 .) Segment Tree 襴碁 豈郁鍵
  • 17. Segment Tree Segment Tree 襴碁 襷蟆 豈郁鍵 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 襴碁( 覦一) 蟾(depth)螳 るジ 蟆螻 蟯 N 覿 谿襦襦 豈譴. Segment Tree 襾語 蟲螳 豈郁鍵 轟 Index襯 蠍一朱, Left Child Index螻 Right Child Index 朱 蟲 . = 2 = 2 + 1 N-1覿 1 蟾讌 企 Index Left Node Right Node襯 伎 ロ. 0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一
  • 18. Segment Tree Python/C++ Code from typing import List def make_stree(arr: List) -> List: stree_size = len(arr) * 2 stree = [0] * stree_size j = len(arr) i = 0 while i != len(arr): stree[j] = arr[i] j += 1 i += 1 for k in reversed(range(1, len(arr))): stree[k] = stree[2 * k] + stree[2 * k + 1] return stree arr = [2, 7, 5, 1, 4, 8, 3, 6, 9] stree = make_stree(arr) print(stree) #include<iostream> #include<vector> using namespace std; using lld = long long; vector<lld> make_stree(const vector<lld> &arr) { vector<lld> stree(arr.size() * 2, 0); int j = arr.size(); int i = 0; while (i != arr.size()) { stree[j++] = arr[i++]; } for (int k = arr.size() - 1; k > 0; k--) { stree[k] = stree[2 * k] + stree[2 * k + 1]; } return stree; } int main() { vector<lld> A = {2, 7, 5, 1, 4, 8, 3, 6, 9}; vector<lld> stree = make_stree(A); for (auto &e : stree) { cout << e << ", "; } cout << endl; return 0; } Python C++
  • 19. Segment Tree 一 Non recursive 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 3覯讌 覿 7覯讌 蟾讌 蟲螳 蟲螻 矩る 蠏碁殊 觜螳 碁 蟲覃 . 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 覦一
  • 20. Segment Tree 一 Non recursive 蟲螳 殊 : (1)るジ讓 企 螻 Index襯 るジ讓曙朱 豺 企, (2)殊 企 覿覈 碁襦 手. 蟲螳 るジ讓 : (1)殊 企 螻 Index襯 殊曙朱 豺 企, (2)るジ讓 企 覿覈 碁襦 手. 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 蟲螳 殊 るジ讓 磯 るジ 伎 蟲伎 螳 蟲螳 殊 るジ讓 螳れ 願鍵 覓語 殊 るジ讓 企 覿覈 碁襦 手 . (覿覈 碁 殊 螻 るジ讓 ) 磯殊 企 螳 螻, Index襯 るジ讓曙朱 豺 企 覿覈 碁襦 手. るジ讓 殊 企 覿覈 碁襦 手 . (覿覈 碁 殊 螻 るジ讓 ) 磯殊 企 螳 螻, Index襯 殊曙朱 豺 企 覿覈 碁襦 手. 6 2 殊 るジ讓
  • 21. Segment Tree 一 Non recursive 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 殊 語 るジ讓 語 覦覯 殊 : Index螳 讌, るジ讓 : Index螳 覿覈 碁襦 手 Index = Index / 2 6 2 殊 るジ讓 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 蟲螳 殊 : (1)るジ讓 企 螻 Index襯 るジ讓曙朱 豺 企, (2)殊 企 覿覈 碁襦 手. 蟲螳 るジ讓 : (1)殊 企 螻 Index襯 殊曙朱 豺 企, (2)るジ讓 企 覿覈 碁襦 手.
  • 22. Segment Tree 一 Non recursive 6 2 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 5 るジ讓 願鍵 覓語 覿覈 碁襦 手 . 磯殊 5襯 . 蠏 るジ讓曙朱 豺 企螻 覿覈 碁襦 手. 3 るジ讓 願鍵 覓語 覿覈 碁襦 手 . 磯殊 讌 螻 覿覈 碁襦 手. 蟲螳 殊 : (1)るジ讓 企 螻 Index襯 るジ讓曙朱 豺 企, (2)殊 企 覿覈 碁襦 手. 蟲螳 るジ讓 : (1)殊 企 螻 Index襯 殊曙朱 豺 企, (2)るジ讓 企 覿覈 碁襦 手.
  • 23. Segment Tree 一 Non recursive 6 2 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 5 殊 願鍵 覓語 覿覈 碁襦 手 . 磯殊 讌 螻 覿覈 碁襦 手. 11 るジ讓 願鍵 覓語 覿覈 碁襦 手 . 磯殊 讌 螻 覿覈 碁襦 手. 蟲螳 殊 : (1)るジ讓 企 螻 Index襯 るジ讓曙朱 豺 企, (2)殊 企 覿覈 碁襦 手. 蟲螳 るジ讓 : (1)殊 企 螻 Index襯 殊曙朱 豺 企, (2)るジ讓 企 覿覈 碁襦 手.
  • 24. Segment Tree 一 Non recursive 6 2 15 2 7 5 1 4 8 3 17 12 5 11 29 16 45 6 9 16 るジ讓 企. 殊 蠍一朱 るジ讓 企 伎 覩襦 16 . るジ讓 蠍一朱 るジ讓 企 覿覈 碁襦 手 讌襷 殊 るジ讓 螳 襷蠍 覓語 襷豺. 蟲螳 殊 : (1)るジ讓 企 螻 Index襯 るジ讓曙朱 豺 企, (2)殊 企 覿覈 碁襦 手. 蟲螳 るジ讓 : (1)殊 企 螻 Index襯 殊曙朱 豺 企, (2)るジ讓 企 覿覈 碁襦 手.
  • 25. Segment Tree 一 Python/C++ Code def calc_stree(stree: List, L: int, R: int): length = len(stree) // 2 ret = 0 left = L 1 + length right = R 1 + length while left <= right: if left % 2 == 1: ret += stree[left] left += 1 if right % 2 == 0: ret += stree[right] right -= 1 left //= 2 right //= 2 return ret lld calc_stree(const vector<lld>& stree, const int L, const int R) { const lld length = stree.size() / 2; lld ret = 0; lld left = L 1 + length; lld right = R 1 + length; for (left, right; left <= right; left /= 2, right /= 2) { if (left % 2 == 1) { ret += stree[left]; left++; } if (right % 2 == 0) { ret += stree[right]; right--; } } return ret; } Python C++
  • 26. Segment Tree 一危 る 襴碁 Index 蟲蠍 覦一伎 0覯讌 Index襯 Segment Tree 谿場 , る 螻褐 Index 襷 伎. = + 1 企 Leaf Node 覿 Root Node 蟾讌 蟲螳 螳煙蠍 Parent Node Index 朱 蟲 . _ = 2 Leaf Node Parent Node 覿 Root Node 蟾讌 Left Node Right Node 螳煙覃 . 15 2 7 5 3 4 8 3 17 12 7 11 29 18 47 6 9 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 2 7 5 3 4 8 3 6 9 0 1 2 3 4 5 6 7 8 蠍一ヾ 覦一伎 4覯讌 螳1 3朱 覲蟆渚 (蠍一ヾ 覦一伎 覲蟆渚蟆 , Segment Tree 覲蟆渚.) 0 45 29 16 17 12 5 11 15 2 7 5 1 4 8 3 6 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 47 29 18 17 12 7 11 15 2 7 5 3 4 8 3 6 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 sidx螳 讌 - Left Node Index : sidx, Right Node Index : sidx + 1 sidx螳 - Left Node Index : sidx 1, Right Node Index : sidx
  • 27. Segment Tree 一危 Python/C++ Code void update_stree(vector<lld>& stree, lld idx, lld data) { const lld length = stree.size() / 2; lld sidx = length + idx - 1; for (stree[sidx] = data; sidx > 1; sidx /= 2) { if (sidx % 2 == 0) { stree[sidx / 2] = stree[sidx] + stree[sidx + 1]; } else { stree[sidx / 2] = stree[sidx] + stree[sidx 1]; } } } C++ def update_stree(stree: List, idx: int, data): length = len(stree) // 2 sidx = length + idx - 1 stree[sidx] = data while sidx > 1: if sidx % 2 == 0: stree[sidx // 2] = stree[sidx] + stree[sidx + 1] else: stree[sidx // 2] = stree[sidx] + stree[sidx 1] sidx //= 2 Python