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. js 进度条,可实现结束和重新开始

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. LINUX数据库的备份,以及远程授权登陆

    mysql dump -u root -p juhui > /data/juhui.sql   //备份数据库 grant all privileges on *.* to xf111@loca ...

  3. IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)

    **** #import "HMViewController.h" // 每秒秒针转6度 #define perSecendA 6 // 每分钟分针转6度 #define perM ...

  4. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  5. 封装常用的js(Base.js)——【01】理解库,获取节点,连缀,

    封装常用的js(Base.js)——[01]理解库,获取节点,连缀,  youjobit07 2014-10-10 15:32:59 前言:       现如今有太多优秀的开源javascript库, ...

  6. lua 可变参数

    问题:对可变参数传递的时候,采用如下方案: local cellData = {MsgText = msgText,Param = ...,CallBackFunc = callBackFunc,Ca ...

  7. ExtJS笔记 Tree

    The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...

  8. 【转】关于Unity协同程序(Coroutine)的全面解析

    http://www.unity.5helpyou.com/2658.html 本篇文章我们学习下unity3d中协程Coroutine的的原理及使用 1.什么是协调程序 unity协程是一个能暂停执 ...

  9. tomcat服务重启linux

    1杀掉tomcat 进程  用ssh登陆到服务器 lsof -i:8080         //找到端口 ps -ef|grep tomcat kill -9 端口 2找到tomcat目下的start ...

  10. Git reset 常见用法

    Git reset 1. 文件从暂存区回退到工作区 2. 版本回退 1.1 git reset HEAD filename :回退文件,将文件从暂存区回退到工作区 //也可以使用 git reset ...