Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 44121   Accepted: 20715
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 ≤ ABN), 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
好久没写,写搓了,orz
代码:
#include <iostream>
#include <cstdio>
#include  <cstring>
#include  <algorithm>
using namespace std;
const int maxn=70000;
int a[maxn];
int x,y;
struct nod
{
    int mi;
    int ma;
};
nod tree[4*maxn];
void build(int p,int l,int r)
{
    if(l==r) {tree[p].mi=a[l];tree[p].ma=a[l];return;}
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build((p<<1)+1,mid+1,r);
    tree[p].mi=min(tree[p<<1].mi,tree[(p<<1)+1].mi);
    tree[p].ma=max(tree[p<<1].ma,tree[(p<<1)+1].ma);
}
int find1(int p,int l,int r,int x,int y)
{
    if(x<=l&&r<=y) {return tree[p].ma;}
    int mid=(l+r)>>1;
    if(y<=mid) return find1(p<<1,l,mid,x,y);
    else if(x>mid) return find1((p<<1)+1,mid+1,r,x,y);
    else return max(find1(p<<1,l,mid,x,mid),find1((p<<1)+1,mid+1,r,mid+1,y));
}
int find2(int p,int l,int r,int x,int y)
{
    if(x<=l&&r<=y) {return tree[p].mi;}
    int mid=(l+r)>>1;
    if(y<=mid) return find2(p<<1,l,mid,x,y);
    else if(x>mid) return find2((p<<1)+1,mid+1,r,x,y);
    else return min(find2(p<<1,l,mid,x,mid),find2((p<<1)+1,mid+1,r,mid+1,y));
}
int main()
{
    int n,q;
    int x,y;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        while(q--)
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",find1(1,1,n,x,y)-find2(1,1,n,x,y));
        }
    }
    return 0;
}

poj3264 线段树的更多相关文章

  1. Balanced Lineup poj3264 线段树

    Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...

  2. POJ3264线段树求最值

    刚开始还觉得有点怪怪的.因为想着如果每个树只是单纯地记录它所在的区间的话会不会有不在区间内的数据给更新了,但是我好像是傻掉了因为如果有这种情况出现的话在父亲节点就会分成l,mid和mid+1,r两个区 ...

  3. POJ3264(线段树入门题)

    Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     ...

  4. poj3264(简单线段树)

    题目链接:https://vjudge.net/problem/POJ-3264 题意:线段树简单应用题,区间查询最大值和最小值的差. 思路:用线段树维护区间的最大值和最小值即可. AC代码: #in ...

  5. 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40687   Accepted: 19137 ...

  6. [POJ3264]Balanced Lineup(线段树,区间最值差)

    题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...

  7. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  8. poj3264(线段树区间求最值)

    题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...

  9. POJ3264(线段树求区间最大最小值)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 41162   Accepted: 19327 ...

随机推荐

  1. 最近公共祖先:LCA及其用倍增实现 +POJ1986

    Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...

  2. sql_autoload_register() 函数 和__autoload() 的区别

    1:__autoload($class) 因为是一个函数,所以只能定义一次,使用多个会冲突报错;而 sql_autoload_register('function') 可定义多个,它有效地创建一个队列 ...

  3. angularjs——工具方法

    1.fromJson 把json字符串转成JSON对象 var jsonStr='[{"Name":"abc","age":12},{&qu ...

  4. 3月22日 html(三)css样式表

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控制精确,但是可重用性差,冗余较多. 例 ...

  5. unexpected problem

    一个比较有趣的字符串问题,问题描述如下 大体意思就是给定一个字符串s以及一个整数m,找出一个能满足以上三个条件的字符串t的个数对10e9 + 7 取余输出. 第二三条是关键,t.s = s.t 举个例 ...

  6. iOS之UISearchBar实时显示结果

    iOS之UISearchBar实时显示结果     UISearchBar 经常是配合UITableView 一起使用的,一般都将UITableView的tableHeaderView属性设置为UIS ...

  7. android 反编译操作

    1:首先将apk程序解压: 2:将classes.dex反编译为jar包:$ ./dex2jar/dex2jar.sh classes.dex 将生成一个新的文件classes_dex2jar.jar ...

  8. Powershell创建数组

    在Powershell中创建数组可以使用逗号. PS C:Powershell> $nums=2,0,1,2 PS C:Powershell> $nums 2 0 1 2 对于连续的数字数 ...

  9. C/C++捕获段错误,打印出错的具体位置(精确到哪一行)

    修订:2013-02-16 其实还可以使用 glibc 的 backtrace_symbols 函数,把栈帧各返回地址里面的数字地址翻译成符号描述的 修订:2011-06-11 背景知识: · 在li ...

  10. JVM基础和调优(一)

    最近的项目中,出现了内存和性能的问题,需要优化,所以趁着这个机会,把自己关于java虚拟机的东整理一下,不对的地方,欢迎指出. 数据类型,因为在java的优化的过程中,检测到的数据类型一般比较的基础, ...