ST表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define N 100001
int a[N],d[N][20],n,m,x,y;
void st()
{
fo(i,1,n)d[i][0]=a[i];
for(int j=1;(1<<j)<=n;++j)
for(int i=1;i+(1<<j)-1<=n;++i)
d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int query(int l,int r)
{
int k=0;
while(1<<(k+1) <=r-l+1)k++;
return max(d[l][k],d[r-(1<<k)+1][k]);
}
int main()
{
scanf("%d%d",&n,&m);
fo(i,1,n)scanf("%d",&a[i]);
st();
fo(i,1,m)
{
scanf("%d%d",&x,&y);
printf("%d\n",query(x,y));
}
return 0;
}
显示 Gitment 评论