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. 【HDU3721】枚举+最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3721 题意:给你一颗n个节点n-1条边的树,每条边都有一个权值,现在让你任意移动一条边然后把这条边连接 ...

  2. javascript中的删除方法

    可能呢再开发的过程中呢使用的不是很多,但是碰上呢可以注意下 1.比如: var x = 10; delete x; console.log(x); 结果是多少,是10,不是异常也不是undefined ...

  3. matlab函数bwareaopen的详解

    matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...

  4. 第一课JAVA开发环境配置

    进行JAVA环境安装首先得进行jdk1.7部署,注意应放在没有中文和空格的目录下,然后进行配置环境变量,配置环境变量分为三步: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_ ...

  5. DHTML概述

    <!-- DHTML概述:动态的HTML.不是一门语言,是多项技术综合体的简称. 其中包含了HTML.CSS.DOM.JavaScript. 这四个技术在动态HTML页面效果定义时,都处于什么样 ...

  6. asp

    <%@LANGUAGE="%> <!--#include file="include.asp"--> <meta http-equiv=&q ...

  7. easyUI loyout tabs自适应宽度

    index.html 页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  8. Convert Sorted Array to Binary Search Tree || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

  9. Maven-006-手动部署第三方构件至 nexus 私服

    某些 Java 构件因许可证因素,无法公开的部署到公共仓库中:或者,一些小型的开源项目(例如 SourceForge.GitHub 中的一些项目),没有将构件分发到中央仓库中,也没有维护自己的仓库,因 ...

  10. myEclipse 界面窗口打不开问题

    用的myEclipse版本为2014,在其他工作空间没有此问题,在此工作有此问题. 解决办法: 把当前工作空间视图重置即可