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 ...
随机推荐
- HTML5实现中国象棋游戏(无人能敌)
1. [代码][JavaScript]代码 var AI = AI||{}; AI.historyTable = {}; //历史表 //人工智能初始化AI.init = func ...
- javascript(9)
js中访问函数 p1.abc() p1["abc"]; js中基于对象 == js面向对象
- 分享知识-快乐自己:2017IDEA破解教程
首先 修改host文件: 文件路径:C:\Windows\System32\drivers\etc\hosts 修改:将“0.0.0.0 account.jetbrains.com”追加到hosts文 ...
- GIT的Push和Pull,强制Pull覆盖本地命令
连接命令: git remote add origin + 你Git库的地址 其中,origin是你对这个Git库地址的标识. 一. 把文件从本地上传到库中 第一步:使用命令 git add命令把文件 ...
- 1080 Graduate Admission (30)(30 分)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- QT(3)第一个QT程序
一.创建一个空项目 二.配置 在demo.pro文件中添加配置: greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 添加main.cpp文件 三.编写代码 ...
- 更换mysql数据目录后出现 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法
服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...
- POJ2387(最短路入门)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38556 Accepted ...
- js中变量声明提前
demo1: var a=10; function b(){alert(a); var a=20; alert(a)} b()//undefined 20 因为:js编译器在执行b这个函数时,会把函数 ...
- Rsync的配置与使用
一.介绍 (不想看直接可以跳过) Rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync本来是用以取代rcp的一个工具,它当前由 rsync.samba.org维护 ...