题目传送门
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

USACO 2007 January Silver
题意:就是求区间[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=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());x=flag?-x:x;
}
inline void write(int x){static const int maxlen=100;static char s[maxlen];if (x<0) {   putchar('-'); x=-x;}if(!x){ putchar('0'); return; }int len=0; for(;x;x/=10) s[len++]=x % 10+'0';for(int i=len-1;i>=0;--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(1){maxn=max(maxn,a[r]),minn=min(minn,a[r]);if(l==r) break;for(r-=1;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=1;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 0;
}

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

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

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

转载于:https://www.cnblogs.com/zhgyki/p/9545316.html

poj3264 Balanced Lineup(树状数组)相关推荐

  1. POJ3264 Balanced Lineup【线段树】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 63040   Accepted: 29405 ...

  2. 8.8线段树和树状数组

    题目链接   http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28619#overview 密码 acmore 还是感觉不怎么会线段树,还是 ...

  3. 洛谷 P5057 [CQOI2006]简单题(树状数组)

    嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...

  4. Color the ball(HDU1556)树状数组

    每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...

  5. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  6. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  7. poj_3067 树状数组

    题目大意 左右两个竖排,左边竖排有N个点,从上到下依次标记为1,2,...N; 右边竖排有M个点,从上到下依次标记为1,2....M.现在从K条直线分别连接左边一个点和右边一个点,求这K条直线的交点个 ...

  8. hdu 1166 敌兵布阵(树状数组)

    题意:区间和 思路:树状数组 #include<iostream> #include<stdio.h> #include<string.h> using names ...

  9. Equalizing Two Strings 冒泡排序or树状数组

    首先考虑排序后相等 如果排序后相等的话就只考虑reverse长度为2的,所以a或者b排序后存在相邻两个字母相等的话就puts YES,n>26也直接puts YES 不然的话就假设c为a,b排完 ...

  10. Hdu 6534 Chika and Friendly Pairs 莫队算法+树状数组

    题目链接 题意求给区间[L,R]中有少对(i,j)满足i<j且abs(a[i]-a[j])<=k. 首先来说暴力的方法就是离散化,然后用树状数组来维护,但是m次询问,m很大,所以说一定会t ...

最新文章

  1. c语言计算字符串的函数是什么,字符函数
  2. 分享Kali Linux 2016.2第46周虚拟机
  3. 《移动项目实践》实验报告——Android数据存储
  4. 【ArcGIS遇上Python】ArcGIS Python按照指定字段批量筛选不同类型的图斑(以土地利用数据为例)
  5. WebSphere社区版,Geronimo1.1八卦和GBean架构
  6. CAD中 OLE不能旋转_工作常备的天正CAD技巧都在这里啦(附教程)
  7. macOS苹果电脑下载m3u8、ts视频
  8. [4G5G专题-81]:流程 - 4G LTE 小区切换流程大全
  9. php0day,Nginx 0day漏洞—却原来是php漏洞
  10. UML结构建模图———复合结构图
  11. Windows10创建工作组、加入工作组、查看工作组包含的所有电脑
  12. ①变量、常量、数据类型解释 ②标识符命原则 ③sizeof使用原则 ④float型科学计数法 ⑤字符转换到ASCII表 ⑥\t 的使用意义【黑马程序员视频】
  13. HTTP和HTTPS、GET和POST
  14. 论文解读:Making Pre-trained Language Models Better Few-shot Learners(LM-BFF)
  15. App推广都有哪些渠道?一张图片让你全看明白!
  16. 【万字长文+100余张图】轻松搞定Unix/Linux环境使用,建议收藏!
  17. keydown与keypress的区别,组合键
  18. 创业公司最应该注重的是什么?
  19. 米大师服务端接入坑记录
  20. web前端性能优化指南

热门文章

  1. 看到一个RISC-V指令集的评论
  2. 管理感悟:一偷懒,必出错
  3. 黎曼ζ 函数中的Γ是否与欧拉B函数中的Γ一样
  4. 管理感悟:深入理解软件
  5. LINUX下载编译Paho-Mqtt-C
  6. 集美大学计算机工程学院 曾勇进,电子政务评估方法AHP 的研究及实现.pdf
  7. camunda 流程执行追踪_从Activiti分裂而来的camunda BPM
  8. python绘制好几个子图_求助,python使用matplotlib画子图颜色,修改多个颜色报错...
  9. mysql连接 xorm_使用go xorm来操作mysql的方法实例
  10. 安卓3d游戏开发引擎_微信小游戏开发怎么选游戏引擎