为什么我用ST表没有过?T了最后两个点
查看原帖
为什么我用ST表没有过?T了最后两个点
574863
yhy_csp楼主2022/4/30 15:37
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#define maxn 100005
/*
倍增:成倍的增加
[l,r]求区间最大值
线段树,树状数组
st表:求区间统计
*/
/*
10 3
1 2 4 9 10 9 7 6 2 9
1 10
4 6
5 9
*/
int n,m;
int p[maxn][21];
//p[i][j]表示以i元素为起点,长度为2^j的区间
using namespace std;
//二分思想,倍增思想
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int main()
{
   	ios::sync_with_stdio(false);
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		p[i][0]=read();
	}
	for(int j=1;(1<<j)<=n;j++)
	{
		for(int i=1;i+(1<<j)-1<=n;i++)
		{
			p[i][j]=max(p[i][j-1],p[i+(1<<(j-1))][j-1]);
			//p[1][1]=max(a[1],a[2])
		}
	}
	for(int i=1;i<=m;i++)
	{
		int l,r;
  		l=read();
  		r=read();
  		int k=log2(r-l+1);
		cout<<max(p[l][k],p[r-(1<<k)+1][k])<<endl;
	}
	return 0;
}
2022/4/30 15:37
加载中...