poj3264 - Balanced Lineup(RMQ_ST)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 45243 | Accepted: 21240 | |
| 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
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.
Output
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
分析 既可以用线段树也可以用ST来做
#include<iostream>
#include<cstdio>
#include<cmath>
#define maxx 50005
using namespace std;
int maxsum[maxx][],minsum[maxx][],n,Q;
int _max(int a,int b)
{
return a>b?a:b;
}
int _min(int a,int b)
{
return a<b?a:b;
}
int main()
{ while(~scanf("%d %d\n",&n,&Q))
{
for(int i=;i<=n;i++)
{
scanf("%d\n",&maxsum[i][]);
minsum[i][]=maxsum[i][];
}
for(int j=;j<=;j++)// 预处理
for(int i=;i<=n;i++) //j 循环在 i 循环外
{
if(i+(<<j)-<=n) // 注意左右区间
{
maxsum[i][j]=_max(maxsum[i][j-],maxsum[i+(<<(j-))][j-]);
minsum[i][j]=_min(minsum[i][j-],minsum[i+(<<(j-))][j-]);
}
}
while(Q--)
{int a,b;
scanf("%d %d\n",&a,&b);
int k=(int)((log((double)(b-a+)))/log(2.0)); //注意要包含端点并转换成double
int maxl=_max(maxsum[a][k],maxsum[b-(<<k)+][k]);//不然有些测试要出问题 比如poj
int minl=_min(minsum[a][k],minsum[b-(<<k)+][k]);
printf("%d\n",maxl-minl);
}
}
return ;
}
poj3264 - Balanced Lineup(RMQ_ST)的更多相关文章
- POJ 3264 Balanced Lineup(RMQ_ST)
题目链接:http://poj.org/problem? id=3264 Description For the daily milking, Farmer John's N cows (1 ≤ N ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- 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)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...
- POJ3264——Balanced Lineup(线段树)
本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...
- kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- 【POJ3264】Balanced Lineup(RMQ)
题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
- [POJ3264]Balanced Lineup(线段树,区间最值差)
题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...
随机推荐
- Archlinux添加MP3播放器
方法1: # pacman -S mplayer From: http://www.linuxdiyf.com/viewarticle.php?id=89207 用mplayer循环播放音乐:mpla ...
- hive查询
查询语句可以直接使用非分析函数的变量的别名 select t1.* from ( select account_id, sum(recharge_money) recharge_money_total ...
- jsp配置项目时出错Deployment failure on Tomcat 6.x. Could not copy all resources to
转自:http://www.2cto.com/kf/201201/116853.html 今天在网上部署项目的时候出现在了问题 tomcat一直部署不上 网上查了一下 原因记下来供大家查看 Deplo ...
- Python自动化 【第六篇】:Python基础-面向对象
目录: 面向过程VS面向对象 面向对象编程介绍 为什么要用面向对象进行开发 面向对象的特性:封装.继承.多态 面向过程 VS 面向对象 面向过程编程(Procedural Programming) ...
- 加密和ssl机制细节
1.1 背景知识 对称加密:加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2 非对称加密:使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方公钥加密来保证 ...
- 8.4 H5知识点总结
HTML简介 HyperText Markup Language 简称为HTML HyperText: 超文本 (文本 + 图片 + 视频 + 音频 + 链接) Markup Language: 标记 ...
- SQL SERVER 批量插入记录
--create function insertData(@count as int,@tsn as bigint,@id as int) --as --begin SET IDENTITY_INSE ...
- Linux驱动框架之framebuffer驱动框架
1.什么是framebuffer? (1)framebuffer帧缓冲(一屏幕数据)(简称fb)是linux内核中虚拟出的一个设备,framebuffer向应用层提供一个统一标准接口的显示设备.帧缓冲 ...
- asp.net GDI+绘制折线
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Python—元组tuple
列表的知识其实就类似于c语言中的数组,可插入.修改.list=[a,b,c,d] 而元组tuple,一旦初始化即不可修改.好处与绝对安全. 定义一个空的元组:t=() 定义只有一个元素的元组:t=(1 ...