博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3264
阅读量:5114 次
发布时间:2019-06-13

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

基础
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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
/*********************************************************************    author    :   Grant Yuan    time      :   2014.7.26    algorithm :   线段树**********************************************************************/#include 
#include
#include
#include
#include
#define MAX 50003#define INF 999999999using namespace std;int n,m;struct node{int l;int r;int Min;int Max;};int m1,m2;node tree[4*MAX];int h[MAX];void build(int left,int right,int root){ tree[root].l=left; tree[root].r=right; if(left==right){ tree[root].Max=h[left]; tree[root].Min=h[left]; return; } int mid=(left+right)>>1; build(left,mid,root*2); build(mid+1,right,root*2+1); tree[root].Max=max(tree[root*2].Max,tree[root*2+1].Max); tree[root].Min=min(tree[root*2].Min,tree[root*2+1].Min);}void findMax(int left,int right,int root){ if(left<=tree[root].l&&right>=tree[root].r) {m1=max(m1,tree[root].Max); return;} int mid=(tree[root].l+tree[root].r)>>1; if(mid>=right) findMax(left,right,root*2); else if(mid
=tree[root].r) {m2=min(m2,tree[root].Min); return;} int mid=(tree[root].l+tree[root].r)>>1; if(mid>=right) findMin(left,right,root*2); else if(mid
>n>>m;int a,b; for(int i=1;i<=n;i++) scanf("%d",&h[i]); build(1,n,1); for(int i=0;i

转载于:https://www.cnblogs.com/codeyuan/p/4254484.html

你可能感兴趣的文章
CI(-)框架结构
查看>>
SQL Server表和字段说明的增加和更新
查看>>
MediaElement 播放本地视频文件
查看>>
tomcat本地部署war包的方式
查看>>
结对编程项目作业4
查看>>
Struts2拦截器
查看>>
mysql (master/slave)复制原理及配置
查看>>
为什么要使用boost::enable_shared_from_this<T>
查看>>
Hive分区(静态分区+动态分区)
查看>>
YY 6.27.0.0优化版
查看>>
400. Nth Digit(LeetCode)
查看>>
第一周作业
查看>>
学习笔记
查看>>
Django 项目一补充
查看>>
2018-10-11
查看>>
JY游戏开发,案例之 《下到一百层》,欢迎大家品赏。
查看>>
java代码示例(6-2)
查看>>
python爬虫Day1(requests基本使用)
查看>>
.jar文件无法运行的解决方法
查看>>
EF Code First建库 增删改查
查看>>