poj3264_Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 42349 | Accepted: 19917 | |
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
线段树维护区间最大值、最小值,然后相减。AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#include <set>
#include <cmath>
#define inf 0x7fffffff
using namespace std;
const int maxn=;
struct b{
int imax,imin;
}tree[maxn*+];
int n,q,x,y,a[maxn+];
void build(int p,int l,int r){
if(l==r) {tree[p].imax=a[l],tree[p].imin=a[l];return ;}
int mid=(l+r)/;
build(p*,l,mid);
build(p*+,mid+,r);
tree[p].imax=max(tree[p*].imax,tree[p*+].imax);
tree[p].imin=min(tree[p*].imin,tree[p*+].imin);
}
int findmin(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imin;
int mid=(l+r)/;
if(x>mid) return findmin(p*+,mid+,r,x,y);
if(y<=mid) return findmin(p*,l,mid,x,y);
return min(findmin(p*+,mid+,r,x,y),findmin(p*,l,mid,x,y));
}
int findmax(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].imax;
int mid=(l+r)/;
if(x>mid) return findmax(p*+,mid+,r,x,y);
if(y<=mid) return findmax(p*,l,mid,x,y);
return max(findmax(p*+,mid+,r,x,y),findmax(p*,l,mid,x,y));
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%d%d",&x,&y);
printf("%d\n",findmax(,,n,x,y)-findmin(,,n,x,y));
}
return ;
}
poj3264_Balanced Lineup的更多相关文章
- poj-3264-Balanced Lineup
poj 3264 Balanced Lineup link: http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS ...
- 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 ...
- D:Balanced Lineup
总时间限制: 5000ms 内存限制: 65536kB描述For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always lin ...
- 三部曲一(数据结构)-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 ...
随机推荐
- the art of seo(chapter eleven)
Tracking Results and Measuring Success goal -> driver ***Why Measuring Success Is Essential to th ...
- web性能压力测试工具http_load/webbench/ad
http_load 下载地址:http://www.acme.com/software/http_load/http_load-12mar2006.tar.gz 程序非常小,解压后也不到100K 居家 ...
- IDT 信息设计工具使用
IDT 大多使用于接外系统数据,也就是非SAP 系统数据库.虽然SAP BW 里有数仓概念,接外部系统的视图.集中在使用建模,query ,再通过BO来展示.可是有些我们需要直连数据库,取一两张表相对 ...
- nginx的fastcgi_param参数详解
在配置nginx的时候,有很多fastcgi_param参数,具体对应是什么值呢 引入:fastcgi_params 文件: fastcgi_params文件具体内容: postman 发送请求: n ...
- COM组件宏观认识
一直搞不清楚COM到底是个什么东西,记录一些个人感想,可能很多错误的,慢慢消化. 一.宏观认识: 1.COM(组件对象模型)是一种标准,规则,要求,即即于建筑设计指标要求. 2.语言无关性,因为是建立 ...
- 编写html页面时常见的问题(二)
这次我接着说几个编写页面时常见的另外几个问题. 不能水平居中对齐 在我们写页面的时候,会遇到一些小细节就是不能水平居中对齐,这种情况影响了页面的美观,这也是写页面的质量不过关的一个体现,其实这种情况的 ...
- ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)
Description Three wizards are doing a experiment. To avoid from bothering, a special magic is set ar ...
- Godot-3D教程-01.介绍3D
创建一个3D游戏将是个挑战,额外增加的Z坐标将使许多用于2D游戏的通用技术不再有用.为了帮助变换(transition),值得一提的是Godot将使用十分相似的API用于2D和3D. 目前许多节点是公 ...
- bzoj1853幸运数字——容斥原理
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1853 dfs实现容斥原理即可. 注意:若在init中写“cnt++”,则出来后需要先cnt-- ...
- 洛谷P1941飞扬的小鸟——细节DP
题目:https://www.luogu.org/problemnew/show/P1941 此题主要注意许多细节,详见代码. 代码如下: #include<iostream> #incl ...