Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 43893   Accepted: 20585
Case Time Limit: 2000MS

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

题意:查询一个区间的最大值最小值,输出二者的差

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 50101
#define K 17
using namespace std;
int hige[N],n,q,l,r;
int min_rmq[N][K+1];
int max_rmq[N][K+1];
int ff[(1<<K)+1];
void input()//读入数据
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)
scanf("%d",&hige[i]);
} void pre_chuli()
{
for(int i=1;i<=n;++i)//初始化长度为2^0的情况
{
min_rmq[i][0]=hige[i];
max_rmq[i][0]=hige[i];
}
for(int j=1;j<=K;++j)
for(int i=1;i+(1<<j)-1<=n;++i)//处理min_rmq[][]和max_rmq[][],第一个中括号放开始点的位置,第二个中括号放区间的长度
{
min_rmq[i][j]=min(min_rmq[i][j-1],min_rmq[i+(1<<j-1)][j-1]);
max_rmq[i][j]=max(max_rmq[i][j-1],max_rmq[i+(1<<j-1)][j-1]);
}
memset(ff,-1,sizeof(ff));
ff[0]=0;
for(int i=0;i<=K;++i)
ff[1<<i]=i;
for(int i=1;i<=(1<<K);++i)//处理当长度为i时,最大的2^t
if(ff[i]==-1)
ff[i]=ff[i-1];
} int max_query(int l,int r)//区间最大值
{
int t=ff[r-l+1];//取长度范围内的最大的2^t
return max(max_rmq[l][t],max_rmq[r-(1<<t)+1][t]);
} int min_query(int l,int r)//区间最小值
{
int t=ff[r-l+1];//取长度范围内的最大的2^t
return min(min_rmq[l][t],min_rmq[r-(1<<t)+1][t]);
} int main()
{
input();
pre_chuli();
for(int i=1;i<=q;++i)
{
scanf("%d%d",&l,&r);
printf("%d\n",max_query(l,r)-min_query(l,r));
}
return 0;
}

  

day 1——ST表练习的更多相关文章

  1. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  2. 【BZOJ-2006】超级钢琴 ST表 + 堆 (一类经典问题)

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2473  Solved: 1211[Submit][Statu ...

  3. 【BZOJ-3956】Count ST表 + 单调栈

    3956: Count Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 99[Submit][Status][Discuss] ...

  4. 【BZOJ-4569】萌萌哒 ST表 + 并查集

    4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 459  Solved: 209[Submit][Status] ...

  5. 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分

    4310: 跳蚤 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 180  Solved: 83[Submit][Status][Discuss] De ...

  6. HDU5726 GCD(二分 + ST表)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...

  7. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  8. Bzoj 2006: [NOI2010]超级钢琴 堆,ST表

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2222  Solved: 1082[Submit][Statu ...

  9. ST表poj3264

      /* ST表多次查询区间最小值 设 g[j][i] 表示从第 i 个数到第 i + 2 ^ j - 1 个数之间的最小值 类似DP的说 ans[i][j]=min (ans[i][mid],ans ...

  10. COJ 1003 WZJ的数据结构(三)ST表

    WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...

随机推荐

  1. 极光征文 | 写写文章就能赢 Filco,岂不美滋滋

    由极光社区举办的第二届征文大赛 --「我和极光的那些事儿」又来啦! 在简书平台发布文章并投稿至「我和极光的那些事」专题,只要参与就能 100% 获得京东购物卡,更有机会赢取象征信仰的 Filco 机械 ...

  2. 【Alpha版本】冲刺阶段 - Day5 - 破浪

    今日进展 袁逸灏:解决音乐播放问题以及跳转问题.(5h) 刘伟康:大致检查了测试规范,参考了其他 alpha 阶段的博客.(1h) 刘先润:解决了敌车与障碍物溢出边界的代码问题,给用户车辆增加了火焰喷 ...

  3. 设计模式NO.3

    设计模式NO.3 本次博客内容为第三次设计模式的练习.根据老师的要求完成下列题目: 题目1 某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商 ...

  4. 小草手把手教你 LabVIEW 串口仪器控制——VISA 串口配置

    建议大家按我发帖子的顺序来看,方便大家理解.请不要跳跃式的阅读.很多人现在看书,都跳跃式的看,选择性的看,导致有些细节的部分没有掌握到,然后又因为某个细节耽误很多时间.以上只是个人建议,高手可以略过本 ...

  5. [NOI2015]软件包管理器

    4621 [NOI2015]软件包管理器  题目等级 : 钻石 Diamond   题目描述 Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过 ...

  6. Java如何调取创蓝253短信验证码

    基于创蓝253短信服务平台的Java调用短信接口API package com.bcloud.msg.http; import java.io.ByteArrayOutputStream; impor ...

  7. 记录Yii2代码调试中出现的两个问题(截图展示)

    1.代码会中断执行,不提示错误信息,是由于substr函数第一个参数为数组造成的 2. 谷歌浏览器调试异步调用接口时出现的错误,需在接口返回处进行断点调试 这两个错误比较隐蔽,调试代码时必须认真仔细

  8. 请求方式:request和 get、post、put

    angular 的 http 多了 Request, Headers, Response ,这些都是游览器的"新特性" Fetch API. Fetch API 和以前的 xmlh ...

  9. 复习HTML+CSS(2)

    n  项目符号嵌套编号思路 标签的内容(文本.项目符号.表格.图片等)必须放在最底层标记中. n  图片标记(行内元素,单边标记) l  语法:<img 属性 = "值"&g ...

  10. Python/零起点(一、数字及元组)

    Python/零起点(一.数字及元组) int整型 int()强行转换成整型数据类型 int整型是不可变,且是不可迭代的对象 一.整型数字用二进制位数表示案例: age=7 #设定一个数字赋值给age ...