Skip to content

Commit

Permalink
seg iterativa e algumas mudancas (#155)
Browse files Browse the repository at this point in the history
* seg iterativa e algumas mudancas

* clang-format
  • Loading branch information
joaomarcosth9 authored Aug 24, 2024
1 parent c6eeaf4 commit 9a83c97
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
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.
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)]);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct SegTree {
replace.assign(n * 4, false);
build(1, 0, n - 1, v);
}

void build(ll *bg, ll *en) { // pra construir com array de C
build(vector<ll>(bg, en));
}
Expand All @@ -69,7 +70,7 @@ struct SegTree {
}
ll query(int l, int r) { return query(1, 0, n - 1, l, r); }

void update(int p, int l, int r, int L, int R, ll val, bool repl) {
void update(int p, int l, int r, int L, int R, ll val, bool repl = 0) {
push(p, l, r);
if (l > R || r < L) return;
if (l >= L && r <= R) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ struct SegTree {
}

void build(const vector<ll> &v) { // pra construir com vector
n = int(v.size());
n = (int)v.size();
t.assign(n * 4, neutral);
build(1, 0, n - 1, v);
}

void build(ll *bg, ll *en) { // pra construir com array de C
build(vector<ll>(bg, en));
}
Expand All @@ -43,7 +44,7 @@ struct SegTree {
}
ll query(int l, int r) { return query(1, 0, n - 1, l, r); }

void update(int p, int l, int r, int i, ll x, bool repl) {
void update(int p, int l, int r, int i, ll x, bool repl = 0) {
if (l == r) {
if (repl) t[p] = x; // substitui
else t[p] += x; // soma
Expand Down

0 comments on commit 9a83c97

Please sign in to comment.