-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.cpp
More file actions
28 lines (24 loc) · 678 Bytes
/
index.cpp
File metadata and controls
28 lines (24 loc) · 678 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
#include<bits/stdc++.h>
using namespace std;
void update(int index,int value, int* BIT, int n){
for(;index <= n; index += (index & (-index)) )
BIT[index] += value;
}
int query(int index, int* BIT){
int sum = 0;
for(;index > 0; index -= index&(-index))
sum += BIT[index];
return sum;
}
int main(){
int n;
int* arr = new int[n+1]();
int* BIT = new int[n+1]();
for(int i = 1; i <= n; i++){
cin >> arr[i];
update(i,arr[i],BIT,n);
}
cout << "Sum of first 5 elements " << query(5,BIT);
cout << "Sum of elts from index 2 to index 6 " << query(6,BIT) - query(1,BIT) << endl;
return 0;
}