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. 小明同学喜欢体育锻炼,他常常去操场上跑步。跑道是一个圆形,在本题中,我们认为跑道是一个半径为R的圆形,设圆心的坐标原点(0,0)。小明跑步的起点坐标为(R,0),他沿着圆形跑道跑步,而且一直沿着一个方向跑步。回到家后,他查看了自己的计步器,计步器显示他跑步的总路程为L。小明想知道自己结束跑步时的坐标,但是他忘记自己是沿着顺时针方向还是逆时针方向跑的了。他想知道在这两种情况下的答案分别是多少。

    include "stdafx.h" #include<iostream> #include<vector> #include<string> ...

  2. 下一代Apache Hadoop MapReduce框架的架构

    背景 随着集群规模和负载增加,MapReduce JobTracker在内存消耗,线程模型和扩展性/可靠性/性能方面暴露出了缺点,为此需要对它进行大整修. 需求 当我们对Hadoop MapReduc ...

  3. easyUI中 datagrid 格式化日期

    $('#List').datagrid({ url: '@Url.Action("GetList")', width:SetGridWidthSub(10), methord: ' ...

  4. android 集成QQ互联 (登录,分享)

    参考:http://blog.csdn.net/syz8742874/article/details/39271117 http://blog.csdn.net/woblog/article/deta ...

  5. JavaScript学习3:原型和继承

    原型 我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个对象,它的用途是包括能够由特定类型的全部实例共享的属性和方法.逻辑上能够这么理解:prototype是通过调用构造函数而创 ...

  6. Android—— 4.2 Vold挂载管理_NetlinkManager (四)

    在前文Android-- 4.2 Vold挂载管理_主体构建main (一)中有结构图表示,Vold是kernel与用户层的一个交互管理模块. Android-- 4.2 Vold挂载管理_Volum ...

  7. 修改linux的hostname (修改linux系统的IP和hostname)

    # vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=yourname //在这修改hostnameNISDOMAIN=eng-cn.platform.c ...

  8. mysql date函数相关用法整理(持续更新)

    date_add(now(), INTERVAL 1 day)   增加一天 date_format(d,'%Y-%m-%d %T')   这里的d为datestamp类型,格式化成  yyyy-MM ...

  9. 将QQ登录接口整合到你的网站和如何修改配置

    http://www.phpfensi.com/php/20140727/3998.html 摘要:QQ登录的官方SDK进行了一些修改,使其更加容易的整合到自己的网站上去... 对QQ登录的官方SDK ...

  10. Ruby操作数据库基本步骤

    1.rails g model university name:string 2.model has_many :colleges belongs_to :university has_one :us ...