Skip to content

Commit

Permalink
Add c++ fenwick tree
Browse files Browse the repository at this point in the history
Signed-off-by: jparisu <[email protected]>
  • Loading branch information
jparisu committed Mar 2, 2024
1 parent fd1b303 commit 55d06a1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/md/intermedio/fenwick/fenwick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Fenwick Tree

```{todo}
`Work In Progress`
```


## Lenguajes

```{toctree}
:maxdepth: 1
fenwick_cpp
fenwick_py
```


## Ejercicios

| Name | Description | Level | URL |
|---------------|------------------|-------|------------------------------------------------|
| fenwick | Update & query | 4.0 | https://open.kattis.com/problems/fenwick |
| supercomputer | Bitsum & biquery | 2.7 | https://open.kattis.com/problems/supercomputer |
| downtime | | 3.1 | https://open.kattis.com/problems/downtime |
65 changes: 65 additions & 0 deletions docs/md/intermedio/fenwick/fenwick_cpp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Bucles [C++]

```{contents}
:local:
:depth: 2
```


```cpp
#include <iostream>
#include <vector>
using namespace std;

struct FenwickTree {

// O(N)
// Create a Fenwick tree with n elements, all initialized to zero
FenwickTree(int size)
: n(size)
, tree(size)
{}

// O(logN)
// Add value to the element at index (index starts on 0)
void update(int index, int value) {
index++;
while (index <= n) {
tree[index - 1] += value;
index += index & -index;
}
}

// O(logN)
// Return the sum from 0 to index (no included) (index starts on 0)
int query(int index) {
int acc = 0;
while (index > 0) {
acc += tree[index - 1];
index -= index & -index;
}
return acc;
}

// O(logN)
// Return the sum from 0 to index (included) (index starts on 0)
int query_includes(int index) {
int acc = 0;
index++;
while (index > 0) {
acc += tree[index - 1];
index -= index & -index;
}
return acc;
}

// O(logN)
// Return the sum from a to b (both included)
int biquery(int a, int b) {
return query_includes(b) - query(a);
}

vector<int> tree;
int n;
};
```
10 changes: 10 additions & 0 deletions docs/md/intermedio/fenwick/fenwick_py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Bucles [Python]

```{contents}
:local:
:depth: 2
```

```{todo}
`Work In Progress`
```
11 changes: 9 additions & 2 deletions docs/md/intermedio/intermedio.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

# Nivel intermedio

```{todo}
`Work In Progress`
**1º de programación**.
Este nivel enseña los conceptos básicos de programación y de Programación competitiva.

```{toctree}
:maxdepth: 1
:caption: Temario
entrada_salida/entrada_salida
bucles/bucles
```

0 comments on commit 55d06a1

Please sign in to comment.