Skip to content

Commit

Permalink
General Sparse Table
Browse files Browse the repository at this point in the history
  • Loading branch information
theSoberSobber authored Apr 10, 2024
1 parent d452810 commit b9a20f3
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions codes/Sparse-General.cpp
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

0 comments on commit b9a20f3

Please sign in to comment.