Skip to content

Instantly share code, notes, and snippets.

View brpapa's full-sized avatar
👋

Bruno Papa brpapa

👋
View GitHub Profile
@brpapa
brpapa / query.cpp
Last active December 30, 2019 05:32
segment tree
// [ql .. qr]: intervalo de consulta
int rangeQuery(int v, int l, int r, int ql, int qr) {
// v: índice atual da bt
// [l .. r]: intervalo atual de arr
if (ql > r || qr < l)
// [l .. r] está completamente fora de [ql .. qr]
return INF;
if (l >= ql && r <= ql)
@brpapa
brpapa / point-update.cpp
Last active December 30, 2019 05:32
segment tree
// value: novo valor de arr[i]
void pointUpdate(int v, int l, int r, int i, int value) {
// v: índice atual da bt
// [l .. r]: intervalo atual de arr
if (l == r) {
// v é nó folha
bt[v] = value; return;
}
@brpapa
brpapa / build.cpp
Last active December 30, 2019 04:31
segment tree
void buildBT(int v, int l, int r) {
// v: índice atual da bt
// [l .. r]: intervalo atual de arr
if (l == r) {
bt[v] = arr[l]; return;
}
int mid = (l + r) / 2;
buildBT(2*v+1, l, mid); // filho à esq de v
buildBT(2*v+2, mid+1, r); // filho à dir de v