Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 53868   Accepted: 25299
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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0 题意 求区间最大值、最小值差
此题可用线段树求解 注意,数组开足够大,输入输出用scanf printf 用cin,cout会超时,没办法本人太菜,不会用其他牛X算法QAQ AC代码
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAX 200000
using namespace std;
typedef long long ll;
struct node
{
int l,r;
int Max,Min;
} tree[MAX];
int a[MAX];
int qmax,qmin;
void build(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
if(l==r)
{
tree[i].Max=tree[i].Min=a[l];
return;
}
int mid=(r+l)>>;
build(i<<,l,mid);
build(i<<|,mid+,r);
tree[i].Max=max(tree[i<<].Max,tree[i<<|].Max);
tree[i].Min=min(tree[i<<].Min,tree[(i<<)|].Min);
}
void query(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
{
qmax=max(qmax,tree[i].Max);
qmin=min(qmin,tree[i].Min);
return;
}
int mid=(tree[i].l+tree[i].r)>>;
if(r<=mid)
query(i<<,l,r);
else if(l>mid)
query(i<<|,l,r);
else
{
query(i<<,l,mid);
query(i<<|,mid+,r);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
build(,,n);
while(m--)
{
int r,l;
scanf("%d%d",&l,&r);
qmax=-INF,qmin=INF;
query(,l,r);
printf("%d\n",qmax-qmin);
}
}
return ;
}

2017ecjtu-summer training #3 POJ3264的更多相关文章

  1. hdu 4946 2014 Multi-University Training Contest 8

    Area of Mushroom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 2016 Multi-University Training Contests

    2016 Multi-University Training Contest 1 2016 Multi-University Training Contest 2 2016 Multi-Univers ...

  3. 2016 Multi-University Training Contest 2 D. Differencia

    Differencia Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. 2016 Multi-University Training Contest 1 G. Rigid Frameworks

    Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  6. The Solution of UESTC 2016 Summer Training #1 Div.2 Problem C

    Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/C Description standard input/output After ...

  7. 2012 Multi-University Training Contest 9 / hdu4389

    2012 Multi-University Training Contest 9 / hdu4389 打巨表,实为数位dp 还不太懂 先这样放着.. 对于打表,当然我们不能直接打,这里有技巧.我们可以 ...

  8. 2014 Multi-University Training Contest 9#11

    2014 Multi-University Training Contest 9#11 Killing MonstersTime Limit: 2000/1000 MS (Java/Others)   ...

  9. 2014 Multi-University Training Contest 9#6

    2014 Multi-University Training Contest 9#6 Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. iOS NSString 文本不同的颜色 标题+文本字体大小 行间距/删除不需要的字符 /以及自适应高度

    #import <Foundation/Foundation.h> @interface TextsForRow : NSObject @property(nonatomic,copy)N ...

  2. C++ 头文件系列(list)

    简介 list实现的实际上是双向链表,所以叫它doubly-linked list也许更好. 因为实现的是双向链表,所以它有两个非常重要的性质: 双向 链表 双向 双向意味着----给定一个元素,我们 ...

  3. xml生成方式二(Xml序列化器XmlSerializer)

    一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...

  4. dropout理解:1神带9坑

    Dropout是深度学习中防止过拟合的一项非常常见的技术,是hinton大神在12年提出的一篇论文里所采用的方法.有传言hinton大神的数学功底不是很好,所以他所提出的想法背后的数学原理并不是很复杂 ...

  5. sourcetree跳过注册的方法

    当前只有Win的版本,Mac自行百度(笑) 很多人用git命令行不熟练,那么可以尝试使用sourcetree进行操作. 然鹅~~sourcetree又一个比较严肃的问题就是,很多人不会跳过注册或者操作 ...

  6. Java自己动手写连接池三

    Java自己动手写连接池三,核心代码; package com.kama.cn; import java.sql.Connection;import java.util.ArrayList;impor ...

  7. php解决json_encode输出GB2312中文问题 (数组)

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存储格式. ...

  8. C# winForm资源文件实现多语言切换

    这是我目前看到过最简单的多语言切换了 操作步驟 介面上的多語 Step1.將表單的Localizable屬性設為True Step2.切換表單的Language屬性為欲使用的語系 設完後會在分頁標籤上 ...

  9. Python学习_09_模块

    模块 模块是python中的最高组织单元,在物理层面上,模块以文件存储,模块的文件名就是模块的名字.py,每个模块都有自己的名称空间. python按照路径搜索来查找模块文件,在PYTHONPATH环 ...

  10. javascript中name,value等属于保留字

    前几天在练习js代码的时候,碰到了一个坑,这是让人醉了. html代码如下: <div> <div> <!--输入 123456--> <lable>请 ...