Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 57259   Accepted: 26831
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

Source

 
  • 区间最值RMQ
  • ST打表
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 5e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int n, m, k, A, B, u, v;
int a[maxn], mx[maxn][], mi[maxn][], fac[];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%d %d",&n,&m)){
for(int i=;i<=n;i++){
scanf("%d",&mx[i][]);
mi[i][]=mx[i][];
}
int k=(int)(log((double)n)/log(2.0));
for(int j=;j<=k;j++)
for(int i=;i+fac[j]-<=n;i++){
mx[i][j]=max(mx[i][j-],mx[i+fac[j-]][j-]);
mi[i][j]=min(mi[i][j-],mi[i+fac[j-]][j-]);
}
while(m--){
scanf("%d %d",&u,&v);
k=(int)(log((double)(v-u+))/log(2.0));
A=max(mx[u][k],mx[v-fac[k]+][k]);
B=min(mi[u][k],mi[v-fac[k]+][k]);
printf("%d\n",A-B);
}
}
return ;
}

POJ_3264_Balanced Lineup的更多相关文章

  1. poj-3264-Balanced Lineup

    poj   3264  Balanced Lineup link: http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS ...

  2. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  3. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  4. D:Balanced Lineup

    总时间限制: 5000ms 内存限制: 65536kB描述For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always lin ...

  5. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  6. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  7. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  8. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John ...

  9. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

随机推荐

  1. struts2防止反复提交的办法

    <? xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC " ...

  2. POJ 3273 Monthly Expense(二分搜索)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  3. 【Nodejs】npm cnpm 淘宝镜像

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...

  4. Ansible的快速入门

    Ansible 是一个简单的自动化引擎,可完成配置管理,应用部署,服务编排等各种IT需求. Ansible使用python语言开发实现的开源软件,依赖于Jinjia2,paramiko和PyYAML这 ...

  5. mybatis 之parameterType="Long"

    <select id="selectByPrimaryKeyByArrayMemberId" resultType="memberModel" param ...

  6. 使用Html和ashx文件实现其简单的注册页面

    记得上一次博客中实现的是其登录页面,其实学会了登录页面,注册页面自然就知道怎么写啦,都是一个意思的,但是今天不知道怎么个情况,写一个注册页面程序 中 一直在出错,大的问题小的问题一直出错,似乎是不在状 ...

  7. Stay hungry, Stay foolish 的原义

    乔布斯在斯坦福大学毕业演讲中说过,他最喜欢的一句话叫做"Stay hungry, Stay foolish". "Stewart和他的人出了好几期<地球产品目录&g ...

  8. gozmq的安装与使用

    1. 安装zmq 下载Windows版安装或linux版本并执行安装命令: tar zxvf zeromq-4.1.6.tar.gz cd zeromq-4.1.6 ./configure make ...

  9. listView优化方案

    1.如果自定义适配器,那么在getView方法中要考虑方法传进来的参数contentView是否为null,如果为null就创建contentView并返回,如果不为null则直接使用.在这个方法中尽 ...

  10. linux Tar 命令参数详解

    tar命令 . 作用 tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户. . 格式 tar [主选项+辅选项] 文件或目录 eg: tar z ...