Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sparse table algorithm #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jatin86400 wants to merge 4 commits into codeIIEST:master from Jatin86400:sparse-table
Closed
(追記)
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Competitive Coding/Search/sparse-table/readme.md
View file Open in desktop

Sparse Table Algorithm

(追記ここまで)
(追記) (追記ここまで)(追記)

Sparse table method supports query time O(1) with extra space O(n Log n).

(追記ここまで)
(追記)

The idea is to precompute minimum of all subarrays of size 2j where j varies from 0 to Log n. Like method 1, we make a lookup table. Here ~lookup[i][j] contains minimum of range starting from i and of size 2j. For example ~lookup[0][3] contains minimum of range [0, 7] (starting with 0 and of size 23)

(追記ここまで)
(追記)

Preprocessing

(追記ここまで)
(追記) (追記ここまで)(追記)

How to fill this lookup table? The idea is simple, fill in bottom up manner using previously computed values.

(追記ここまで)
(追記)

For example, to find minimum of range [0, 7], we can use minimum of following two.

(追記ここまで)
  • Minimum of range [0, 3]
  • Minimum of range [4, 7]
(追記)

Based on above example, below is formula,

(追記ここまで)
(追記)

If ~arr[lookup[i][j-1]] <= ~arr[lookup[i+2j-1-1][j-1]] ~lookup[i][j] = ~lookup[i][j-1]

(追記ここまで)
(追記)

Else ~lookup[i][j] = ~lookup[i+2j-1-1][j-1]

(追記ここまで)
(追記)

alt text

(追記ここまで)
(追記)

Query

(追記ここまで)
(追記) (追記ここまで)(追記)

For any arbitrary range [l, R], we need to use ranges which are in powers of 2. The idea is to use closest power of 2. We always need to do at most one comparison (compare minimum of two ranges which are powers of 2). One range starts with L and and ends with "L + closest-power-of-2". The other range ends at R and starts with "R – same-closest-power-of-2 + 1". For example, if given range is (2, 10), we compare minimum of two ranges (2, 9) and (3, 10).

(追記ここまで)
(追記)

j = floor(Log(R-L+1))

(追記ここまで)
(追記)

If ~arr[lookup[L][j]] <= ~arr[lookup[R-(int)pow(2,j)+1][j]] RMQ(L, R) = ~lookup[L][j]

(追記ここまで)
(追記)

Else RMQ(L, R) = ~lookup[i+2j-1-1][j-1]

(追記ここまで)
(追記)

Since we do only one comparison, time complexity of query is O(1).

(追記ここまで)
Binary file not shown.
60 changes: 60 additions & 0 deletions Competitive Coding/Search/sparse-table/sparse-table.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<bits/stdc++.h>
using namespace std;

#define md 1000000007
#define ll long long int
#define vi vector<int>
#define vll vector<i64>
#define pb push_back
#define all(c) (c).begin(),(c).end()
int main()
{

int n;
cin>>n;//Input the number of elements in the array.
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];//read all the elements
}
int cols = log(n)/log(2);//Cols represent the no of coloumns in the sparse table .
int rows = n;//rows represent the no of rows in the sparse table
int lookup[rows][cols+1];
for(int i=0;i<=cols;i++)//This is the procedure to build a sparse table
{

for(int j=0;j<rows;j++)
{
if(i==0)
lookup[j][i]=j;
else
{

if(j+pow(2,i)<=n)
{

if(a[lookup[j+(int)pow(2,i-1)][i-1]]>a[lookup[j][i-1]])//This is the procedure to create a sparse table for range minimum query. This statement of code can be appropriately changed for another type of query.
lookup[j][i]=lookup[j][i-1];
else
lookup[j][i] = lookup[j+(int)pow(2,i-1)][i-1];

}
}
//cout<<stable[j][i]<<endl;
}
}

int left,right;
cin>>left>>right;
int k = right-left+1;//This represent the total length of the query range.

int col = (int)(log(k)/log(2));//This represent the coloumn we first look for in the sparse table.

int min1 = a[lookup[left-1][col]];
int remaining = k- pow(2,col);
int min2 = a[lookup[left+remaining-1][col]];

cout<<"minimum in the given range is :"<<min(min1,min2)<<endl;


}

AltStyle によって変換されたページ (->オリジナル) /