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 ;
}

(╯°Д°)╯︵┻━┻

POJ3264-Balanced Lineup-线段树的更多相关文章

  1. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

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

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

  5. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  6. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  7. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

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

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

  9. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  10. POJ 3264 Balanced Lineup (线段树)

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

随机推荐

  1. 微信JS-SDK使用步骤(以微信扫一扫为例)

    概述: 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用 ...

  2. Docker(七):Docker容器卷管理

    1.使用容器卷的原因:Docker容器产生的数据,如果不通过commit生成新的镜像,数据会在容器删除后丢失.为了能持久化保存和共享容器的数据,Docker提出了两种管理数据的方式:数据卷和数据卷容器 ...

  3. Handwritten Parsers & Lexers in Go (翻译)

    用go实现Parsers & Lexers 在当今网络应用和REST API的时代,编写解析器似乎是一种垂死的艺术.你可能会认为编写解析器是一个复杂的工作,只保留给编程语言设计师,但我想消除这 ...

  4. 聊聊API网关的作用

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.5px "Trebuchet MS" } p.p2 { margin: 0.0px ...

  5. SQL基础学习_01_数据库和表

    SQL语句及其种类 1. SQL语句分为三类:     DDL(Data Definition Language): CREATE.DROP.ALTER;     DML(Data Manipulat ...

  6. SqlServer Lock_Escalation

    在今天的文章里,我想谈下SQL Server里锁升级(Lock Escalations).锁升级是SQL Server使用的优化技术,用来控制在SQL Server锁管理里把持锁的数量.我们首先用SQ ...

  7. XML的解析(DOM以及SAX方式)

    感谢http://blog.csdn.net/redarmy_chen/article/details/12951649(关于SAX解析)以及http://blog.csdn.net/zhangerq ...

  8. Hibernate学习笔记(2)---hibernate核心文件

    配置hibernate.cfg.xml hibernate配置文件包含连接持久层与映射文件所需的基本信息.配置文件名默认为hibernate.cfg.xml. hibernate.cfg.xml文件配 ...

  9. vue2.0 微信oauth认证的正确调用位置

    运行在微信端的项目,很重要的环节是oauth认证:那在vue项目中,在何时何地调用oauth认证最合适呢? 经过观察,在项目启动过程中,会执行main.js文件:所以我将认证放在main.js中操作: ...

  10. (笔记):组合and继承之访问限制(一)

    下面在介绍组合与继承之前,先介绍一下访问限制,访问限制:public.protected.private三者是按照授权的大小排序的.这里有个博客,对这三者有了经典的诠释.http://blog.csd ...