-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seg iterativa e algumas mudancas (#155)
* seg iterativa e algumas mudancas * clang-format
- Loading branch information
1 parent
c6eeaf4
commit 9a83c97
Showing
4 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
Codigos/Estruturas-de-Dados/Segment-Tree/Segment-Tree-Iterativa/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# [Segment Tree Iterativa](itseg_tree.cpp) | ||
|
||
Implementação padrão de Segment Tree, suporta operações de consulta em intervalo e update pontual. Está implementada para soma, mas pode ser facilmente modificada para outras operações. A construção é $\mathcal{O}(n)$ e as operações de consulta e update são $\mathcal{O}(\log n )$. | ||
|
||
Essa implementação é iterativa, o que a torna mais eficiente que a recursiva, além de ser mais fácil de implementar. |
41 changes: 41 additions & 0 deletions
41
Codigos/Estruturas-de-Dados/Segment-Tree/Segment-Tree-Iterativa/itseg_tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
struct SegTree { | ||
ll merge(ll a, ll b) { return a + b; } | ||
const ll neutral = 0; | ||
|
||
inline int lc(int p) { return p * 2; } | ||
inline int rc(int p) { return p * 2 + 1; } | ||
|
||
int n; | ||
vector<ll> t; | ||
|
||
void build(int _n) { // pra construir com tamanho, mas vazia | ||
n = _n; | ||
t.assign(n * 2, neutral); | ||
} | ||
|
||
void build(const vector<ll> &v) { // pra construir com vector | ||
n = (int)v.size(); | ||
t.assign(n * 2, neutral); | ||
for (int i = 0; i < n; i++) t[i + n] = v[i]; | ||
for (int i = n - 1; i > 0; i--) t[i] = merge(t[lc(i)], t[rc(i)]); | ||
} | ||
|
||
void build(ll *bg, ll *en) { // pra construir com array de C | ||
build(vector<ll>(bg, en)); | ||
} | ||
|
||
ll query(int l, int r) { | ||
ll ansl = neutral, ansr = neutral; | ||
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) { | ||
if (l & 1) ansl = merge(ansl, t[l++]); | ||
if (r & 1) ansr = merge(t[--r], ansr); | ||
} | ||
return merge(ansl, ansr); | ||
} | ||
|
||
void update(int i, ll x, bool replace = false) { | ||
i += n; | ||
t[i] = replace ? x : merge(t[i], x); | ||
for (i >>= 1; i > 0; i >>= 1) t[i] = merge(t[lc(i)], t[rc(i)]); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters