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. Django+xadmin打造在线教育平台(八)

    十一.用户信息 11.1.个人信息展示 (1)新建‘usercenter-bae.html’当母板 {% load staticfiles %} <!DOCTYPE html> <h ...

  2. JQuery操作option的添加、删除、取值

    1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var checkTex ...

  3. 2017年秋软工-领跑衫获奖感言&我最感谢的人

    啥都不说,先上幅图.获得领跑衫,开心. 一.回忆 这是我第二次来上恩师杨的软件工程,第一次是2016年春,那时候我还是本科三年级的学生.忘了第一次为啥去蹭课,印象中是我的榜样亮哥把我给忽悠过去的?我也 ...

  4. c字符数组

    一.PTA实验作业 题目1:统计一行文本的单词个数 1. 本题PTA提交列表 2. 设计思路 定义一个长度为1000的字符数组str[1000] 在定义 i=0,cnt=0:cnt用来记录单词的个数 ...

  5. alpha-咸鱼冲刺day1-紫仪

    总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展   登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花 ...

  6. scrapy crawl xmlfeed spider

    from scrapy.spiders import XMLFeedSpider from myxml.items import MyxmlItem class XmlspiderSpider(XML ...

  7. JDBC操作数据库的三种方式比较

    JDBC(java Database Connectivity)java数据库连接,是一种用于执行上sql语句的javaAPI,可以为多种关系型数据库提供统一访问接口.我们项目中经常用到的MySQL. ...

  8. BM V7000数据恢复成功案例;服务器数据恢复

    IBM V7000存储是一款定位中端的存储设备,很多企业选择该服务器作为存储,最近北亚数据恢复中心接到一例V7000服务器数据恢复案例,下面将对本次数据恢复的过程和数据恢复方法进行归纳总结,希望对各位 ...

  9. 在windows环境下安装redis和phpredis的扩展

    在windows环境下安装redis和phpredis的扩展 1.首先配置php: 需要在windows的集成环境中找到php的扩展文件夹,ext,然后在网上寻找自己的php对应的.dll文件 比如说 ...

  10. LeetCode & Q119-Pascal's Triangle II-Easy

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...