Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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


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
#include"cstdio"
#include"cstring"
#include"algorithm"
#define lson (rt<<1),l,mid
#define rson (rt<<1)|1,mid+1,r
#define gmid (a[rt].l+a[rt].r)>>1;
using namespace std;
const int MAXN=;
struct node{
int l,r;
int maxn,minn;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
scanf("%d",&a[rt].maxn);
a[rt].minn=a[rt].maxn;
return ;
} int mid=gmid;
build(lson);
build(rson);
a[rt].maxn=max(a[rt<<].maxn,a[(rt<<)|].maxn);
a[rt].minn=min(a[rt<<].minn,a[(rt<<)|].minn);
} int query(int rt,int l,int r,int op)
{
if(a[rt].l==l&&a[rt].r==r)
{
if(op==) return a[rt].maxn;
else return a[rt].minn;
} int mid=gmid; if(r<=mid) return query(rt<<,l,r,op);
else if(mid<l) return query((rt<<)|,l,r,op);
else{
if(op==) return max(query(rt<<,l,mid,op),query((rt<<)|,mid+,r,op));
else return min(query(rt<<,l,mid,op),query((rt<<)|,mid+,r,op));
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,,n);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
int diff=query(,l,r,)-query(,l,r,);//1表示求最大值,2表示求最小值
printf("%d\n",diff);
}
} }

POJ3264(线段树入门题)的更多相关文章

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

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

  2. hdu 1166敌兵布阵(线段树入门题)

    >>点击进入原题测试<< 思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记.入门题 cin,cout会超时,记得加std::ios::sync_wit ...

  3. [poj2104]可持久化线段树入门题(主席树)

    解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...

  4. hiho1079 - 数据结构 线段树(入门题,离散化)

    题目链接 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,但是贴来贴去 ...

  5. Mosaic HDU 4819 二维线段树入门题

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  6. A Simple Problem with Integers(线段树入门题)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. HDU1698(线段树入门题)

    Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  8. hiho1080 - 数据结构 线段树(入门题,两个lazy tag)

    题目链接 维护区间和,两个操作:一个是将某个区间设置成一个值,一个是将某个区间增加一个固定值 /**************************************************** ...

  9. Balanced Lineup poj3264 线段树

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

随机推荐

  1. 有关SQL注入的知识

    SQL注入攻击是非常令人讨厌的安全漏洞,是所有的web开发人员,不管是什么平台,技术,还是数据层,需要确信他们理解和防止的东西.不幸的是,开发人员往往不集中花点时间在这上面,以至他们的应用,更糟糕的是 ...

  2. 身份证归属地查询免费api接口代码

    描写叙述 :依据身份证编号 查询归属地信息. 身份证实体类: package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @versio ...

  3. JavaScript读书笔记(6)-Array RegExp

    1.Array类型 ECMAScript数组的每一项可以保存任何类型的数据,数组的大小也可以动态调整: (1) 创建数组 第一种方式:Array构造函数 var colors=new Array(); ...

  4. lucene分词器中的Analyzer,TokenStream, Tokenizer, TokenFilter

    分词器的核心类: Analyzer:分词器 TokenStream: 分词器做优点理之后得到的一个流.这个流中存储了分词的各种信息,能够通过TokenStream有效的获取到分词单元. 下面是把文件流 ...

  5. Docker入门系列8

    commit docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser ...

  6. WPF实现ScrollViewer滚动到指定控件处

    在前端 UI 开发中,有时,我们会遇到这样的需求:在一个 ScrollViewer 中有很多内容,而我们需要实现在执行某个操作后能够定位到其中指定的控件处:这很像在 HTML 页面中点击一个链接后定位 ...

  7. HTML5,不仅仅是看上去非常美(第二弹:打造最美3D机房)

    前言 近期项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,曾经一直搞java,对js的一些语言特性和概念一时还转换只是来. 上一篇第一 ...

  8. HashMap与 HashTable, Treemap的区别

    (一)HashMap 1.HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null; 2.HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数 ...

  9. 【BZOJ3721】PA2014 Final Bazarek 贪心

    [BZOJ3721]PA2014 Final Bazarek Description 有n件商品,选出其中的k个,要求它们的总价为奇数,求最大可能的总价. Input 第一行一个整数n(1<=n ...

  10. 之前博客中的代码都放到github上

    之前一直把代码托管在taocode上,现在已经不能用了,所以把代码整理了一下,统一都放在gibhub上了. LALR(1)语法分析生成器:https://github.com/kiven-li/xby ...