#include<iostream>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;constint N =200010, M =18;int n, m;int w[N];int f[N][M];//初始化 按区间长度来初始化。voidinit(){for(int j =0; j < M; j ++)for(int i =1; i +(1<< j)-1<= n; i ++)if(!j) f[i][j]= w[i];else f[i][j]=max(f[i][j -1], f[i +(1<< j -1)][j -1]);}//RMQ 区间最值问题 求[l, r]最大值,max(f[l][k],f[r - (1 << k) + 1][k]);intquery(int l,int r){int len = r - l +1;int k =log(len)/log(2);returnmax(f[l][k], f[r -(1<< k)+1][k]);}intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&w[i]);init();scanf("%d",&m);while(m --){int l, r;scanf("%d%d",&l,&r);printf("%d\n",query(l, r));}return0;}
题目链接:天才的记忆题目分析:0.RMQ(区间最值问题)1.题解:题解code:#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N = 200010, M = 18;int n, m;int w[N];int f[N][M];//初始化 按区间长度来初始化。vo