SPOJ RMQSQ Range Minimum Query  《RMQ》

本文介绍了一种解决区间最小值查询问题的有效算法。该算法利用预处理技术,通过构建一个二维数组来存储不同长度区间的最小值,实现快速查询。输入包括一系列整数和多个查询指令,每个查询询问指定区间内的最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 Status

Description

You are given a list of numbers and queries. Each query is specified by two numbers 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

 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值