Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 53721   Accepted: 25244
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 ≤ 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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

题意就是在一堆数里找一定范围里的最大值和最小值,计算差值。

因为知道这个题肯定不能瞎写就写对,所以还很认真的写了一个代码,真是智障,不管怎么写,反正T了。

线段树,不会用,瞎写。。。

线段树大法好,可惜智障,改了12遍(此刻内心¥…&%……*(*&),出现各种问题嘛,存最小值要再有东西存人家啊。。。

最后实在改不出来了,求助了大佬,最后问题还是出在最小值问题上,要求最小值,一开始比较的ans就要比数组里的最大数要大啊。。。要审清题和题意。。。

线段树!!!

代码(垃圾写的乱七八糟):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
#define N 300000
struct node{
int left,right,maxx,minn;
}tree[N*];
int n,m,minn,maxx;
void build(int l,int r,int pos){
tree[pos].left=l;
tree[pos].right=r;
tree[pos].maxx=;
tree[pos].minn=1e9;
if(tree[pos].left==tree[pos].right)return;
int mid=(l+r)>>;
build(l,mid,pos*);
build(mid+,r,pos*+);
}
void update(int x,int y,int pos){
if(tree[pos].left==x&&tree[pos].right==x){
tree[pos].maxx=y;
tree[pos].minn=y;
return;
}
int mid=(tree[pos].left+tree[pos].right)>>;
if(x>mid)
update(x,y,pos*+);
else
update(x,y,pos*);
tree[pos].maxx=max(tree[pos*].maxx,tree[pos*+].maxx);
tree[pos].minn=min(tree[pos*].minn,tree[pos*+].minn);
}
void query(int x,int y,int pos){
if(tree[pos].left==x&&tree[pos].right==y){
maxx=max(maxx,tree[pos].maxx);
minn=min(minn,tree[pos].minn);
return ;
}
int mid=(tree[pos].left+tree[pos].right)>>;
if(y<=mid)query(x,y,pos*);
else if(x>mid)query(x,y,pos*+);
else{
query(x,mid,pos*);
query(mid+,y,pos*+);
}
}
int main(){
int m,n;
int ans1,ans2;
while(~scanf("%d%d",&n,&m)){
getchar();
build(,n,);
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
update(i,x,);
}
int a,b;
while(m--){
scanf("%d%d",&a,&b);
maxx=;
minn=1e9;
query(a,b,);
printf("%d\n",maxx-minn);
}
}
return ;
}

(╯°Д°)╯︵┻━┻

POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  2. ACM_最值差(线段树区间查询最值)

    最值差 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定N个数A1A2A3A4...AN.求任意区间Ai到Aj中的最大数与最小 ...

  3. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  4. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

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

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

  6. POJ 3264 Balanced Lineup 线段树 第三题

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

  7. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  8. poj 3264 Balanced Lineup (线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 ...

  9. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

随机推荐

  1. Web.config配置文件中的属性add,key,value含义

    这是添加自定义字符串的方式,保存是以键-值的形式保存的,可以通过key获取value,一般用这种方法配置全局内使用的字符串. <configuration>是配置文件的根配置节. < ...

  2. python数据绘图常用方法总结

    挖坑,以后还会更新吧 做数学建模画图使用了matplotlib和numpy,这里简单总结一下常用的用法 一.数据拟合 1.np.polyfit(x, y, n) 使用n次多项式去拟合x,y散点图,返回 ...

  3. 【题解】ZJOI2017仙人掌

    感觉这题很厉害啊,虽然想了一天多但还是失败了……(:д:) 这题首先注意到给定图中如果存在环其实对于答案是没有影响的.然后关键之处就在于两个 \(dp\) 数组,其中 \(f[u]\) 表示以 \(u ...

  4. [Leetcode] Length of last word 最后一个单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...

  5. JavaScript使用数组拼接字符串性能如何?

    传统上,字符串连接一直是js中性能最低的操作之一. view source   print? 1 var text="Hello"; 2 text+=" World!&q ...

  6. 如何在数据访问层上提高js的执行效率

    本文讲到的是如何从数据访问层面上提高JS 代码的执行效率.总的来讲有以下几条原则: 函数中读写局部变量总是最快的,而全局变量的读取则是最慢的: 尽可能地少用with 语句,因为它会增加with 语句以 ...

  7. HDU3338:Kakuro Extension(最大流)

    Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Codeforces Round #524 (Div. 2) A. Petya and Origami

    A. Petya and Origami 题目链接:https://codeforc.es/contest/1080/problem/A 题意: 给出n,k,k表示每个礼品里面sheet的数量(礼品种 ...

  9. centos6上使用fpm打python2.7 rpm包并兼容python2.6

    centos6上使用fpm打python2.7 rpm包并兼容python2.6 作者 运维小兵_加油 关注 2016.09.22 00:28 字数 501 阅读 45评论 0喜欢 1 工作中我们常常 ...

  10. 解决“并非来自 Chrome 网上应用店。”

    Chrome谷歌浏览器已停用不支持的扩展程序解决方法 第一种方法:(亲测有效) 1.首先把需要安装的第三方插件,后缀.crx 改成 .rar,然后解压,得到一个文件夹 2.再打开chrome://ex ...