Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 40493 Accepted: 19035

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 <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL; int R[55000][20][2]; int N,Q; void RMQ()//计算区间的最值(0代表最大值1代表最小值)
{
for(int j=1;(1<<j)<=N;j++)
{
for(int i=0;i+(1<<j)-1<N;i++)
{
R[i][j][0]=max(R[i][j-1][0],R[i+(1<<(j-1))][j-1][0]); R[i][j][1]=min(R[i][j-1][1],R[i+(1<<(j-1))][j-1][1]);
}
}
} int RMQ_Look(int l,int r)
{
int ans=0; while((1<<(ans+1))<=(r-l+1))
{
ans++;
} return max(R[l][ans][0],R[r-(1<<ans)+1][ans][0])-min(R[l][ans][1],R[r-(1<<ans)+1][ans][1]);
} int main()
{
int u,v; scanf("%d %d",&N,&Q); for(int i=0;i<N;i++)
{
scanf("%d",&R[i][0][0]); R[i][0][1]=R[i][0][0];
} RMQ(); for(int i=1;i<=Q;i++)
{
scanf("%d %d",&u,&v); u--; v--; printf("%d\n",RMQ_Look(u,v));
}
return 0;
}

Balanced Lineup(树状数组 POJ3264)的更多相关文章

  1. poj3264 Balanced Lineup(树状数组)

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

  2. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

  3. 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)

    维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...

  4. [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)

    传送门 树状数组裸题 #include <cstdio> #include <cstring> #include <iostream> #include <a ...

  5. [USACO17JAN]Balanced Photo平衡的照片 (树状数组)

    题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...

  6. st表树状数组入门题单

    预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...

  7. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  8. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  9. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

随机推荐

  1. hdu Remainder

    这道题是道很明显的bfs题.因为对数论没什么研究 ,所以这道题目里的两个关键点并不知道,看了别人的题解才知道 . 1.为避免取模后出现负数,采用:x%y=(x%y+y)%y 2.全部采用对m*k取模后 ...

  2. 网络地址转换NAT原理及其作用

    1 概述 1.1 简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task F ...

  3. for memory long term update

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. #例如,下面的命令: rm `find /path -type f ...

  4. Python一般错误

    1. IndentationError: unindent does not match any outer indentation level 格式对齐的问题.Python对空格和Tab有严格区别

  5. usaco 2016 Feb 负载平衡

    题目大意:平面上一堆点,用两条平行于坐标轴的直线将其分为四部分,使得点数最多的一部分最少 第一维枚举,第二维三分,点集用两棵树状数组维护 #include<bits/stdc++.h> # ...

  6. 【转】C#(ASP.Net)获取当前路径的方法集合

    转自:http://www.gaobo.info/read.php/660.htm //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Asse ...

  7. linux下开启防火墙,打开端口

    service iptables start时提示:“iptables: No config file.                                  [WARNING]” 此时打 ...

  8. mysql重点--执行计划

    explain SQL: 在sql语句前面加explain实现"执行计划"的功能.功能是比较准确的显示将要执行这条sql语句的运行状况. select_simple 是查询类型:t ...

  9. jQuery源代码阅读之二——jQuery静态属性和方法

    一.jQuery.extend/jQuery.fn.extend //可接受的参数类型如下:jQuery.extend([deep],target,object1,[objectN]) jQuery. ...

  10. 从零开始学Linux[二]:常用操作:用户组、进程、网络、ssh

    摘要:Linux基础学习:创建用户组和用户.软件包管理.磁盘管理.进程管理.前后台进程的切换.网络配置.浏览网页.远程登录ssh 第一节,主要介绍一些简单命令,这节介绍一些日常操作. 1.创建用户组和 ...