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语言实现)
总结常见的单链表操作函数,复习使用,仅供参考,代码调试通过. #include<stdio.h> typedef struct node{ int data; struct node *n ...
- python+selenium之断言Assertion
一.断言方法 断言是对自动化测试异常情况的判断. # -*- coding: utf-8 -*- from selenium import webdriver import unittest impo ...
- android 内存泄露测试
Android 程序由java语言编写,android的内存管理与java相似,通过new为对象分配内存,所有对象在java堆内分配空间,对象回收有个垃圾回收器来完成.GC就是垃圾收集的意思(Gaba ...
- [LR]遇到的坑及常用技巧
解决问题是需要智慧的 1.LR内部问题可以查看帮助文档 一般位于LR安装目录的bin目录下,如下图 打开Troubleshooting.chm文件,比如我们跑场景的过程中报错,报错信息为:-27995 ...
- 运行JavaWeb项目报错Access denied for user 'root'@'localhost' (using password: YES)
问题重现:(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000): ...
- flask 快速入门链接
http://docs.jinkan.org/docs/flask/quickstart.html
- SC || Chapter 5 复习向
可复用性 ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 行为子结构 对于父子的继承关系的要求: ·子类可以增加方法,但不可以删 ·子类需实现抽象类型中未实现的方法 ·子类重写(override)的方法必须 ...
- NPM下载模块包说明
博主对npm包安装收集了各种资料和实践后对它们之间的差异整理,写下这篇文章避免自己忘记,同时也给node.js猿友一点指引. 我们在使用 npm install 安装模块的模块的时候 ,一般会使用下面 ...
- tomcat - 自带日志的区分
在tomcat 中,logs文件夹下会存放着一些tomcat自带的日志文件,其中有三种文件: 1 > localhost_access_log.2017-12-28 文件,它用来记录tomcat ...
- [已解决] odoo12 菜单不显示,安装后多出菜单
描述:odoo11中自定义模块写的,除了res.partner,res.users使用odoo自带的.其他的写了一个中国城市l10n_cn_city模型,一个账单模型(继承l10n_cn_city). ...