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. Configuring Robolectric

    There are numerous ways to customize how Robolectric behaves at runtime. Config Annotation The prima ...

  2. My way to Python - Day05 - 面向对象-思维导图

    My way to Python - Day05 - 面向对象   思维导图

  3. win 10 安装 mysql解压版 步骤

    参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...

  4. n进制转为十进制

    主程序代码 - #include <stdio.h> #include <string.h> main() { long t1; int i, n, t, t3; ]; pri ...

  5. Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)

    // VacationRequest.java /** * author : 冯孟活 ^_^ * dates : 2015年9月1日 下午10:32:58 * class : 演示简单的公司请假流程 ...

  6. win2008 64位下.net 无法访问oracle

    这两天换了台新机子,就想弄个新系统win2008 64bit来测试下,也尝尝新鲜,结果是碰的头破血流啊,哈哈就像挖宝似的 环境:win2008 64bit + IIS7+.net2.0 +ORACLE ...

  7. JAVA除去制定字符的方法

    只需调用replaceAll()方法: public class Test { public static void main(String[] args) { String s= "abc ...

  8. 新装的mysql,直接安装板

    Windows安装MySQL解压版 http://www.cnblogs.com/xiaoit/p/3932241.html my文件 如下: [mysql]# 设置mysql客户端默认字符集defa ...

  9. 读懂IL代码(一)

    以前刚开始学C#的时候,总有高手跟我说,去了解一下IL代码吧,看懂了你能更加清楚的知道你写出来的代码是如何运行互相调用的,可是那时候没去看,后来补的,其实感觉也不晚.刚开始看IL代码的时候,感觉非常吃 ...

  10. java SWT/Rap 计算器版本2(键盘鼠标兼容)

    package cn.lesaas.nof.rwtswt.ui.dialog; import java.math.BigDecimal; import org.eclipse.swt.SWT;impo ...