Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 64655   Accepted: 30135
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

  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

Source

题意:就是求区间[l,r]的最大值与最小值之差
首先,介绍一下树状数组,树状数组的复杂度为O(logn),之所以这么快,它是用到了分块优化的思想
,比如:1-5就分为1、1-2、3、1-2-3-4、5;而树状数组最基本实现的就是求前缀和和区间单点更新;在本题中,我们也能效仿这样吗,当然。单点更新极值的话跟之前的一样;重点是求区间的极值,我们不能像求前缀和一样sum[r]-sum[l],那应该怎么办呢?
其实这里还是用到分块优化的思想,但是我们不能直接用Max[r],因为这里表示区间可能超出了我们要求的区间,但是我们可以利用一些包含在[l,r]这里面的块,从而降低复杂度。
假设我们暴力的话,就是从右往左遍历一遍a数组然后取最值,那么在这之中,如果有一些分块在[l,r]之中,就可以直接使用这些块了,就不用去遍历他们,但是遇到有的块超出了[l,r],我们就只能直接比较a[r]了。
代码:
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define mod 1000000007
  7. #define INF 0x3f3f3f3f
  8. #define MAX 50005
  9. int Max[MAX],Min[MAX];
  10. int a[MAX];
  11. int n,q;
  12. inline void read(int &x){
  13. char ch;
  14. bool flag=false;
  15. for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
  16. for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
  17. x=flag?-x:x;
  18. }
  19. inline void write(int x){
  20. static const int maxlen=;
  21. static char s[maxlen];
  22. if (x<) { putchar('-'); x=-x;}
  23. if(!x){ putchar(''); return; }
  24. int len=; for(;x;x/=) s[len++]=x % +'';
  25. for(int i=len-;i>=;--i) putchar(s[i]);
  26. }
  27. int lowbit(int x)
  28. {
  29. return x&-x;
  30. }
  31. void updata(int i,int val)
  32. {
  33. while(i<=n)
  34. {
  35. Min[i]=min(Min[i],val);
  36. Max[i]=max(Max[i],val);
  37. i+=lowbit(i);
  38. }
  39. }
  40. int query(int l,int r)
  41. {
  42. int maxn=a[l],minn=a[r];
  43. while()
  44. {
  45. maxn=max(maxn,a[r]),minn=min(minn,a[r]);
  46. if(l==r) break;
  47. for(r-=;r-l>=lowbit(r);r-=lowbit(r))
  48. maxn=max(Max[r],maxn),minn=min(minn,Min[r]);
  49. }
  50. return maxn-minn;
  51. }
  52. int main()
  53. {
  54. memset(Min,INF,sizeof(Min));
  55. read(n);read(q);
  56. for(int i=;i<=n;i++){
  57. read(a[i]);
  58. updata(i,a[i]);
  59. }
  60. while(q--)
  61. {
  62. int a,b;
  63. read(a),read(b);
  64. write(query(a,b));
  65. putchar('\n');
  66. }
  67. return ;
  68. }

时间复杂度近似为log2(n)。

参考博客:https://www.cnblogs.com/mypride/p/5002556.html

https://blog.csdn.net/u012602144/article/details/52734329

 

poj3264 Balanced Lineup(树状数组)的更多相关文章

  1. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  2. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

  3. 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)

    维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...

  4. [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)

    传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...

  5. [USACO17JAN]Balanced Photo平衡的照片 (树状数组)

    题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...

  6. st表树状数组入门题单

    预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...

  7. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  8. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  9. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

随机推荐

  1. 二、Ajax请求MVC中数据查询表返回datatable

    一.Ajax请求MVC中数据查询表返回datatable 解决方式 返回list

  2. kotlin中实现匿名内部类

    1.常规的方式实现匿名内部类 valueAnimator.addUpdateListener(object :AnimatorUpdateListener { override fun onAnima ...

  3. java类使用

    package java04; /* * 通常情况下,一个类不能直接使用,需要创建一个对象,才能使用 * *步骤: * 1.导包:就是指出需要使用的类在什么位置 * import 包名称.类名称: * ...

  4. Linux --忘记root密码/su: Authentication failure

    如果忘记了root用户的密码,或者su root的时候,提示:su: Authentication failure 那么,可以通过以下的方式来重新设置密码,而后,再尝试,那么就可以顺利su root了 ...

  5. jsp中引入js文件缓存问题解决

    加上版本UUID <script charset="utf-8" src="${basePath}js/souke/soukegalist.js?v=<%=U ...

  6. Linux中检查内存使用情况的命令

    Linux操作系统包含大量工具,所有这些工具都可以帮助您管理系统.从简单的文件和目录工具到非常复杂的安全命令,在Linux上没有太多不能做的事情.而且,虽然普通桌面用户可能不需要在命令行熟悉这些工具, ...

  7. 线程池(ThreadPool)创建

    线程池创建方式jdk1.5 Java通过Executors(jdk1.5并发包)提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活 ...

  8. python的strip和split函数

    这两个函数都是string的类函数 1.strip是去掉字符串头尾的特定字符,分三个 aa=' bb=aa.rstrip(') cc=aa.lstrip(') dd=aa.strip(') print ...

  9. Install packages failed: Installing packages: error occurred. ------简单的问题常常会被忽略

    用 pip install 安装了wxpy这个库,但是使用的时候却报错:ImportError: No module named wxpy 我先用 pip list 查看了一下,发现这个库是已经存在的 ...

  10. Heartbeat安装及配置

    1.yum源安装 yum -y install heartbeat 更新yum源 yum install epel-release -y yum源有问题,改用下载rpm包安装 2.rpm安装 下载rp ...