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

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
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]了。
代码:
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
#define MAX 50005
int Max[MAX],Min[MAX];
int a[MAX];
int n,q;
inline void read(int &x){
char ch;
bool flag=false;
for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
x=flag?-x:x;
}
inline void write(int x){
static const int maxlen=;
static char s[maxlen];
if (x<) { putchar('-'); x=-x;}
if(!x){ putchar(''); return; }
int len=; for(;x;x/=) s[len++]=x % +'';
for(int i=len-;i>=;--i) putchar(s[i]);
}
int lowbit(int x)
{
return x&-x;
}
void updata(int i,int val)
{
while(i<=n)
{
Min[i]=min(Min[i],val);
Max[i]=max(Max[i],val);
i+=lowbit(i);
}
}
int query(int l,int r)
{
int maxn=a[l],minn=a[r];
while()
{
maxn=max(maxn,a[r]),minn=min(minn,a[r]);
if(l==r) break;
for(r-=;r-l>=lowbit(r);r-=lowbit(r))
maxn=max(Max[r],maxn),minn=min(minn,Min[r]);
}
return maxn-minn;
}
int main()
{
memset(Min,INF,sizeof(Min));
read(n);read(q);
for(int i=;i<=n;i++){
read(a[i]);
updata(i,a[i]);
}
while(q--)
{
int a,b;
read(a),read(b);
write(query(a,b));
putchar('\n');
}
return ;
}

时间复杂度近似为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. axios 利用new FileReader() 下载文件获取返回的错误信息

    this.axios({           method: "post",           url: url,           data: data,           ...

  2. CSS-02 BFC的理解

    两个概念 感觉BFC挺重要的,于是最近查阅网上资料后小结一下,如果有不对的地方还望指正. 先理解两个概念: BOX :盒子模型 Block-Leave Box :块级元素 display属性为bloc ...

  3. Mac版Navicat Premium激活教程

    工具: Navicat Premium12.0.20 安装包 下载注册机工具包 链接:https://pan.baidu.com/s/1NS8gk780ds1Xn-zHrSIzIw  密码:dvke ...

  4. redis的hmset乐观锁的实现

    1.lua脚本(集成实现了乐观锁,hmset ,expire等) local key=KEYS[1]; local oldVerion=tonumber(ARGV[1]); local seconds ...

  5. Machine Learning:机器学习算法

    原文链接:https://riboseyim.github.io/2018/02/10/Machine-Learning-Algorithms/ 摘要 机器学习算法分类:监督学习.半监督学习.无监督学 ...

  6. 记录每个action执行时间

    import org.apache.commons.lang.time.StopWatch; import org.aspectj.lang.JoinPoint; import org.aspectj ...

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

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

  8. SpringBoot---概述

    1.概述 1.1.SpringBoot解决什么问题? 1.1.1.配置---> 自动化配置 1.1.2.依赖---> SpringBoot提供了一系列的Start POM,整合各项功能的时 ...

  9. 【Elasticsearch】Elasticsearch索引的创建、查看及修改

    转: 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/liuxiao723846/art ...

  10. vue 中使用scss

    1.下载 npm install --save-dev sass-loader npm install --save-dev node-sass npm install sass-loader --s ...