1636: [Usaco2007 Jan]Balanced Lineup

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 476  Solved: 345
[Submit][Status]

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

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

Sample Input

* 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 Output

6
3
0

HINT

 

Source

题解:
裸RMQ,英语渣哭了QAQ....
代码:
 #include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 50000+100
#define maxm 100000
using namespace std;
int n,m,a[maxn],f[maxn][],g[maxn][];
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void rmq()
{
for(int i=;i<=n;i++)f[i][]=a[i];
for(int i=;i<=floor(log(n)/log());i++)
for(int j=;j<=n-(<<i)+;j++)
f[j][i]=max(f[j][i-],f[j+(<<(i-))][i-]);
for(int i=;i<=n;i++)g[i][]=a[i];
for(int i=;i<=floor(log(n)/log());i++)
for(int j=;j<=n-(<<i)+;j++)
g[j][i]=min(g[j][i-],g[j+(<<(i-))][i-]);
}
int query(int x,int y)
{
int z=floor(log(y-x+)/log());
return max(f[x][z],f[y-(<<z)+][z])-min(g[x][z],g[y-(<<z)+][z]);
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();m=read();
for(int i=;i<=n;i++)a[i]=read();
rmq();
while(m--)
{
int x=read(),y=read();
printf("%d\n",query(x,y));
}
return ;
}

BZOJ1636: [Usaco2007 Jan]Balanced Lineup的更多相关文章

  1. bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*

    bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...

  2. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  3. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  4. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  5. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  6. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  7. [Usaco2007 Jan]Balanced Lineup排队

    [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...

  8. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  9. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

随机推荐

  1. myeclipse配置svn亲测

    1.安装目录更改为myeclipse install 目录:E:\MyEclipse85\MyEclipse 8.5common 目录:           E:\MyEclipse85\Common ...

  2. ubuntu14.04安装samba、vpn

    前言: VPN+SAMBA,能够让你将网上申请到的VPS目录映射成自己电脑的网络驱动器,方便开发. 一.安装samba 来源:http://jingyan.baidu.com/article/00a0 ...

  3. mvc form

    当点击提交按钮后,想在Controll里取到Form里的数据. 必须在控件上设置name属性 例如<input type='text',name='userId'/>, 在controll ...

  4. jQuery注册验证

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. spring事务管理学习

    spring事务管理学习 spring的事务管理和mysql自己的事务之间的区别 参考很好介绍事务异常回滚的文章 MyBatis+Spring 事务管理 spring中的事务回滚例子 这篇文章讲解了@ ...

  6. postgresql sql修改表,表字段

    1.更改表名 alter table 表名 rename to 新表名 2.更改字段名 alter table 表名 rename 字段名 to 新字段名 3.增加列 ALTER TABLE ud_w ...

  7. Echarts使用随笔(2)-Echarts中mapType and data

    本文出处:http://blog.csdn.net/chenxiaodan_danny/article/details/39081071  series : [                {   ...

  8. Java封装的与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前

    public class CalendarCal { /**  * 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前  *   * @param calendar  *       ...

  9. Tomcat设置最佳线程数总结

    最佳线程数: 性能压测的情况下,起初随着用户数的增加,QPS会上升,当到了一定的阀值之后,用户数量增加QPS并不会增加,或者增加不明显,同时请求的响应时间却大幅增加.这个阀值我们认为是最佳线程数. 为 ...

  10. Spring 中各种通知

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...