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.. NQ+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 思路:ST表板子题,ST[i][j]表示下表从i到i+2^j-1的最值,查询时,已知l与r,长度len=r-l+1,且2^log2(len)>len/2,令k=log2(len),ST[l][k]肯定超过了长度的一半,反向取后侧,r-m+1=2^len,另一侧就是ST[r-2^k+1][k]
const int maxm = 5e4+;

int Max[maxm][], Min[maxm][], N, Q;

int main() {
scanf("%d%d", &N, &Q);
int t, l, r;
for(int i = ; i <= N; ++i) {
scanf("%d", &t);
Max[i][] = Min[i][] = t;
}
for(int k = ; (<<k) <= N; ++k) {
for(int i = ; i+(<<k)- <= N; ++i) {
Max[i][k] = max(Max[i][k-], Max[i+(<<(k-))][k-]);
Min[i][k] = min(Min[i][k-], Min[i+(<<(k-))][k-]);
}
}
for(int i = ; i < Q; ++i) {
scanf("%d%d", &l, &r);
int k = log((double)(r-l+)) / log(2.0);
printf("%d\n", max(Max[l][k],Max[r-(<<k)+][k]) - min(Min[l][k], Min[r-(<<k)+][k]));
}
return ;
}
												

Day6 - H - Balanced Lineup POJ - 3264的更多相关文章

  1. (线段树)Balanced Lineup --POJ --3264

    链接: 对于POJ老是爆,我也是醉了, 链接等等再发吧! http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/G 只 ...

  2. Balanced Lineup POJ - 3264

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  3. G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题

    题意 给出一个序列  每次查询区间的max-min是多少 思路:直接维护max 和min即可  写两个query分别查最大最小值 #include<cstdio> #include< ...

  4. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  5. Gold Balanced Lineup POJ - 3274

    Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...

  6. poj 3264 Balanced Lineup (RMQ)

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

  7. G - Balanced Lineup

    G - Balanced Lineup POJ - 3264 思路:水题,线段树的基本操作即可. #include<cstdio> #include<cstring> #inc ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. Go安装gRPC

    grpc-go的官方安装命令 go get google.golang.org/grpc 无法正常使用. 我们可以用以下的命令替代,达到同样的效果 git clone https://github.c ...

  2. [aac @ ...] more samples than frame size (avcodec_encode_audio2)

    在用FFmpeg对音频进行编码的时候报如下错误: [aac @ 000001cfc2717200] more samples than frame size (avcodec_encode_audio ...

  3. Centos7 nginx 反向代理的配置

    一.正向代理与反向代理 1.正向代理 正向代理往VPN理解 正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说: 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这 ...

  4. C++11并发编程1------并发介绍

    也许有人会觉得多线程和并发难用,复杂,还会让代码出现各种各样的问题,不过,其实它是一个强有力的工具,能让程序充分利用硬件资源,让程序运行得更快. 何谓并发: 两个或更多独立得活动同时发生.计算机中就是 ...

  5. linux查漏补缺-linux命令行安装mysql

    apt安装 sudo apt-get update sudo apt-get install mysql-server root@192:/sys/fs/cgroup# apt-get install ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 安装

    所有平台的 MySQL 下载地址为: MySQL 下载:https://dev.mysql.com/downloads/mysql/ 注意:安装过程我们需要通过开启管理员权限来安装,否则会由于权限不足 ...

  7. hdu 1599 find the mincost route floyd求无向图最小环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  8. QQ企业通--客户端登陆模块设计---知识点

    AutoValidate 枚举  确定控件在失去用户输入焦点时应如何验证其数据. 成员名称 说明 Disable 将不进行隐式验证.设置此值将不会妨碍对 Validate 或 ValidateChil ...

  9. postgresql shell脚本传递参数并执行sql脚本并

    参考: https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line ...

  10. VueCli3 项目结构和具体作用