其他分享
首页 > 其他分享> > ST表模板

ST表模板

作者:互联网

#include<bits/stdc++.h>
using namespace std;
int n,m;
int f[100111][50],logn[10000011];
inline int read(){
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int main(){
	n = read();m = read();
	for(int i = 2;i <= n;i++){
		logn[i] = logn[i >> 1] + 1;//初始化,logn数组代表它是代表2的几次方的 
	}
	for(int i = 1;i <= n;i++){
		f[i][0] = read();//输入 
	}
	for(int j = 1;j <= logn[n];j++){//判断边界 
		for(int i = 1;i <= n - (1 << j) + 1;i++){//判断边界 
			f[i][j] = max(f[i][j - 1],f[i + (1 << (j - 1))][j - 1]);//预处理 
		}
	}
	for(int i = 1;i <= m;i++){
		int l,r;
		l = read();
		r = read();
		int k = logn[r - l + 1]; //找到它属于几次方范围内
		printf("%d\n",max(f[l][k],f[r - (1 << k) + 1][k]));//求最值 
		//cout << max(f[l][k],f[r - (1 << k) + 1][k]) << endl;
	}
	return 0;
}

标签:int,max,ST,read,while,logn,模板,getchar
来源: https://blog.csdn.net/luozhichen/article/details/122702836