洛谷P3865 【模板】ST表

OI-Wiki

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#pragma GCC optimize("O2")
using namespace std;
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ls rt<<1
#define rs rt<<1|1
#define one first
#define two second
#define MS 100009
#define INF 1e18
#define mod 998244353
#define Pi acos(-1.0)
#define Pair pair<LL,LL>
#define eps 1e-9

LL n,m,k;
LL lg2[MS]; 
LL p[MS][31];

inline LL read(){
	LL x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

void init_lg2(){
	lg2[1] = 0;
	for(int i=2;i<=n;i++) lg2[i] = lg2[i/2]+1;
}

int main(){
	//ios::sync_with_stdio(false);
	n = read();
	m = read();
	init_lg2();
	for(int i=1;i<=n;i++){
		p[i][0] = read();
	}
	for(int i=1;i<=lg2[n];i++){
		for(int j=1;j+(1<<(i-1))<=n;j++){
			p[j][i] = max(p[j][i-1],p[j+(1<<(i-1))][i-1]);
		}
	}
	while(m--){
		LL l,r;
		l = read() , r = read();
		LL t = lg2[r-l+1];
		LL ans = max(p[l][t],p[r-(1<<t)+1][t]);
		printf("%lld\n",ans);
	}
	

	return 0;
}
posted @ 2021-03-14 10:52  棉被sunlie  阅读(52)  评论(0)    收藏  举报