Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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

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

——————————————————————我是分割线——————————————————————————————————
【解法】
  很容易想到用线段树解,但是代码复杂度太高,懒得打了。
  所以就用刚学的ST算法解决。
【代码】
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#define maxn 100001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
#define maxm 21
using namespace std;
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;
}
int fm[maxn][maxm],fi[maxn][maxm],p[maxn];
int n,q;
inline int init()
{
cin>>n>>q;
F(i,,n){
cin>>p[i];
}
F(i,,n){
fm[i][]=fi[i][]=p[i];
}
int m=floor((int)(log10((double)n)/log10((double))));
F(j,,m)F(i,,n){
fm[i][j]=max(fm[i+(<<(j-))][j-],fm[i][j-]);
fi[i][j]=min(fi[i+(<<(j-))][j-],fi[i][j-]);
}
}
inline int stmax(int a,int b)
{
int m=floor((int)(log10((double)(b-a+))/log10((double))));
return max(fm[a][m],fm[b-(<<m)+][m]);
}
inline int stmin(int a,int b)
{
int m=floor((int)(log10((double)(b-a+))/log10((double))));
return min(fi[a][m],fi[b-(<<m)+][m]);
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
init();int c;
while(q--)
{
int a,b;
cin>>a>>b;
if(a>b) swap(a,b);
c=stmax(a,b)-stmin(a,b);
cout<<c<<endl;
}
return ;
}

poj 3264

 

最新文章

  1. DOS下windows系统查看wifi密码
  2. jQuery瀑布流插件——jQuery.Waterfall
  3. SQLSTATE[HY000] [2003] Cant connect to MySQL server
  4. 关于Fragment 不响应onActivityResult的情况分析 (
  5. Android——学习:线性布局权重分配
  6. css link和@import区别
  7. jsLint配置参数解释
  8. Codeforces Round #290 (Div. 2) C. Fox And Names dfs
  9. UVALive 4128 Steam Roller(最短路(拆点,多状态))
  10. C# DateTime
  11. SPSS相关和回归分析
  12. 在VC++中启用内存泄露检测
  13. PHP:小数位计算
  14. [原创]Nexus5 源码下载、编译、真机烧录过程记录
  15. springboot 入门一 hello world!
  16. javascript 面向对象设计之 Function 普通类
  17. JAVA之JDBC的简单使用(Mysql)
  18. mysql 客户端连接报错Illegal mix of collations for operation
  19. filter的知识点 和 实例
  20. Android ImageView 的scaleType 属性图解

热门文章

  1. JavaScript: The Evil Parts - 1
  2. IEEEXtreme 10.0 - Painter&#39;s Dilemma
  3. 微信小程序之wepy自动化架构搭建(fly+wepy-plugin-replace)
  4. 在kubernetes运行一个容器案例
  5. Java 单例模式的七种写法
  6. RecyclerView悬浮标题
  7. python log的处理方式
  8. Jquery操作select标签的常用方法
  9. hibernate4日志配置
  10. Circular dependencies cannot exist in RelativeLayout