Last active
June 17, 2019 22:02
-
-
Save Rockbet/c2f7a15927bf686bec7b4b285ec1be56 to your computer and use it in GitHub Desktop.
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
// Os parametros da função Update são: | |
// O nó atual, tl e tr, o intervalo que o nó atual abrange, idx e x, o índice que queremos atualizar e o seu valor. | |
void update(int node, int tl, int tr, int idx, int x){ | |
// Se o nó atual é uma folha, então ele representa o intervalo de idx até idx. | |
// Então, atualizaremos o seu valor e retornaremos a função. | |
if(tl==tr){ | |
tree[node]=x; | |
vet[l]=x; | |
return; | |
} | |
// Caso contrário, declararemos mid como o índice que divide o nó atual em duas metades. | |
int mid = (tl+tr)/2; | |
// Se idx se encontra no intervalo de tl até mid, atualizaremos o filho da esquerda | |
// do nó atual, pos ele abrange o intervalo de tl até mid. | |
// Senão, então ele se encontra de mid+1 até r, então: | |
// Basta atualizarmos o filho da direita do nó atual. | |
if(tl<=idx and idx<=mid) update(2*node+1,tl,mid,idx,v); | |
else update(2*node+2,mid+1,tr,idx,v); | |
// Após termos atualizado um dos dois filhos, atualizaremos o valor do nó atual. | |
tree[node] = tree[2*node+1] + tree[2*node+2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment