poj 3368(RMQ问题)

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int num[100100];
int n,q;
int d[100010][20];
int f[100010];
void RMQ_init()
{
for (int i = 1; i<=n; i++) {
d[i][0] = f[i];
}
for (int j = 1; (1<<j)<=n; j++) {
for (int i = 1; i+(1<<j)-1<=n; i++) {
d[i][j] = max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
}
}
int RMQ(int l,int r)
{
if (l>r) {
return 0;
}
int k = 0;
while ((1<<(k+1))<=r-l+1) {
k++;
}
return max(d[l][k], d[r-(1<<k)+1][k]);
}
int main()
{

while (scanf("%d",&n)&&n) {
scanf("%d",&q);
for (int i = 1; i<=n; i++) {
scanf("%d",&num[i]);
if (i == 1) {
f[1] = 1;
continue;
}
else if(num[i] == num[i-1])
{
f[i] = f[i-1]+1;
}
else
f[i] = 1;
}
RMQ_init();
for (int i = 0; i<q; i++) {
int x,y;
scanf("%d%d",&x,&y);
int t = x;
while (t<=y&&num[t] == num[t-1]) {
t++;
}
int cnt = RMQ(t,y);
int ans = max(t-x, cnt);
printf("%d\n",ans);
}
}
return 0;
}

05-20 390

03-09 262
01-30 902
08-13 302
04-10 1670
02-13 286
10-17 49
08-17 24
04-05 511
09-20 2082
11-16 255
08-18 787
10-30 47
09-24 298
03-20 832
09-17 26
04-18 45
08-16 607
04-01 984