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

 

poj 3264 Balanced Lineup 题解的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  2. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  3. poj 3264 Balanced Lineup (RMQ)

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

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

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

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

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

  6. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  7. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  8. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

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

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

随机推荐

  1. Android网络开启、关闭整理

    package com.my.device_admin.business; import java.lang.reflect.Method; import android.content.Contex ...

  2. JavaScript: The Evil Parts - 1

    最近在看JavaScript框架设计,在讲解类型判定的时候提到了一些“匪夷所思的情况”,不过没有明说都是什么时候会出现这些情况.自己玩儿了一下,写写随笔吧.不过可能除了我找到的,还有会其他时候会出现这 ...

  3. Expert C Programming 阅读笔记(~CH1)

    P4: 好梗!There is one other convention—sometimes we repeat a key point to emphasize it. In addition, w ...

  4. bzoj 1877 最小费用流

    思路:挺裸的费用流,拆拆点就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...

  5. NOIP2011 D1 T2选择客栈

    上题目: 题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每 ...

  6. disconf-web 分布式配置管理平台

    一.需求 实现分布式配置中心:(1)集中管理外部依赖的服务配置和服务内部配置(2)提供web管理平台进行配置和查询(3)支持服务注册与发现(4)支持客户端拉取配置(5)支持订阅与发布,配置变更主动通知 ...

  7. 【数据结构】Trie树

    数据结构--Trie树 概念 Trie树,又称字典树.前缀树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计 ...

  8. Unity 2D游戏开发教程之游戏精灵的开火状态

    Unity 2D游戏开发教程之游戏精灵的开火状态 精灵的开火状态 “开火”就是发射子弹的意思,在战争类型的电影或者电视剧中,主角们就爱这么说!本节打算为精灵添加发射子弹的能力.因为本游戏在后面会引入敌 ...

  9. 「LOJ6482」LJJ爱数数

    「LOJ6482」LJJ爱数数 解题思路 : 打表发现两个数 \(a, b\) 合法的充要条件是(我不管,我就是打表过的): \[ a + b = \text{gcd}(a, b)^2 \] 设 \( ...

  10. RE:从零开始的AGC被虐(到)生活(不能自理)

    RE:从零开始的AGC被虐(到)生活(不能自理) 「一直注视着你,似近似远,总是触碰不到.」 --来自风平浪静的明天 AtCoder Grand Contest 001 B: Mysterious L ...