Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 33389   Accepted: 15665
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 ≤ 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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; int minv,maxv;
struct node
{
int l,r,maxv,minv;
int mid()
{
return (l+r)/;
}
}nodes[*MS]; void creat(int root,int l,int r)
{
nodes[root].l=l;
nodes[root].r=r;
nodes[root].minv=INF;
nodes[root].maxv=-INF;
if(l==r)
return ;
creat(root<<,l,(l+r)/);
creat(root<<|,(l+r)/+,r);
} void updata(int root,int pos,int value)
{
if(nodes[root].l==nodes[root].r)
{
nodes[root].minv=nodes[root].maxv=value;
return ;
}
nodes[root].minv=min(nodes[root].minv,value);
nodes[root].maxv=max(nodes[root].maxv,value);
if(pos<=nodes[root].mid())
updata(root<<,pos,value);
else
updata(root<<|,pos,value);
} void query(int root,int l,int r) //注意这里的minv,maxv使用的全局变量,
{ //使用引用也可以
if(nodes[root].minv>=minv&&nodes[root].maxv<=maxv)
return ; // 剪枝
if(nodes[root].l>=l&&nodes[root].r<=r)
{
minv=min(minv,nodes[root].minv);
maxv=max(maxv,nodes[root].maxv);
return ;
}
if(l<=nodes[root].mid())
query(root<<,l,r);
if(r>nodes[root].mid())
query(root<<|,l,r);
} int main()
{
int N,Q,x,y;
scanf("%d%d",&N,&Q);
creat(,,N);
for(int i=;i<=N;i++)
{
scanf("%d",&x);
updata(,i,x);
}
while(Q--)
{
scanf("%d%d",&x,&y);
minv=INF;
maxv=-INF;
query(,x,y);
printf("%d\n",maxv-minv);
}
return ;
}

Balanced Lineup(最简单的线段树题目)的更多相关文章

  1. 【ACM/ICPC2013】线段树题目集合(一)

    前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...

  2. 线段树 & 题目

    首先说下我写的线段树吧. 我是按照线段树[完全版]那个人的写法来写的,因为网上大多数题解都是按照他的写法来写. 确实比较飘逸,所以就借用了. 节点大小是maxn是4倍,准确来说是大于maxn的2^x次 ...

  3. 嗯 第二道线段树题目 对左右节点和下标有了更深的理解 hdu1556

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)

    An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  5. 「CQOI2006」简单题 线段树

    「CQOI2006」简单题 线段树 水.区间修改,单点查询.用线段树维护区间\([L,R]\)内的所有\(1\)的个数,懒标记表示为当前区间是否需要反转(相对于区间当前状态),下方标记时懒标记取反即可 ...

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

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

  7. POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53721   Accepted: 25244 ...

  8. 洛谷P5057 [CQOI2006]简单题(线段树)

    题意 题目链接 Sol 紫色的线段树板子题??... #include<iostream> #include<cstdio> #include<cmath> usi ...

  9. poj 3468 A Simple Problem with Integers(原来是一道简单的线段树区间修改用来练练splay)

    题目链接:http://poj.org/problem?id=3468 题解:splay功能比线段树强大当然代价就是有些操作比线段树慢,这题用splay实现的比线段树慢上一倍.线段树用lazy标记差不 ...

随机推荐

  1. [转] Web前端优化之 Cookie篇

    原文链接: http://lunax.info/archives/3095.html Web 前端优化最佳实践第三部分面向 Cookie .目前只有 2 条实践规则. 1. 缩小 Cookie (Re ...

  2. [转] Web前端优化之 内容篇

    原文网址: http://lunax.info/archives/3090.html Yahoo! 的 Exceptional Performance team 在 Web 前端方面作出了卓越的贡献. ...

  3. 多级联动导航栏(top导航)

    http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/ This is a multi-lev ...

  4. 一条scan查询把HBase集群干趴下

    最近在给公司搭建CDH集群,在测试集群性能时,写了一条简单的scan查询语句竟然把hbase集群的所有regionserver干趴下了.这让我云里雾里的飘飘然. 背景介绍 CDH集群,2台主节点.3台 ...

  5. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  6. C#多线程的几种实现方法

    1.最简单的多线程 using System; using System.Threading; namespace ThreadTest1 { public class SimpleThread { ...

  7. HTTP协议状态码详解

    HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了.  如果是做AJAX,REST,网络爬虫, ...

  8. ucGUI例程收藏

    ucGUI 几个重要例程Demo   按钮的定制 #include <stddef.h> #include <string.h> #include "WM.h&quo ...

  9. 从0,1,2...n中统计0,1,2...9各出现了多少次【SWUN1597】

    题目就是说给你一个N.计算一下从0,1,2,3,4,5,,,,,,n-1,n中计算出0,1,2,3,,,,7,8,9分别出现了多少次... #include<cstdio> #includ ...

  10. swfupload用法总结

    <script src="${base}/thirdparty/swfupload/swfupload.js" type="text/javascript" ...