forked from sanghaisubham/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIT.cpp
More file actions
44 lines (43 loc) · 803 Bytes
/
BIT.cpp
File metadata and controls
44 lines (43 loc) · 803 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
#include<iostream>
#include<string>
using namespace std;
int update(int index,int val,int BITree[],int n)
{
while(index<=n)
{
BITree[index]+=val;
index+=index&(-index);
}
}
int BITsum(int index,int BITree[])
{
int sum=0;
while(index>0)
{
sum+=BITree[index];
index-=index&(-index);
}
return sum;
}
int main()
{
int n,q,a,b,suma=0,sumb=0,i;
string s;
cin>>n>>q;
int BITree[1000001]={0};
while(q--)
{
cin>>s;
cin>>a>>b;
if(s.compare("add")==0)
{
update(a,b,BITree,n);
}
if(s.compare("find")==0)
{
suma=BITsum(a-1,BITree);
sumb=BITsum(b,BITree);
cout<<(sumb-suma)<<endl;
}
}
}