SPOJ RMQSQ Range Minimum Query 《RMQ》
2016-07-22 20:49
666 查看
Range Minimum Query
Submit Status
Description
You are given a list of N numbers and Q 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.
<= 10,000). The next Q lines each contain two numbers i and j which specify a query you must answer (0 <= i, j <= N-1).
Hint
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;
}
Time Limit: 3000MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
You are given a list of N numbers and Q 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 (Q<= 10,000). The next Q lines each contain two numbers i and j 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 |
代码:
#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;
}
相关文章推荐
- 基于MVC+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出
- 基于MVC+EasyUI的Web开发框架经验总结(9)--在Datagrid里面实现外键字段的转义操作
- 基于MVC+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
- 基于MVC+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动
- 128. Longest Consecutive Sequence
- hdoj 2818 Building Block(加权并查集)
- 基于MVC+EasyUI的Web开发框架经验总结(6)--在页面中应用下拉列表的处理
- UITabelView自适应高度那些事
- 【burpsuite实战】暴力破解密码
- 基于MVC+EasyUI的Web开发框架经验总结(5)--使用HTML编辑控件CKEditor和CKFinder
- 有意思的GacUI
- LinkedBlockingQueue的offer与put的区别
- 基于MVC+EasyUI的Web开发框架经验总结(4)--使用图表控件Highcharts
- Number Sequence(存在循环规律)
- 基于MVC+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据
- 基于MVC+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
- Android基础总结(3)——UI界面布局
- Confluent介绍(一)
- 基于MVC4+EasyUI的Web开发框架形成之旅--权限控制
- Java break&continue标签的使用