ST表模板题,套板子即可
ST表模板:点我打开链接
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e5+5;
int dpMin[N][20],dpMax[N][20];
int a[N];
int n,q;
void Init()
{
for(int i=1;i<=n;i++)
dpMin[i][0] = dpMax[i][0] = a[i];
for (int j=1;j<=log(n)/log(2);j++) //只能j在外,i在内
{
for(int i=1;i<=n;i++)
{
if (i+(1<<j)-1<=n)
{
dpMin[i][j]=min(dpMin[i][j-1],dpMin[i+(1<<(j-1))][j-1]);
dpMax[i][j]=max(dpMax[i][j-1],dpMax[i+(1<<(j-1))][j-1]);
}
else
break;
}
}
}
int ST_min(int l,int r)
{
int k=log(r-l+1)/log(2.0);
return min(dpMin[l][k],dpMin[r-(1<<k)+1][k]);
}
int ST_max(int l,int r)
{
int k=log(r-l+1)/log(2.0);
return max(dpMax[l][k],dpMax[r-(1<<k)+1][k]);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
int l,r;
while(~scanf("%d%d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
Init();
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",ST_max(l,r));
}
}
return 0;
}