-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsparsetable.cpp
69 lines (69 loc) · 1.19 KB
/
sparsetable.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll sparsetable[100][100];
ll data[100];
ll twopow[100];
void init(){
ll i = 0;
twopow[0] = 1;
for (i=1;i<100;i++){
twopow[i] = 2*twopow[i-1];
}
}
void buildtable(ll n){
//Building table from bottom
ll i=0,k;
for (i=n;i>0;i--){
for (k=0;i+twopow[k]<=n+1;k++){
if (k == 0){
sparsetable[i][k] = data[i];
}
else{
sparsetable[i][k] = sparsetable[i][k-1] + sparsetable[i+twopow[k-1]][k-1];
}
}
}
}
void printtable(ll n){
ll i=0,k;
for (i=1;i<=n;i++){
for (k=0;i+twopow[k]<=n+1;k++){
cout << sparsetable[i][k] << " ";
}
cout << endl;
}
}
ll query(ll start,ll end){
ll k=0,ans=0;
end = end + 1;
while (start != end){
if (start + twopow[k] > end){
ans = ans + sparsetable[start][k-1];
start = start + twopow[k-1];
k = 0;
}
else{
k++;
}
}
return ans;
}
int main(){
init();
ll i,n,j;
cout << "Enter no of elements in the array ";
cin >> n;
cout << "Enter the elements " << endl;
for (i=1;i<=n;i++){
cin >> data[i];
}
buildtable(n);
printtable(n);
for (i=1;i<=n;i++){
for (j=i;j<=n;j++){
cout << "i is " << i << " j is " << j << " " << query(i,j) << endl;
}
}
return 0;
}