Skip to content

Commit

Permalink
Create sparse_table.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravUppoor authored Sep 17, 2021
1 parent 5e7d8b3 commit dc16ba0
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Data Structures/sparse_table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Sparse Table
// Range Min Queries
// Time - O(1)
// Space - O(N*32)

template <typename T>
class SparseTable {
public:
using F = function<T(const T&, const T&)>;

int n;
vector<vector<T>> mat;
F f;
SparseTable(): n(), mat(), f() {}

template <typename U>
SparseTable(const U& arr, int n, F f): n(n), f(f), mat((int)log2(n) + 1) {
mat[0].resize(n);
for (int i = 0; i < n; i++)
mat[0][i] = T(arr[i]);
for (int j = 1; j < mat.size(); j++) {
mat[j].resize(n - (1 << j) + 1);
for (int i = 0; i + (1 << j) <= n; i++) {
mat[j][i] = f(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
}
}
}
T query(int l, int r) {
int j = 32 - __builtin_clz(r - l + 1) - 1;
return f(mat[j][l], mat[j][r + 1 - (1 << j)]);
}
};

void solve() {
int n; cin >> n;

vector<int> a(n);
for(auto &x : a) cin >> x;

SparseTable<int> gcd(a, n, [&](int a, int b) { return __gcd(a,b); });
SparseTable<int> mn(a, n, [&](int a, int b) { return min(a,b); });
}

0 comments on commit dc16ba0

Please sign in to comment.