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 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
随机推荐
- reactnative资源
http://facebook.github.io/react-native/docs/getting-started.html
- 由Cocos2d-x工程入口窥见代理模式
关于设计模式(Design Pattern),自从“四人帮”第一次在<Design Patterns: Elements of Reusable Object-Oriented Software ...
- Windows Azure 配置Active Directory 主机(1)
现在越来越多企业将自己业务系统迁移云端,方便公司日常运维管理.这篇文章将简单介绍一下,从 Windows Azure 虚拟网络上的虚拟机 (VM) 中的 Corp Active Directory 林 ...
- nginx只允许域名访问网址,禁止ip访问
修改nginx配置 文件 在server段里插入如下正则: if ( $host != 'www.baidu.com') { return 403; } 说明:如果访问讨还不是www.baidu.co ...
- 2018.2.14 Java中的哈夫曼编码
概念 哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,哈夫曼编码是可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造 ...
- appium---命令行启动appium
在客户端的appium长时间运行的时候,出产生一些数据.日志有可能会对appium的内存有所增长,严重的会使appium产生崩溃,这个时候就推荐使用通过cmd进行运行appium, 安装前提需要安装N ...
- vue 报错unknown custom element解决方法
原因: 没有引入相关组件导致的 解决办法: 如果组件是按需引入的必须引入你当前用到的组件,否则会报错
- shell脚本,awk取奇数行与偶数行方法。
第一种方法: 第二种方法: 第三种方法:
- UICollectionView实现无限轮播
#import "KGNewsController.h"#import "KGNewsCell.h"#import "KGNews.h"#i ...
- 理解JS闭包的几个小实验
学了JavaScript有一段时间了,但是对闭包还是不太理解,于是怀着心中的疑问做了几个小实验,终于有点明白了. 首先看一下MDN上的定义:闭包是函数和声明该函数的词法环境的组合. 简单来说,闭包是一 ...