Cod sursa(job #2981313)
| Utilizator | Data | 17 februarie 2023 18:04:48 | |
|---|---|---|---|
| Problema | Arbori indexati binar | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <iostream>
#define N 1002
int Tree[N], n;
void update(int pos, int element){
while(pos <= n){
Tree[pos] += element;
pos += (pos & -pos);
}
}
int query(int pos){
int sum = 0;
while(pos > 0){
sum += Tree[pos];
pos -= (pos & -pos);
}
return sum;
}
int main(){
//int n;
std::cin >> n;
for(int i=1; i<=n; i++){
int element;
std::cin >> element;
update(i, element);
}
std::cout << query(4);
}