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

  1. 6 3
  2. 1
  3. 7
  4. 3
  5. 4
  6. 2
  7. 5
  8. 1 5
  9. 4 6
  10. 2 2

Sample Output

  1. 6
  2. 3
  3. 0
  4.  
  5. 线段树维护最大最小,不涉及更改,只用pushup query就可以了
  6.  
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <string>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <stack>
  14. using namespace std;
  15. #define FO freopen("in.txt","r",stdin);
  16. #define rep(i,a,n) for (int i=a;i<n;i++)
  17. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  18. #define pb push_back
  19. #define mp make_pair
  20. #define all(x) (x).begin(),(x).end()
  21. #define fi first
  22. #define se second
  23. #define SZ(x) ((int)(x).size())
  24. #define debug(x) cout << "&&" << x << "&&" << endl;
  25. #define lowbit(x) (x&-x)
  26. #define mem(a,b) memset(a, b, sizeof(a));
  27. typedef vector<int> VI;
  28. typedef long long ll;
  29. typedef pair<int,int> PII;
  30. const ll mod=;
  31. const int inf = 0x3f3f3f3f;
  32. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  33. ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
  34. //head
  35.  
  36. const int maxn=;
  37. int minn[maxn<<],maxx[maxn<<],n,q,a[maxn],maxpos,minpos;
  38.  
  39. void pushup(int rt) {
  40. minn[rt]=min(minn[rt<<],minn[rt<<|]);
  41. maxx[rt]=max(maxx[rt<<],maxx[rt<<|]);
  42. }
  43.  
  44. void build(int rt,int L,int R){
  45. minn[rt]=;
  46. maxx[rt]=;
  47. if(L==R) {
  48. scanf("%d",&a[rt]);
  49. minn[rt]=maxx[rt]=a[rt];
  50. return;
  51. }
  52. int mid=(L+R)>>;
  53. build(rt<<,L,mid);
  54. build(rt<<|,mid+,R);
  55. pushup(rt);
  56. }
  57.  
  58. void query(int rt,int L,int R,int l,int r) {
  59. if(L>=l&&R<=r) {
  60. minpos=min(minpos,minn[rt]);
  61. maxpos=max(maxpos,maxx[rt]);
  62. return;
  63. }
  64. int mid=(L+R)>>;
  65. if(l<=mid) query(rt<<,L,mid,l,r);
  66. if(r>mid) query(rt<<|,mid+,R,l,r);
  67. }
  68.  
  69. int main() {
  70. while(~scanf("%d%d",&n,&q)) {
  71. build(,,n);
  72. int l,r;
  73. while(q--) {
  74. maxpos=-,minpos=inf;
  75. scanf("%d%d",&l,&r);
  76. query(,,n,l,r);
  77. printf("%d\n",l==r?:maxpos-minpos);
  78. }
  79. }
  80. }
  1.  

kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)的更多相关文章

  1. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

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

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

  5. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  6. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  7. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

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

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

  9. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

随机推荐

  1. [MySQL]修改mysql数据库的root密码的方法

    方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass ...

  2. UML 学习[一]

    上了好久软件工程,才开始这门课程中重要部分的学习----uml图. 统一建模语言(UML,英语:Unified Modeling Language)是非专利的第三代建模和规约语言.UML是一种开放的方 ...

  3. python正则以及collections模块

    正则 一.认识模块  什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.p ...

  4. java 多线程系列---JUC原子类(五)之AtomicLongFieldUpdater原子类

    AtomicLongFieldUpdater介绍和函数列表 AtomicLongFieldUpdater可以对指定"类的 'volatile long'类型的成员"进行原子更新.它 ...

  5. java 多线程系列基础篇(七)之线程休眠

    1. sleep()介绍 sleep() 定义在Thread.java中.sleep() 的作用是让当前线程休眠,即当前线程会从“运行状态”进入到“休眠(阻塞)状态”.sleep()会指定休眠时间,线 ...

  6. 使用pip一次升级所有安装的Python包(太牛了)

    import pip from subprocess import call for dist in pip.get_installed_distributions(): call("pip ...

  7. ansible for devops读书笔记第一章

    yum -y install ansible ansible --version mkdir /etc/ansible touch /etc/ansible/hosts [example]   www ...

  8. 【转载】C语言综合实验1—学生信息管理系统

    http://www.cnblogs.com/Anker/archive/2013/05/06/3063436.html 实验题目:学生信息管理系统 实验要求:用户可以选择1-7可以分别进行学生信息的 ...

  9. MVC5网站部署到IIS7

    server 2008R2+IIS7.5下配置不会出现什么问题,这里记录下在server2008+IIS7下的配置 参考了一下:http://www.cnblogs.com/fcu3dx/p/3773 ...

  10. WindowBuilder的安装与简介

    ---------------siwuxie095                             WindowBuilder 直达链接: http://www.eclipse.org/win ...