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 ...
随机推荐
- C++拾遗(五)——类
类是 C++ 中最重要的特征.C++ 语言的早期版本被命名为“带类的 C(Cwith Classes)”,以强调类机制的中心作用.随着语言的演变,创建类的配套支持也在不断增加.语言设计的主要目标也变成 ...
- Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...
- [会员登入] 透过 E-Mail进行身份认证、启用会员权利
[會員登入] 透過 E-Mail進行身份認證.啟用會員權利 这个问题是在论坛上看见的 其实,遇见问题的时候,我会建议初学者先想一下「流程」 您想怎么作?需要哪些步骤? 一旦「流程」清楚了 您是哪一步骤 ...
- MovieReview—NINE LIVES(九条命)
Struggle & Family A successful middle-aged man in the movie became a cat by falling from ...
- Windows 漏洞利用开发
第一阶段:简单栈溢出 分析栈溢出原理 寻找溢出点,了解pattern_create和pattern_offset计算溢出点的原理 寻找JMP ESP跳板,分析利用JMP ESP跳板劫持程序流的原理 编 ...
- appium---常用的adb命令
在测试android-app的时候,adb命令可以帮助我们解决许多问题 什么是adb Android Debug Bridge,我们一般简称为adb,主要存放在sdk安装目录下的platform-to ...
- Spring3中好用的工具类收集
1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...
- python特殊字符转义符号表示
- OAuth认证协议中的HMACSHA1加密算法
<?php function hmacsha1($key,$data) { $blocksize=64; $hashfunc='sha1'; if (strlen($key)>$block ...
- 【mac】【转发】Mac系统升级后,按大小写键没反应了,切换大小写的灯不亮了
Mac系统升级后发现caps lock 锁定大小写的键,失灵了,居然可以用来切换输入法了,经过一排查后, 使用以下几种方法处理: 方式一:长按 caps lock 键,来切换大小写 方式二:caps ...