-
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.
Merge pull request #72 from BRUTEUdesc/joao
Joao
- Loading branch information
Showing
16 changed files
with
123 additions
and
108 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
Codigos/Estruturas-de-Dados/Disjoint-Set-Union/DSU-Bipartido/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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [DSU Bipartido](bipartite_dsu.cpp) | ||
|
||
DSU que mantém se um conjunto é bipartido (visualize os conjuntos como componentes conexas de um grafo e os elementos como vértices). O método $unite$ adiciona uma aresta entre os dois elementos dados, e retorna $true$ se os elementos estavam em conjuntos diferentes (componentes conexas diferentes) e $false$ caso contrário. O método $bipartite$ retorna $true$ se o conjunto (componente conexa) que contém o elemento dado é bipartido e $false$ caso contrário. Todas as operações são $\mathcal{O}(\log n)$. | ||
DSU que mantém se um conjunto é bipartido (visualize os conjuntos como componentes conexas de um grafo e os elementos como vértices). O método $unite$ adiciona uma aresta entre os dois elementos dados, e retorna `true` se os elementos estavam em conjuntos diferentes (componentes conexas diferentes) e `false` caso contrário. O método `bipartite` retorna `true` se o conjunto (componente conexa) que contém o elemento dado é bipartido e `false` caso contrário. Todas as operações são $\mathcal{O}(\log n)$. |
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
2 changes: 1 addition & 1 deletion
2
Codigos/Estruturas-de-Dados/Disjoint-Set-Union/DSU-Rollback/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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [DSU com Rollback](rollback_dsu.cpp) | ||
|
||
DSU que desfaz as últimas operações. O método $checkpoint$ salva o estado atual da estrutura, e o método $rollback$ desfaz as últimas operações até o último checkpoint. As operações de unir dois conjuntos e verificar em qual conjunto um elemento está são $\mathcal{O}(\log n)$, o rollback é $\mathcal{O}(k)$, onde $k$ é o número de alterações a serem desfeitas e o $checkpoint$ é $\mathcal{O}(1)$. Importante notar que o rollback não altera a complexidade de uma solução, uma vez que $\sum k = \mathcal{O}(q)$, onde $q$ é o número de operações realizadas. | ||
DSU que desfaz as últimas operações. O método `checkpoint` salva o estado atual da estrutura, e o método `rollback` desfaz as últimas operações até o último checkpoint. As operações de unir dois conjuntos e verificar em qual conjunto um elemento está são $\mathcal{O}(\log n)$, o rollback é $\mathcal{O}(k)$, onde $k$ é o número de alterações a serem desfeitas e o `checkpoint` é $\mathcal{O}(1)$. Importante notar que o rollback não altera a complexidade de uma solução, uma vez que $\sum k = \mathcal{O}(q)$, onde $q$ é o número de operações realizadas. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [DSU](dsu.cpp) | ||
|
||
Estrutura que mantém uma coleção de conjuntos e permite as operações de unir dois conjuntos e verificar em qual conjunto um elemento está, ambas em $\mathcal{O}(1)$ amortizado. O método $find$ retorna o representante do conjunto que contém o elemento, e o método $unite$ une os conjuntos que contém os elementos dados, retornando $true$ se eles estavam em conjuntos diferentes e $false$ caso contrário. | ||
Estrutura que mantém uma coleção de conjuntos e permite as operações de unir dois conjuntos e verificar em qual conjunto um elemento está, ambas em $\mathcal{O}(1)$ amortizado. O método `find` retorna o representante do conjunto que contém o elemento, e o método `unite` une os conjuntos que contém os elementos dados, retornando `true` se eles estavam em conjuntos diferentes e `false` caso contrário. |
2 changes: 1 addition & 1 deletion
2
Codigos/Estruturas-de-Dados/Disjoint-Set-Union/Offline-DSU/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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [DSU Offline](offline_dsu.cpp) | ||
|
||
Algoritmo que utiliza o Full DSU (DSU com Rollback e Bipartido) que permite adição e **remoção** de arestas. O algoritmo funciona de maneira offline, recebendo previamente todas as operações de adição e remoção de arestas, bem como todas as perguntas (de qualquer tipo, conectividade, bipartição, etc), e retornando as respostas para cada pergunta no retorno do método $solve$. Complexidade total $\mathcal{O}(q\cdot(\log q + \log n))$, onde $q$ é o número de operações realizadas e $n$ é o número de nodos. | ||
Algoritmo que utiliza o Full DSU (DSU com Rollback e Bipartido) que permite adição e **remoção** de arestas. O algoritmo funciona de maneira offline, recebendo previamente todas as operações de adição e remoção de arestas, bem como todas as perguntas (de qualquer tipo, conectividade, bipartição, etc), e retornando as respostas para cada pergunta no retorno do método `solve`. Complexidade total $\mathcal{O}(q\cdot(\log q + \log n))$, onde $q$ é o número de operações realizadas e $n$ é o número de nodos. |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [Fenwick Tree](fenwick_tree.cpp) | ||
|
||
Árvore de Fenwick (ou BIT) é uma estrutura de dados que permite atualizações pontuais e consultas de prefixos em um vetor em $\mathcal{O}(\log n)$. A implementação abaixo é 0-indexada (é mais comum encontrar a implementação 1-indexada). A consulta em ranges arbitrários com o método $query$ é possível para qualquer operação inversível, como soma, XOR, multiplicação, etc. A implementação abaixo é para soma, mas é fácil adaptar para outras operações. O método $update$ soma $d$ à posição $i$ do vetor, enquanto o método $updateSet$ substitue o valor da posição $i$ do vetor por $d$. | ||
Árvore de Fenwick (ou BIT) é uma estrutura de dados que permite atualizações pontuais e consultas de prefixos em um vetor em $\mathcal{O}(\log n)$. A implementação abaixo é 0-indexada (é mais comum encontrar a implementação 1-indexada). A consulta em ranges arbitrários com o método `query` é possível para qualquer operação inversível, como soma, XOR, multiplicação, etc. A implementação abaixo é para soma, mas é fácil adaptar para outras operações. O método `update` soma $d$ à posição $i$ do vetor, enquanto o método `updateSet` substitue o valor da posição $i$ do vetor por $d$. |
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
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 |
---|---|---|
@@ -1,51 +1,65 @@ | ||
typedef long long ll; | ||
template <ll MINL = ll(-1e9 - 5), ll MAXR = ll(1e9 + 5)> struct LichaoTree { | ||
const ll INF = ll(2e18) + 10; | ||
struct Line { | ||
ll a, b; | ||
Line(ll a_ = 0, ll b_ = -INF) : a(a_), b(b_) { } | ||
ll operator()(ll x) { return a * x + b; } | ||
}; | ||
vector<Line> tree; | ||
vector<int> L, R; | ||
|
||
const ll MAXN = 2e5 + 5, INF = 1e18 + 9, MAXR = 1e18; | ||
int newnode() { | ||
tree.push_back(Line()); | ||
L.push_back(-1); | ||
R.push_back(-1); | ||
return int(tree.size() - 1); | ||
} | ||
|
||
struct Line { | ||
ll a, b = -INF; | ||
__int128 operator()(ll x) { return (__int128)a * x + b; } | ||
} tree[4 * MAXN]; | ||
int idx = 0, L[4 * MAXN], R[4 * MAXN]; | ||
LichaoTree() { newnode(); } | ||
|
||
int le(int n) { | ||
if (!L[n]) { | ||
L[n] = ++idx; | ||
} | ||
return L[n]; | ||
} | ||
int ri(int n) { | ||
if (!R[n]) { | ||
R[n] = ++idx; | ||
int le(int u) { | ||
if (L[u] == -1) { | ||
L[u] = newnode(); | ||
} | ||
return L[u]; | ||
} | ||
return R[n]; | ||
} | ||
|
||
void insert(Line line, int n = 0, ll l = -MAXR, ll r = MAXR) { | ||
ll mid = (l + r) / 2; | ||
bool bl = line(l) < tree[n](l); | ||
bool bm = line(mid) < tree[n](mid); | ||
if (!bm) { | ||
swap(tree[n], line); | ||
} | ||
if (l == r) { | ||
return; | ||
int ri(int u) { | ||
if (R[u] == -1) { | ||
R[u] = newnode(); | ||
} | ||
return R[u]; | ||
} | ||
if (bl != bm) { | ||
insert(line, le(n), l, mid); | ||
} else { | ||
insert(line, ri(n), mid + 1, r); | ||
} | ||
} | ||
|
||
__int128 query(int x, int n = 0, ll l = -MAXR, ll r = MAXR) { | ||
if (l == r) { | ||
return tree[n](x); | ||
void insert(Line line, int n = 0, ll l = MINL, ll r = MAXR) { | ||
ll mid = (l + r) / 2; | ||
bool bl = line(l) > tree[n](l); | ||
bool bm = line(mid) > tree[n](mid); | ||
bool br = line(r) > tree[n](r); | ||
if (bm) { | ||
swap(tree[n], line); | ||
} | ||
if (line.b == -INF) { | ||
return; | ||
} | ||
if (bl != bm) { | ||
insert(line, le(n), l, mid - 1); | ||
} else if (br != bm) { | ||
insert(line, ri(n), mid + 1, r); | ||
} | ||
} | ||
ll mid = (l + r) / 2; | ||
if (x < mid) { | ||
return max(tree[n](x), query(x, le(n), l, mid)); | ||
} else { | ||
return max(tree[n](x), query(x, ri(n), mid + 1, r)); | ||
|
||
ll query(int x, int n = 0, ll l = MINL, ll r = MAXR) { | ||
if (tree[n](x) == -INF || (l > r)) | ||
return -INF; | ||
if (l == r) { | ||
return tree[n](x); | ||
} | ||
ll mid = (l + r) / 2; | ||
if (x < mid) { | ||
return max(tree[n](x), query(x, le(n), l, mid - 1)); | ||
} else { | ||
return max(tree[n](x), query(x, ri(n), mid + 1, r)); | ||
} | ||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,10 +1,5 @@ | ||
# [Soma do floor (n/i)](sum_of_floor.cpp) | ||
|
||
<!-- DESCRIPTION --> | ||
Computa | ||
Esse código computa, em $\mathcal{O}(\sqrt{n})$, o seguinte somatório: | ||
|
||
$$ \sum_{i=1}^{n} \lfloor\frac{n}{i}\rfloor $$ | ||
|
||
<!-- DESCRIPTION --> | ||
|
||
- Complexidade de tempo: $\mathcal{O}(\sqrt{n})$ | ||
$$ \sum_{i=1}^{n} \left\lfloor \frac{n}{i}\right\rfloor $$ |
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
Oops, something went wrong.