TOJ1698: Balanced Lineup
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 ≤ A ≤ B ≤ N), 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
输出区间最大最小值之差,RMQ
#include<bits/stdc++.h>
using namespace std;
const int N=;
int dmi[N][],dma[N][],f[N];
int n,q,l,r;
void RMQ_init()
{
for(int j=; (<<j)<=n; j++)
for(int i=; i+j-<=n; i++)
dmi[i][j]=min(dmi[i][j-],dmi[i+(<<(j-))][j-]),dma[i][j]=max(dma[i][j-],dma[i+(<<(j-))][j-]);
}
int RMQ(int l,int r)
{
int k=f[r-l+];
return max(dma[l][k],dma[r-(<<k)+][k])-min(dmi[l][k],dmi[r-(<<k)+][k]);
}
int main()
{
f[]=-;
scanf("%d%d",&n,&q);
for(int i=; i<=n; i++)
scanf("%d",&dma[i][]),dmi[i][]=dma[i][],f[i]=((i&(i-))==)?f[i-]+:f[i-];
RMQ_init();
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",RMQ(l,r));
}
return ;
}
线段树
#include<bits/stdc++.h>
using namespace std;
#define lson l,mi,rt<<1
#define rson mi+1,r,rt<<1|1
struct T
{
int ma,mi,l,r;
}tree[];
int h[];
int ma,mi;
void build(int l,int r,int rt)
{
tree[rt].l=l,tree[rt].r=r;
if(l==r)
{
tree[rt].ma=tree[rt].mi=h[l];
return;
}
int mi=(l+r)>>;
build(lson);
build(rson);
tree[rt].ma=max(tree[rt<<].ma,tree[rt<<|].ma);
tree[rt].mi=min(tree[rt<<].mi,tree[rt<<|].mi);
}
void findma(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].ma>ma)ma=tree[rt].ma;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findma(l,r,rt<<);
else if(mi<l)
findma(l,r,rt<<|);
else
findma(lson),findma(rson);
} void findmi(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
if(tree[rt].mi<mi)mi=tree[rt].mi;
return;
}
int mi=(tree[rt].l+tree[rt].r)>>;
if(mi>=r)
findmi(l,r,rt<<);
else if(mi<l)
findmi(l,r,rt<<|);
else
findmi(lson),findmi(rson);
}
int main()
{
int n,q,i,a,b;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++)
scanf("%d",&h[i]);
build(,n,);
while(q--)
{
ma=;
mi=1e9;
scanf("%d%d",&a,&b);
findma(a,b,),findmi(a,b,);
printf("%d\n",ma-mi);
}
return ;
}
TOJ1698: Balanced Lineup的更多相关文章
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- Balanced Lineup(树状数组 POJ3264)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...
- 三部曲一(数据结构)-1022-Gold Balanced Lineup
Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Othe ...
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- poj3264 - Balanced Lineup(RMQ_ST)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 45243 Accepted: 21240 ...
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup
1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer John ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- POJ3264 Balanced Lineup
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 44720 Accepted: 20995 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
随机推荐
- 进度条插件使用demo
1.下载地址: http://down.htmleaf.com/1502/201502031710.zip 2.效果图: 3.HTML代码:其中80设置当前所占百分比,即蓝色部分比例:注意引入必须的j ...
- servlet forword服务器端跳转
web.xml中配置servlet的映射和访问路径 <?xml version="1.0" encoding="UTF-8"?><web-ap ...
- BZOJ 4563: [Haoi2016]放棋子
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 389 Solved: 248[Submit][Status][Discuss] Descriptio ...
- OpenLayers 3 的 图层控制控件
openlayers3的control中没有提供默认的图层控制控件. 但是git上已经有造好的轮子,直接拿来用就可以了.地址 https://github.com/walkermatt/ol3-lay ...
- 读书笔记2013-2 Linux内核设计与实现A
读书笔记2013-2 Linux内核设计与实现A <Linux内核设计与实现> 简介 这本书不是想Linux源码剖析那样一行行分析Linux源代码的书,而是从Linux历史,Linux哲学 ...
- 数据类型-------JavaScript
之前只是简单的学过JavaScript和JQuery,虽然一般的要求都能完成,但并没有深入,这次是看了一个网站,很详细的教学,想重新认识一下JavaScript和JQuery. 本文摘要:http:/ ...
- Spring学习笔记之Spring概述
概述 Spring是一个java应用最广的开源框架,它是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Deve ...
- ORACLE中RECORD、VARRAY、TABLE、IS REF CURSOR 的使用及实例详解
ORACLE中RECORD.VARRAY.TAB.IS REF CURSOR LE的使用及实例详解 create or replaceprocedure PRO_RECORD_ROW_TAB_EXAM ...
- ES6 -- 模板字符串(反单引号)
1)直接使用变量 // before var str = 'test'; console.log(str + "123"); // now var str = 'test'; co ...
- Linux - 后台运行 ctrl + z , jobs , bg , fg
一.& 最经常被用到 这个用在一个命令的最后,可以把这个命令放到后台执行 二.ctrl + z 可以将一个正在前台执行的命令放到后台,并且暂停三.jobs查看当前有多少在后台运行的命令四.fg ...