博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 3264 Balanced Lineup 线段树
阅读量:3904 次
发布时间:2019-05-23

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

题目:

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 ≤ ABN), 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

思路:

建立两个线段树数组,一个存最大值,一个存最小值,然后建立线段树,这个题目没有更新操作,只有查询操作。

代码如下:

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

你可能感兴趣的文章
win7下 背景色更改为护眼颜色
查看>>
最小二乘法拟合圆公式推导及vc实现
查看>>
Google搜索使用技巧
查看>>
【HTML】网页中嵌入视频
查看>>
日行一善的100种方式
查看>>
pdflatex插入EPS格式图片的两种方法
查看>>
在博客中用latex写公式
查看>>
Windows 7 虚拟串口 VSPD 6
查看>>
Matlab图像处理小结
查看>>
【ROS】Learning tf教程各部分结果
查看>>
ROS资源
查看>>
安装ARBOTIX SIMULATOR
查看>>
Python初学者
查看>>
Python初学者(续1)
查看>>
Python初学者(续2)
查看>>
ROS下实现timed_out_and_back功能
查看>>
四元数与旋转
查看>>
ROS 学习系列 -- RViz中移动机器人来学习 URDF,TF,base_link, map,odom和odom 主题的关系
查看>>
OpenCV图像的轮廓的匹配
查看>>
9个优秀网上免费标签云生成工具
查看>>