Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 62103 Accepted: 29005

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

Source

USACO 2007 January Silver

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int minV=INF;
int maxV=-INF;
struct Node
{
int L,R;//区间起点和终点
int minV,maxV;//本区间里的最大最小值
int Mid(){
return (L+R)/2;
}
};
Node tree[800010];
void BuildTree(int root, int L, int R)
{
tree[root].L = L;
tree[root].R = R;
tree[root].minV = INF;
tree[root].maxV = -INF;
if(L != R)
{
BuildTree(2*root+1, L, (L+R)/2);
BuildTree(2*root+2, (L+R)/2 + 1, R);
}
}
void Insert(int root, int i ,int v)//将第i个数,其值为v,插入线段树
{
if(tree[root].L == tree[root].R)
{
tree[root].minV = tree[root].maxV = v;
return ;
}
tree[root].minV = min(tree[root].minV, v);
tree[root].maxV = max(tree[root].maxV, v);
if(i <= tree[root].Mid())
Insert(2*root+1,i,v);
else
Insert(2*root+2,i,v);
}
void Query(int root, int s, int e)//查询区间[s,e]中的最小值和最大值,如果更优就记在全局变量里
//minV和maxV里
{
if(tree[root].minV >= minV && tree[root].maxV <= maxV)
return;
if(tree[root].L == s && tree[root].R == e)
{
minV = min(minV, tree[root].minV);
maxV = max(maxV, tree[root].maxV);
return;
}
if(e <= tree[root].Mid())
Query(2*root+1, s, e);
else if(s > tree[root].Mid())
Query(2*root+2, s, e);
else
{
Query(2*root+1, s, tree[root].Mid());
Query(2*root+2, tree[root].Mid()+1, e);
}
}
int main()
{
int n,q,h;
int i,j,k;
scanf("%d%d",&n,&q);
BuildTree(0,1,n);
for(i=1;i<=n;i++)
{
scanf("%d",&h);
Insert(0,i,h);
}
for(i=0;i<q;i++)
{
int s,e;
scanf("%d%d",&s,&e);
minV = INF;
maxV = -INF;
Query(0,s,e);
printf("%d\n",maxV - minV);
}
}
/*
6 3
1 7 3 4 2 5
1 5
4 6
2 2
*/

POJ 3264 Balanced Lineup 【线段树/区间最值差】的更多相关文章

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

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

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

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

  3. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  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 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  6. 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 ...

  7. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

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

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

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

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

  10. Poj 3264 Balanced Lineup RMQ模板

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

随机推荐

  1. Spark探索经典数据集MovieLens

    Spark探索经典数据集MovieLens 阅读目录 前言 环境 初步预览 探索用户数据 探索电影数据 探索评级数据 回到顶部 前言 MovieLens数据集包含多个用户对多部电影的评级数据,也包括电 ...

  2. 【bzoj1458】士兵占领 有上下界最小流

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  3. HDU 2126 01背包(求方案数)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. The XOR Largest Pair [Trie]

    描述 在给定的N个整数A1,A2--AN中选出两个进行xor运算,得到的结果最大是多少? 输入格式 第一行一个整数N,第二行N个整数A1-AN. 输出格式 一个整数表示答案. 样例输入 3 1 2 3 ...

  5. fuser命令找到占用资源的进程

    fuser 概述 fuser命令是用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息. 例一: #fuser –m –u /mnt/usb1 /mnt/us ...

  6. [CVPR2018]Learning to Adapt Structured Output Space for Semantic Segmentation

    学习适应结构化输出空间进行语义分割 在语义分割场景中,虽然物体在外表上不同,但是他们的输出是结构化且共享很多例如空间分布, 局部内容等信息.所以作者提出了multi-level的输出空间adaptat ...

  7. HDU2057

    http://acm.hdu.edu.cn/showproblem.php?pid=2057 涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表示的.一个比较困惑的 ...

  8. 状压dp的题目列表 (一)

    状压dp的典型的例子就是其中某个数值较小. 但是某个数值较小也不一定是状压dp,需要另外区分的一种题目就是用暴力解决的题目,例如UVA818 紫书215 题目列表: ①校长的烦恼 UVA10817 紫 ...

  9. DotNETCore 学习笔记 异常处理

    Error Handling public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseIISP ...

  10. [ CodeVS冲杯之路 ] P2456

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/2456/ 用贪心的思想,木材当然要尽量分成多的木板,而大的木材能够分成大木板,但是小的木材不一定能够分成大的木板,所以木 ...