您的位置:首页 > 产品设计 > UI/UE

SPOJ RMQSQ Range Minimum Query  《RMQ》

2016-07-22 20:49 666 查看
Range Minimum Query

Time Limit: 3000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu
Submit Status

Description

You are given a list of numbers and queries. Each query is specified by two numbers i and j; the answer to each query is the minimum number between the range [i, j] (inclusive).

Note: the query ranges are specified using 0-based indexing.

Input

The first line contains N, the number of integers in our list (N <= 100,000). The next line holds N numbers that are guaranteed to fit inside an integer. Following the list is a number (Q
<= 10,000
). The next Q lines each contain two numbers i and which specify a query you must answer (0 <= i, j <= N-1).

Output

For each query, output the answer to that query on its own line in the order the queries were made.

Example

Input:
3
1 4 1
2
1 1
1 2

Output:
4
1


Hint

Added by:Joshua Kirstein
Date:2014-10-18
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster:Cube (Intel G860)
Languages:All
Submit Status

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define MIM 101000
int MI[MIM][50];
int shu[MIM];
int main()
{
int n,m;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&shu[i]);
MI[i][0]=shu[i];
}
int p=log2(n);
for (int i=1;i<=p;i++)
for (int j=1;j-1+(1<<i)<=n;j++)
{
MI[j][i]=min(MI[j][i-1],MI[j+(1<<(i-1))][i-1]);
}
scanf("%d",&m);
int x,y,a,b;
for (int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
x++;y++;
int p=log2(y+1-x);
b=min(MI[x][p],MI[y+1-(1<<p)][p]);
printf("%d\n",b);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: