Aprendi Fenwick Tree! (Binary Indexed Tree)
This commit is contained in:
parent
e6633ede97
commit
8d6a6e3d61
50
Problems/NepsAcademy/neps_43.cpp
Normal file
50
Problems/NepsAcademy/neps_43.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
vector<int> bit, vetor_100pct_original;
|
||||||
|
|
||||||
|
void update_bit(int pos, int x){
|
||||||
|
int pracsum = -vetor_100pct_original[pos-1];
|
||||||
|
vetor_100pct_original[pos-1] = x;
|
||||||
|
for(; pos <= bit.size(); pos += (pos&-pos)){
|
||||||
|
bit[pos] += pracsum + x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int sumrange_bit(int pos){
|
||||||
|
int answ = 0;
|
||||||
|
for(; pos > 0; pos -= (pos&-pos)){
|
||||||
|
answ += bit[pos];
|
||||||
|
}
|
||||||
|
return answ;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
|
||||||
|
int n,q, x, t,k,p;
|
||||||
|
cin >> n >> q;
|
||||||
|
|
||||||
|
bit.assign(n+1,0);
|
||||||
|
vetor_100pct_original.assign(n,0);
|
||||||
|
for(int i = 1; i <= n; i++){
|
||||||
|
cin >> x;
|
||||||
|
bit[i] += x;
|
||||||
|
vetor_100pct_original[i-1] = x;
|
||||||
|
for(int j = i+(i&-i); j <= n; j += (j&-j)){
|
||||||
|
bit[j] += x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while(q--){
|
||||||
|
cin >> t >> k;
|
||||||
|
if(t == 0){
|
||||||
|
cin >> p;
|
||||||
|
update_bit(k, p);
|
||||||
|
}else{
|
||||||
|
cout << sumrange_bit(k) << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user