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. JSP + AJAX完整实例及代码

    (1)发送请求index.jsp,注意引入jquery.min.js文件 <%@ page language="java" contentType="text/ht ...

  2. 执行JDBC的executeUpdate()方法时,报错:数据类型不一致,应为number,但却为binary

    该原因是因为,在拼写update语句的时候将一个number类型的字段的值赋为了null导致的,如果想将一个number类型的字清空,不能使用null,可以使用“”来替代.

  3. matplotlib 安装与使用

    1.在ubuntu下输入 sudo apt-get install python-matplotlib 安装matplotlib 2.简单代码使用

  4. php课程---php使用PDO方法详解(转)

    本文详细分析了php使用PDO方法.分享给大家供大家参考.具体分析如下: PDO::exec:返回的是int类型,表示影响结果的条数. 代码如下: PDOStatement::execute 返回的是 ...

  5. 在VPS上部署fq环境

    VPS购买地址 1. 由于我选择的是CentOS 6 x86版本, 需要安装如下准备工具: git, gcc-c++, zlib-devel, openssl-devel, pcre-devel 2. ...

  6. 【iCore3 双核心板】例程三十一:HTTP_IAP_FPGA实验——更新升级FPGA

    实验指导书及代码包下载: http://pan.baidu.com/s/1gdYnQGN iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  7. 【微信开发】 新浪SAE开发平台 注意事项

    1. 微信开发 新浪SAE开发平台 验证Token 一直失败? 这个问题困扰了一个又一个的微信学习者,现在百度到的答案有:在echo $echoStr;之前添加header('content-type ...

  8. yii框架分页

  9. 如何获取网页上的LOGO

    一般公司网页上的图片都会禁止右键另存为,用截图工具接下来的图会带背景色,PS成背景透明有点费时间. 用Google Chrome 或Firefox 打开目标网页,右键点击审查元素,将鼠标放在图片上,一 ...

  10. grabcut

    http://blog.csdn.net/zouxy09/article/details/8535087