mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create RMQ-ST.cpp
This commit is contained in:
parent
7c51a21e45
commit
632bcfa92c
34
.ACM-Templates/RMQ-ST.cpp
Normal file
34
.ACM-Templates/RMQ-ST.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
namespace RMQ_ST
|
||||
{
|
||||
const int MAXN=10000;
|
||||
int f[MAXN][MAXN];
|
||||
int a[MAXN];
|
||||
int n;
|
||||
void init()
|
||||
{
|
||||
for(int i = 1;i<=n;i++)
|
||||
{
|
||||
f[i][0]=a[i];
|
||||
}
|
||||
for(int j=1;(1<<j)<=n;j++)
|
||||
{
|
||||
for(int i=1;i+(i<<j)-1<=n;i++)
|
||||
{
|
||||
f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int rmq(int L,int R)
|
||||
{
|
||||
int k=0;
|
||||
while((1<<(k+1)<=R-L+1)) k++;
|
||||
return max(f[L][k],f[R-(1<<k)+1][k]);
|
||||
}
|
||||
|
||||
|
||||
}/// End of namespace RMQ_ST
|
Loading…
Reference in New Issue
Block a user