还是rmq.
原来如此
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int a[N],b[N],rmq[N][20];
int pw(int k){
int res=1;
while(k--) res*=2;
return res;
}
int getk(int l,int r){
int k=0;
while(r-l+1>=pw(k+1)) ++k;
return k;
}
int main() {
int n,qn;
while(~scanf("%d",&n) && n){
scanf("%d",&qn);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<=n;){
int p=upper_bound(a+1,a+1+n,a[i])-a-1;
for(int j=i;j<p;++j) b[j]=0;
b[p]=p-i+1;
i=p+1;
}
for(int i=1;i<=n;++i) rmq[i][0]=b[i];
for(int u=1;u<=20;++u){
for(int i=1;i+pw(u)<=n;++i){
rmq[i][u]=max(rmq[i][u-1],rmq[i+pw(u-1)][u-1]);
}
}
for(int i=1;i<=qn;++i){
int l,r;scanf("%d%d",&l,&r);
int p1=upper_bound(a+1,a+1+n,a[l])-a;
--p1;
if(p1>r) p1=r;
int p2=lower_bound(a+1,a+1+n,a[r])-a;
if(p2<l) p2=l;
int res=max(p1-l+1,r-p2+1);
if(p1+1<=p2-1){
int k=getk(p1+1,p2-1);
res=max(res,max(rmq[p1+1][k],rmq[p2+1-pw(k)][k]));
}
printf("%d\n",res);
}
}
return 0;
}
标签:rmq,3368,int,res,scanf,while,POJ
From: https://www.cnblogs.com/zan-mei-tai-yang/p/18399125