Balanced Lineup(ST)
描述
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.
输入
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.
输出
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.
样例输入
6 3
1
7
3
4
2
5
1 5
4 6
2 2
样例输出
6
3
0
题目大意:
给定一个序列,每次查询一个区间,求这个区间最大值与最小值的差。
#include <bits/stdc++.h>
using namespace std;
const int N=5e4+;
const int INF=0x3f3f3f3f;
int st1[N][],st2[N][],a[N];///st1[i][j]代表以第i个开始长度为2^j的区间内的最大值
int query(int l,int r)
{
int k=log2(r-l+);///需要查询的区间长度
return max(st1[l][k],st1[r-(<<k)+][k])-min(st2[l][k],st2[r-(<<k)+][k]);///[l][k]以l开始,[r-(1<<k)+1][k]到r结束
}
int main()
{
int n,m;
memset(st2,INF,sizeof st2);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
st1[i][]=st2[i][]=a[i];
for(int j=;j<;j++)
for(int i=;i+(<<(j-))<=n;i++)
st1[i][j]=max(st1[i][j-],st1[i+(<<(j-))][j-]),st2[i][j]=min(st2[i][j-],st2[i+(<<(j-))][j-]);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r));
}
return ;
}
Balanced Lineup(ST)的更多相关文章
- 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...
- Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)
传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...
- POJ 3264 Balanced Lineup(ST模板)
链接:http://poj.org/problem?id=3264 题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问 思路:ST表模板,预处理区间最值,O(1)复杂度询问 AC代码: # ...
- poj3264 - Balanced Lineup(RMQ_ST)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 45243 Accepted: 21240 ...
- Balanced Lineup(RMQ)
原题传送门 就是裸RMQ啊.. 求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以. 至于算法嘛.自己学~(好吧,放个传送门...) 然后就是最后把maxsum-minsum就好啦233~ ...
- POJ 题目3264 Balanced Lineup(RMQ)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 39046 Accepted: 18291 ...
- POJ 3264 Balanced Lineup(RMQ_ST)
题目链接:http://poj.org/problem? id=3264 Description For the daily milking, Farmer John's N cows (1 ≤ N ...
- POJ 3264 Balanced Lineup(RMQ)
点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...
- 【POJ3264】Balanced Lineup(RMQ)
题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
随机推荐
- 【extjs6学习笔记】1.7 初始:加载第三方库
https://www.sencha.com/blog/integrating-ext-js-with-3rd-party-libraries-2/ Introduction Ext JS provi ...
- 101个MySQL的调节和优化技巧
MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些思 ...
- [神经网络]一步一步使用Mobile-Net完成视觉识别(二)
1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第二篇,调用官方例子并获取数据集. 上一节里面记得我们需要配置PYTHONPATH,大家应该发现,每次 ...
- javascript报错:ReferenceError: $ is not defined解决办法
原因很简单,要么是未导入jquery包,要么是导入的顺序不对. 例如,我在制作Chrome扩展程序时,其中的一块代码如下: 然后运行时报上述错误. 解决方法:我们不难发现script位置有问题,因为$ ...
- DaemonSet 典型应用场景【转】
Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本.DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本. DaemonS ...
- LeetCode 53题 最大子序和 -- JavaScript
解题思路分析: 该题是在一个整数数组中找到一个和最大的连续子数组,并返回和值.那么如何找到一个和最大的连续子数组呢?我们知道,这肯定需要遍历数组才行:好,那我们就开始遍历数组.首先,我们初始化最大和 ...
- java基础—注解annotation
一.认识注解 注解(Annotation)很重要,未来的开发模式都是基于注解的,JPA是基于注解的,Spring2.5以上都是基于注解的,Hibernate3.x以后也是基于注解的,现在的Struts ...
- 【转】MFC 程序入口和执行流程
一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...
- 【状态压缩dp】1195: [HNOI2006]最短母串
一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...
- 【Git版本控制】GitHub上fork项目和clone项目的区别
fork:在github页面,点击fork按钮,将别人的仓库复制一份到自己的仓库. clone:直接将github中的仓库克隆到自己本地电脑中 问题1:pull request的作用 比如在仓库的主人 ...