博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3264 Balanced Lineup(求区间的最大值与最小值之差)
阅读量:2443 次
发布时间:2019-05-10

本文共 2628 字,大约阅读时间需要 8 分钟。

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 37869   Accepted: 17751
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

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 31734251 54 62 2

Sample Output

630

解题思路:先求最大值,再求最小值

/******************************************************************************* * Author :          jinbao * Email :           dongjinbao913106840144@gmail.com * Last modified :   2015-05-11 20:30 * Filename :        poj3264.cpp * Description :      * *****************************************************************************/#include 
#include
using namespace std;#define min(x,y) x
y?x:yconst int MAX = 50000+1;struct cow{ int Min,Max;}p[4*MAX];int a[MAX];void build(int node,int begin,int end){ if (begin==end){ scanf("%d",&a[begin]); p[node].Min=p[node].Max=a[begin]; } else{ build(node*2,begin,(begin+end)/2); build(node*2+1,(begin+end)/2+1,end); p[node].Min=min(p[node*2].Min,p[node*2+1].Min); p[node].Max=max(p[node*2].Max,p[node*2+1].Max); }}int query(int node,int begin,int end,int left,int right,int flag){ if (end
right){ if (flag==0) return 0xffffff; return -1; } if (begin>=left && end<=right){ if (flag==0) return p[node].Min; return p[node].Max; } int m=query(2*node,begin,(begin+end)/2,left,right,flag); int n=query(2*node+1,(begin+end)/2+1,end,left,right,flag); if (flag==0) return min(m,n); return max(m,n);}int main(){ int n,q,l,r,Min,Max; while (~scanf("%d%d",&n,&q)){ build(1,1,n); while (q--){ scanf("%d%d",&l,&r); int ans = query(1,1,n,l,r,1) - query(1,1,n,l,r,0); printf("%d\n",ans); } } return 0;}

转载地址:http://cbbqb.baihongyu.com/

你可能感兴趣的文章
红帽Linux新系统整合虚拟技术 实现简易操作(转)
查看>>
Linux下/etc/default/boot文件字段说明(转)
查看>>
Linux壁纸系列三十四(转)
查看>>
使用带有Dtrace的FreeBSD(转)
查看>>
Fedora Core 4硬盘安装方法(转)
查看>>
常用的系统状态查询命令(转)
查看>>
『推荐』上G的linux视频教程和电子书FTP下载,速度快内容实用!(转)
查看>>
AIX系统日常管理(转)
查看>>
Fedora Core 6的新特性(转)
查看>>
不得不说 僵尸网络导致垃圾邮件猛增(转)
查看>>
linux网络知识:TCP/IP设置内容(转)
查看>>
GNOME帮助Linux应用于商业桌面环境(转)
查看>>
linux网络知识:与网络设置有关的几个文件(转)
查看>>
Linux文件内容查询命令(转)
查看>>
libc.a 文件恢复(转)
查看>>
SCO UNIX上cpio命令详细用法(转)
查看>>
Linux系统可卸载内核模块完全指南(下)(转)
查看>>
思考-两个大表的关联.txt
查看>>
WIDTH_BUCKET和NTILE函数.txt
查看>>
sql plan baseline(二)
查看>>