Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 52328   Accepted: 24551
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 maxn 1000000 using namespace std;
int n,m,ans,x,y,a[maxn],p[maxn];
int f1[maxn][],f2[maxn][]; inline int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;} void ST()
{
for(int i=;i<=n;i++)
f1[i][]=f2[i][]=a[i];
for(int j=;j<=;j++)
for(int i=;i+(<<j)-<=n;i++)
{
f1[i][j]=min(f1[i][j-],f1[i+(<<j-)][j-]);
f2[i][j]=max(f2[i][j-],f2[i+(<<j-)][j-]);
}
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
if((<<j)>i)
{
p[i]=j-;
break;
}
} int query(int l,int r)
{
int k=p[r-l+];
int ans1=min(f1[l][k],f1[r-(<<k)+][k]);
int ans2=max(f2[l][k],f2[r-(<<k)+][k]);
return ans2-ans1;
} int main()
{
n=init();m=init();
for(int i=;i<=n;i++)
a[i]=init();
ST();
for(int i=;i<=m;i++)
{
x=init();y=init();
printf("%d\n",query(x,y));
}
return ;
}

poj3264Balanced Lineup(倍增ST表)的更多相关文章

  1. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

  2. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  3. 浅谈 倍增/ST表

    命题描述 给定一个长度为 \(n\) 的序列,\(m\) 次询问区间最大值 分析 上面的问题肯定可以暴力对吧. 但暴力肯定不是最优对吧,所以我们直接就不考虑了... 于是引入:倍增 首先,倍增是个什么 ...

  4. P7599-[APIO2021]雨林跳跃【二分,倍增,ST表】

    正题 题目链接:https://www.luogu.com.cn/problem/P7599 题目大意 \(n\)棵树,在某棵树上时可以选择向左右两边第一棵比它高的树跳,现在\(q\)次询问从\([A ...

  5. Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)

    传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...

  6. POJ3264:Balanced Lineup——题解+st表解释

    我早期在csdn的博客之一,正好复习st表就拿过来.http://write.blog.csdn.net/mdeditor#!postId=63713810 这道题其实本身不难(前提是你得掌握线段树或 ...

  7. 【BZOJ1047】[HAOI2007]理想的正方形 (倍增ST表)

    [HAOI2007]理想的正方形 题目描述 有一个\(a*b\)的整数组成的矩阵,现请你从中找出一个\(n*n\)的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: ...

  8. 洛谷 P2880 [USACO07JAN]Balanced Lineup G (ST表模板)

    题意:给你一组数,询问\(q\)次,问所给区间内的最大值和最小值的差. 题解:经典RMQ问题,用st表维护两个数组分别记录最大值和最小值然后直接查询输出就好了 代码: int n,q; int a[N ...

  9. poj 3264 倍增 ST表

    #include<iostream> #include<cmath> using namespace std; ; int a[maxn]; ]; ]; int quick(i ...

随机推荐

  1. 记一些关于acm的小知识(自用,粗糙,勿点呀)

    #define INF 0x7ffffff,定义一个很大的数

  2. [POJ1155]TELE(树形背包dp)

    看到这道题的第一眼我把题目看成了TLE 哦那不是重点 这道题是树形背包dp的经典例题 题目描述(大概的): 给你一棵树,每条边有一个cost,每个叶节点有一个earn 要求在earn的和大于等于cos ...

  3. Springboot2.0中jpa默认创建的mysql表为myisam引擎问题

    使用Springboot2.0后,使用jpa操作mysql数据库时,默认创建的表的引擎是myisam,myisam是不能加外键的,找了一些资源,最终可以用此方法解决! yml格式: spring: j ...

  4. TestNG参数化测试

    参数化有两种方法: 第一种:在xml文件中声明 第二种:用@DataProvider注解 先介绍第一种方法: ParameterTest类:用@Parameters({"name" ...

  5. tomcat7解决jsp参数传递的中文乱码问题

    解决jsp参数传递的中文乱码问题 制作人:全心全意 在jsp页面中,通过参数传递传递中文时,在显示参数值时中文内容变成了乱码.这是因为请求参数的文字编码方式与页面中的不一致造成的,所有的request ...

  6. 微信小程序支付全问题解决

    这几天在做小程序的支付,没有用官方的SDK,这里就纯用官方的文档搞一发. * 注作者使用的PHP,不过支付流程都是这样 开发前必读 主要流程 小程序前端发送求参请求 接受请求封装 "统一下单 ...

  7. Datatable 导出到execl 官网demo

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  8. [bzoj4027][HEOI2015][兔子与樱花] (树形dp思想+玄学贪心)

    Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接 ...

  9. pyhthon第一个小脚本——文件备份

    先说说这个脚本的作用:对指定路径的文件进行压缩备份到另一个指定的路径,并且压缩文件的文件名用当时的日期+时间命名. 先是对着<简明Python教程>上的代码敲的,一堆错误,书上给的是lin ...

  10. Linux - 模块编程初试

    计算机网络的课程设计要做防火墙,老师没有限制在什么系统上面做,所以决定在Linux上实现.找了一下相关的资料,发现其实Linux有提供Netfilter/Iptables,为用户提供防火墙的功能,稍微 ...