-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d452810
commit b9a20f3
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//Sparse-General | ||
//General Implementation of Sparse table with the template<class T> structure | ||
|
||
template<class T> | ||
class sparseTable | ||
{ | ||
public: | ||
int n,k; | ||
vector<vector<T>> table; | ||
vector<T> logs; | ||
function<T(T,T)> operation; | ||
void init(int x,function<T(T,T)> _operation) | ||
{ | ||
operation=_operation; | ||
n=x; | ||
logs.resize(n+1); | ||
logs[1]=0; | ||
for(int i=2;i<=n;i++) | ||
logs[i]=logs[i/2]+1; | ||
k=*max_element(logs.begin(),logs.end()); | ||
table.resize(k+1,vector<T>(n)); | ||
} | ||
|
||
void build(vector<T> &arr) | ||
{ | ||
for(int i=0;i<n;i++) | ||
table[0][i]=arr[i]; | ||
|
||
for(int j=1;j<=k;j++) | ||
{ | ||
for(int i=0;i+(1<<j)<=n;i++) | ||
table[j][i]=operation(table[j-1][i],table[j-1][i+(1<<(j-1))]); | ||
} | ||
} | ||
// 1 based indexing | ||
T query(int l , int r) | ||
{ | ||
assert(l<=r); | ||
assert(l>=0 && r<n); | ||
int j = logs[r - l + 1]; | ||
T answer = operation(table[j][l], table[j][r-(1<<j)+1]); | ||
return answer; | ||
} | ||
}; | ||
|
||
|
||
// does not have a constructor, make an instance and then use the init method to use this |