RMQ求區間最值問題
阿新 • • 發佈:2019-02-03
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int map[1000005][20];
int n;
void work()
{
int i,j;
for(j=1;1<<j<=n;j++){
for(i=1;i+(1<<j)-1<=n;i++){
map[i][j]=min(map[i][j-1],map[i+(1<<j-1)][j-1]);
}
}
}
int query(int z,int y)
{
int x=int (log(y-z+1)/log(2));
return min(map[z][x],map[y-(1<<x)+1][x]);
}
int main()
{
scanf("%d",&n);
int i,a,b,k;
for(i=1;i<=n;i++)
scanf("%d",&map[i][0]);
work();
scanf("%d",&k);
for(i=1;i<=k;i++){
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b));
}
return 0;
}
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int map[1000005][20];
int n;
void work()
{
int i,j;
for(j=1;1<<j<=n;j++){
for(i=1;i+(1<<j)-1<=n;i++){
map[i][j]=min(map[i][j-1],map[i+(1<<j-1)][j-1]);
}
}
}
int query(int z,int y)
{
int x=int (log(y-z+1)/log(2));
return min(map[z][x],map[y-(1<<x)+1][x]);
}
int main()
{
scanf("%d",&n);
int i,a,b,k;
for(i=1;i<=n;i++)
scanf("%d",&map[i][0]);
work();
scanf("%d",&k);
for(i=1;i<=k;i++){
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b));
}
return 0;
}