「Day-3 提高笔记-ST表 & RMQ」

#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 1e5 + 5;
int n,m;
int lg2[MAXN];
int f[MAXN][50];

int main(){
	
	cin.tie(0) -> ios::sync_with_stdio(0);
	
	cin >> n >> m;
	
	for(int i = 1;i <= n;i ++){
		cin >> f[i][0];
		if(i != 1) lg2[i] = lg2[i >> 1] + 1; 
	}
	
	for(int j = 1;j <= lg2[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;
		cin >> l >> r;
		int mid = lg2[r - l + 1];
		cout << max(f[l][mid],f[r - (1 << mid) + 1][mid]) << '\n';
	}
	
	return 0;
}
posted @ 2024-10-22 19:18  To_Carpe_Diem  阅读(2)  评论(0编辑  收藏  举报