-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBIT_Q1.cpp
More file actions
77 lines (63 loc) · 955 Bytes
/
BIT_Q1.cpp
File metadata and controls
77 lines (63 loc) · 955 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*Question 1 Given array of size N and Q querries
Solve two type of quereies
Type1 : i x : replace ith index by value x
Type 2 : l r : find sum from l to r
Input :
N Q
N elemnts of array
Type i x / Type l r(Q lines)
Example :
5 4
3 4 5 6 3
2 2 4
1 2 6
2 2 4
2 1 5
Output :
15
17
23
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int bit[N];
void update(int i,int x){
for(;i<=N;i+=(i&(-i)))
{
bit[i]+=x;
}
}
int sum(int i){
int ans=0;
for(;i>0;i-=(i&(-i))){
ans+=bit[i];
}
return ans;
}
int main(){
int n,q;
cin>>n>>q;
int a[n+1];
//Always 1 based indexing in BIT
for(int i=1;i<=n;i++){
cin>>a[i];
//Inserting the elements in BIT;
update(i,a[i]);
}
while(q--){
int type;
cin>>type;
if(type==1){
int i,x;
cin>>i>>x;
update(i,a[i]-x);
a[i]=x;
}
else{
int l,r;
cin>>l>>r;
cout<<sum(r)-sum(l-1)<<endl;
}
}
}