-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwicktree.cpp
More file actions
46 lines (42 loc) · 829 Bytes
/
fenwicktree.cpp
File metadata and controls
46 lines (42 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> tree;
int LSOne(int n){
return n & (-n);
}
void update(int pos, int val){
while(pos<=tree.size()){
tree[pos]+=val;
pos+=LSOne(pos);
}
int query(int pos){
int res=0;//neutro
while(pos>0){
res+= tree[pos];
pos-= LSOne(pos);
}
return res;
}
int main(){
int n,k;
cin>>n>>k;
tree.assign(n+1,0);
for(int i=0; i<n;i++){
int aux;
cin>>aux;
update(i+1, aux);//prohibido empezar en 0 ft i+1
}
/*for(auto x: tree)
cout<<x<<endl;
*/
for(int i=0; i<k;i+)
//char op;
int a,b;
cin>>a>>b;
//if(op=='P')
cout<<query(b)-query(a-1)<<endl;
//if(op=='C')
//update(a, b);
}
}