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 ...
随机推荐
- CSS3 初步学习
CSS3有一些是与旧版CSS2.1重叠的,有一些是没有浏览器支持的,全学没必要,下面只记录一下有用的. 一.CSS3边框 1.圆角border-radius border-radius:值越大,角越圆 ...
- Study Emgu VoteForUniqueness
Recently i was studying Emgu, but find there is a bug in the VoteForUniqueness function in class Fea ...
- COleChangeSourceDialog不能Change Source的解决方法
在微软给的例子OClient中,有选中一个OLE对象然后Change Source的功能,但是会报错.分析了一下是这样的: void CMainView::OnOleChangeSource() ...
- linq 对Sum()函数的支持
首先看一段SQL语句: SELECT SUM(TASKAPPR) AS APPRCOUNT, SUM(TASKLOCKED) AS LOCKEDCOUNT, SUM(TASKCHECKED) AS C ...
- 控件的相对位置与绝对位置-UI界面编辑器(SkinStudio)教程
绝对位置: 相对位置: 相对位置配合Anchor属性使用 例如Anchor属性选择left|top(即相对位置的left和top保持不变) 窗口大小改变前: 窗口大小改变后: 可以看到控件相对位置的l ...
- [转载] 2. JebAPI 之 jeb.api.dex
本文转载自: https://www.zybuluo.com/oro-oro/note/142842 1. jeb.api.dex.Dex 这个类代表正在被JEB处理的DEX文件. 要想更好的了解这个 ...
- C#控件根据窗体改变大小
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- vim--golang代码补全
我想说,我折腾了很久编辑器,试了九种办法 最后我只成功了一种 但我依然想就我混乱的逻辑做下整理 一.一开始,我试图入手ipad编码软件,大概9美金吧,叫Textastic.我试图用它的近亲来试验Tex ...
- 新一代IDE Light Table开源:让编程工作更简单
近日,Light Table项目创始人Chris Granger在其博客上宣布Light Table开源,将代码全部托管在GitHub上,遵循GNU开源许可.与此同时,还发布了0.6版本,该版本添加了 ...
- python--自动删除文件
1.目的:定期自定删除7天前的数据 python脚本如下: #coding=utf-8 import os,time,datetime #需定时删除的目录的上一层路径 data_dir="/ ...