POJ 3264 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

两种办法:
1.RMQ模板题,AC代码如下:

#include <cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=5e5+5;
int n,q,l,r,dp1[N][50],dp2[N][50],a[N];
void rmq_init()
{for(int i=1;i<=n;i++)dp1[i][0]=dp2[i][0]=a[i];for(int j=1;(1<<j)<=n;j++)for(int i=1;i+(1<<j)-1<=n;i++){dp1[i][j]=min(dp1[i][j-1],dp1[i+(1<<j-1)][j-1]);dp2[i][j]=max(dp2[i][j-1],dp2[i+(1<<j-1)][j-1]);}
}int rmqmin(int l,int r)
{int k=log2(r-l+1);return min(dp1[l][k],dp1[r-(1<<k)+1][k]);
}int rmqmax(int l,int r)
{int k=log2(r-l+1);return max(dp2[l][k],dp2[r-(1<<k)+1][k]);
}int main()
{scanf("%d%d",&n,&q);for(int i=1;i<=n;i++) scanf("%d",&a[i]);rmq_init();while(q--){scanf("%d%d",&l,&r);printf("%d\n",rmqmax(l,r)-rmqmin(l,r));}
}

2.线段树,消耗的内存小一些,时间一样

#include <cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5e4+5;
struct node{int mx,mn;
}tree[N<<2];
int n,q,l,r,mn,mx,a[N];
void pushup(int i)
{tree[i].mx=max(tree[i<<1].mx,tree[(i<<1)+1].mx);tree[i].mn=min(tree[i<<1].mn,tree[(i<<1)+1].mn);
}void build(int i,int l,int r){if(l==r){tree[i].mn=tree[i].mx=a[l];return ;}int mid=l+(r-l)/2;build(i<<1,l,mid);build((i<<1)+1,mid+1,r);pushup(i);
}void query(int i,int l,int r,int x,int y){if(x<=l&&r<=y){mx=max(mx,tree[i].mx);mn=min(mn,tree[i].mn);return ;}int mid=l+(r-l)/2;if(x<=mid) query(i<<1,l,mid,x,y);if(y>mid) query((i<<1)+1,mid+1,r,x,y);
}int main() {scanf("%d%d",&n,&q);for(int i=1;i<=n;i++) scanf("%d",&a[i]);build(1,1,n);while(q--){scanf("%d%d",&l,&r);mn=1e9,mx=-1e9;query(1,1,n,l,r);printf("%d\n",mx-mn);}return 0;
}

POJ 3264 Balanced Lineup相关推荐

  1. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  2. POJ 3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53629   Accepted: 25223 ...

  3. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  4. POJ 3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 24349   Accepted: 11348 ...

  5. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

  6. 【RMQ】POJ 3264 Balanced Lineup

    前言 这题出现在RMQRMQRMQ的WordWordWord文档中,我就直接把以前ACACAC过的程序交上去,结果WAWAWA了,然后才发现这道题还要求最小值... 链接 http://poj.org ...

  7. POJ 3264 Balanced Lineup (RMQ)

    Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...

  8. POJ 3264: Balanced Lineup

    2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:查询区间最大值/最小值之差,最基础的线段树应用. 代码: #include <cstdio>/ ...

  9. POJ 3264.Balanced Lineup-RMQ(ST)详解

    先写一道水题的博客,为后面要写的博客做一个铺垫. ヾ(◍°∇°◍)ノ゙ RMQ(Range Minimum/Maximum Query),即区间最值查询,对于长度为n的数列A,回答若干询问RMQ(A, ...

最新文章

  1. java step1:基础知识5(java中Timer和TimerTask的使用)
  2. python批量分析表格_示例python 批量操作excel统计销售榜品牌及销售额
  3. int** 赋值_关于Java语言复合赋值运算符的两个问题,快来瞧瞧
  4. sqlalchemy filter
  5. [转帖] Windows 与linux的栈大小问题
  6. 第三章:3.4 处理登陆的请求
  7. mysql+workbench+6.1+下载,MySQL Workbench 6.3.1 发布下载
  8. 论文赏析【EMNLP19】多粒度自注意力机制(MG-SA)
  9. 总结篇——从零搭建maven多模块springboot+mybatis项目
  10. 007-JQuery 筛选
  11. ORACLE WebLogic Server 安装部署
  12. 安卓平板python编程软件下载_notepad++下载-notepad++中文版下载v7.6.2 中文增强版-西西软件下载...
  13. 高速钢(HSS)金属切削刀具的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  14. 草图变真人脸?AI:可以,多草都行
  15. 关于U盘病毒(又名Autorun病毒)
  16. W ndows7蓝屏0x00000024,Win7蓝屏代码0x00000024修复方法
  17. GaussDB灾备方案的设计
  18. POI使用word模板文件循环输出行并导出word
  19. WDM音频驱动程序概览
  20. list数据比对与list对象比对

热门文章

  1. torch和torchvision对应版本(最新版,含有torchvision 0.13.0版本)
  2. Linux SPI 子系统(x86平台)
  3. kubernetes安装脚本-非高可用版。一键安装含Master和Node。
  4. flask 网页 javascript 按钮 点击事件
  5. 椭圆曲线ECC倍点运算forJava
  6. 网页引用Font Awesome图标
  7. 基于T5CPU的智能屏产品型号与内核对照表
  8. centos(5) : centos7 使用yum安装mysql并开启远程连接及重置密码
  9. pcie台式网卡无法开热点
  10. 好多大鱼的国风火车站,却有满满的科技感