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. 蓝桥杯java历年真题及答案整理1~20.md

    蓝桥杯java历年真题及答案整理(闭关一个月,呕心沥血整理出来的) 1 算法是这样的,如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!种.如:给定 A.B.C三个不同的字符,则结果为:A ...

  2. [15单片机] STC15F104W开发入门及模拟串口程序

    STC15F104W开发入门及模拟串口程序 Saturday, 31. March 2018 09:42AM - beautifulzzzz 前言 最近找到一款51内核的SOP8封装的8脚单片机STC ...

  3. The sum of numbers form 0 to n.(20.9.2017)

    #include <stdio.h> int main() { int a,b,sum; printf("输入一个数字: "); scanf("%d" ...

  4. Flask 扩展 Flask-RESTful

    Flask路由可以指定HTTP请求方法,并在请求函数中根据不同的请求方法,执行不同的逻辑.这样实现一个Restful的请求已经相当简单了 Flask还有更简便的方法,就是其Flask-RESTful扩 ...

  5. nyoj水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  6. c#+wpf项目性能优化之OutOfMemoryException解密

    近期,使用c#+wpf开发的软件准备正式投入使用了,使用前进行了大量的测试,测试后发现了一些问题,其中最让人头疼的就是软件的性能问题(稳定性). 这里的稳定性具体表现在机器的cpu占有率和内存使用情况 ...

  7. Apollo单向SSL认证(2)

    一.生成ks和ts 二.连接测试 1.配置 2.测试

  8. OAuth是什么?

    一.OAuth的概念 1.问题的提出 2.应用场景 3.规范演进 二.OAuth的运行原理 1.参与者 访问私有数据需要用户参与(客户.用户.服务提供者) 访问公共数据不需要用户参与(客户.服务提供者 ...

  9. OAuth2.0学习(1-4)授权方式1-授权码模式(authorization code)

    参与者列表: (1) Third-party application:第三方应用程序,又称客户端(client),如:"云冲印".社交应用. (2)HTTP service:HTT ...

  10. Linux安装git和maven的详细过程

    一.使用yum安装git 当前安装环境是centos6.5 由于在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装 yum -y install git 由于centos6 ...