-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBIT.cpp
52 lines (52 loc) · 935 Bytes
/
BIT.cpp
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
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
vector<long int>bit(10000,0);
void updatebit(int n,int index,int val)
{
index+=1;
while(index<=n)
{
bit[index]+=val;
index+=index&(-index);
}
}
long int getsum(int index)
{
index+=1;
long int sum=0;
while(index>0)
{
sum+=bit[index];
index-=index&(-index);
}
return sum;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,q,l,r,t,f;
vector<long int> arr(10000);
cin>>n>>q;
for(int i=0;i<n;i++)
{
cin>>arr[i];
updatebit(n,i,arr[i]);
}
for(int i=0;i<q;i++)
{
cin>>t>>l>>r>>f;
if(t==1)
{
updatebit(n,l,f);
updatebit(n,r+1,-f);
}
else if(t==2)
{
cout<<getsum(l)<<endl;
}
}
}
}