Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 75294   Accepted: 34483
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

题意

查询区间内最大值和最小值的差值

解决

解法一:ST表

通过O(n*log(n))的预处理后,在O(1)的时间内查询出区间最值。

预处理的本质是DP,定义一个dp数组dp[i][j],dp[i][j]表示:从第i个数起,连续2^j个数的最值,可以很容易得到:dp[i][0]就是第i个数的本身

接下来可以得到转移方程:dp[i][j]=max(dp[i][j-1],dp[i+2^(j-1)][j-1])

然后我们可以通过预处理求出所有位置的dp数组的值

在查询的时候,先算出所求区间的长度对2取对数,即求出上述方程中的j,然后用方程O(1)查询即可

解法二:线段树

代码

ST表

 1 #include <iostream>
2 #include <algorithm>
3 #include <cmath>
4 #define ll long long
5 #define ull unsigned long long
6 #define ms(a,b) memset(a,b,sizeof(a))
7 const int inf=0x3f3f3f3f;
8 const ll INF=0x3f3f3f3f3f3f3f3f;
9 const int maxn=1e6+10;
10 const int mod=1e9+7;
11 const int maxm=1e3+10;
12 using namespace std;
13 // rmq[i][j]表示从i开始的第2^i位上的最大/最小值
14 // rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1])
15 int rmq_max[maxn][30];
16 int rmq_min[maxn][30];
17 int a[maxn];
18 int main(int argc, char const *argv[])
19 {
20 #ifndef ONLINE_JUDGE
21 freopen("/home/wzy/in.txt", "r", stdin);
22 freopen("/home/wzy/out.txt", "w", stdout);
23 srand((unsigned int)time(NULL));
24 #endif
25 ios::sync_with_stdio(false);
26 cin.tie(0);
27 int n,q;
28 cin>>n>>q;
29 for(int i=1;i<=n;i++)
30 cin>>a[i],rmq_min[i][0]=a[i],rmq_max[i][0]=a[i];
31 for(int j=1;(1<<j)<=n;j++)
32 for(int i=1;i+(1<<(j-1))-1<=n;i++)
33 rmq_min[i][j]=min(rmq_min[i][j-1],rmq_min[i+(1<<(j-1))][j-1]),rmq_max[i][j]=max(rmq_max[i][j-1],rmq_max[i+(1<<(j-1))][j-1]);
34 while(q--)
35 {
36 int x,y;
37 cin>>x>>y;
38 int z=(int)(log(y-x+1)/log(2));
39 int ans=max(rmq_max[x][z],rmq_max[y-(1<<z)+1][z])-min(rmq_min[x][z],rmq_min[y-(1<<z)+1][z]);
40 cout<<ans<<"\n";
41 }
42 #ifndef ONLINE_JUDGE
43 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
44 #endif
45 return 0;
46 }

线段树

不太会线段树,代码略丑

 1 #include <iostream>
2 #include <algorithm>
3 #define ll long long
4 #define ull unsigned long long
5 #define ms(a,b) memset(a,b,sizeof(a))
6 const int inf=0x3f3f3f3f;
7 const ll INF=0x3f3f3f3f3f3f3f3f;
8 const int maxn=1e6+10;
9 const int mod=1e9+7;
10 const int maxm=1e3+10;
11 using namespace std;
12 int a[maxn];
13 struct wzy
14 {
15 int value_min,value_max;
16 }p[maxn];
17 inline int lson(int p){return p<<1;}
18 inline int rson(int p){return p<<1|1;}
19 inline void push_max(int o)
20 {
21 p[o].value_max=max(p[lson(o)].value_max,p[rson(o)].value_max);
22 }
23 inline void push_min(int o)
24 {
25 p[o].value_min=min(p[lson(o)].value_min,p[rson(o)].value_min);
26 }
27 void build(int o,int l,int r)
28 {
29 if(l==r)
30 {
31 p[o].value_min=p[o].value_max=a[l];
32 return ;
33 }
34 int mid=(l+r)>>1;
35 build(lson(o),l,mid);
36 build(rson(o),mid+1,r);
37 push_max(o);
38 push_min(o);
39 }
40 int res,res1;
41 int query_min(int nl,int nr,int l,int r,int o)
42 {
43 if(nl<=l&&nr>=r)
44 return min(res,p[o].value_min);
45 int mid=(l+r)>>1;
46 if(nl<=mid)
47 res=query_min(nl,nr,l,mid,lson(o));
48 if(nr>mid)
49 res=query_min(nl,nr,mid+1,r,rson(o));
50 return res;
51 }
52 int query_max(int nl,int nr,int l,int r,int o)
53 {
54 if(nl<=l&&nr>=r)
55 return max(res1,p[o].value_max);
56 int mid=(l+r)>>1;
57 if(nl<=mid)
58 res1=query_max(nl,nr,l,mid,lson(o));
59 if(nr>mid)
60 res1=query_max(nl,nr,mid+1,r,rson(o));
61 return res1;
62 }
63 int main(int argc, char const *argv[])
64 {
65 #ifndef ONLINE_JUDGE
66 freopen("/home/wzy/in.txt", "r", stdin);
67 freopen("/home/wzy/out.txt", "w", stdout);
68 srand((unsigned int)time(NULL));
69 #endif
70 ios::sync_with_stdio(false);
71 cin.tie(0);
72 int n,q;
73 cin>>n>>q;
74 for(int i=1;i<=n;i++)
75 cin>>a[i];
76 build(1,1,n);
77 int x,y;
78 while(q--)
79 {
80 cin>>x>>y;
81 res=inf;res1=0;
82 cout<<query_max(x,y,1,n,1)-query_min(x,y,1,n,1)<<"\n";
83 }
84 #ifndef ONLINE_JUDGE
85 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
86 #endif
87 return 0;
88 }

POJ 3264:Balanced Lineup(区间最值查询ST表&线段树)的更多相关文章

  1. POJ 3264 Balanced Lineup 区间最值

    POJ3264 比较裸的区间最值问题.用线段树或者ST表都可以.此处我们用ST表解决. ST表建表方法采用动态规划的方法, ST[I][J]表示数组从第I位到第 I+2^J-1 位的最值,用二分的思想 ...

  2. poj 3264 Balanced Lineup 区间极值RMQ

    题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...

  3. Poj 3264 Balanced Lineup RMQ模板

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

  4. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

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

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

  6. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

  7. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  8. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  9. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

随机推荐

  1. 『学了就忘』Linux文件系统管理 — 64、磁盘配额的配置步骤

    目录 1.手工建立一个5GB的分区 2.建立需要做限制的三个用户 3.在分区上开启磁盘配额功能 4.建立磁盘配额的配置文件 5.开始设置用户和组的配额限制 6.启动和关闭配额 7.磁盘配额的查询 8. ...

  2. 重学Git(一)

    一.最最最基础操作 # 初始化仓库 git init # 添加文件到暂存区 git add readme.md # 提交 git commit -m 'wrote a readme file' 二.简 ...

  3. YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

    YARP 是微软开源的一个反向代理项目,英文名叫 Yet Another Reverse Proxy .所谓反向代理最有名的那就是 nginx 了,没错 YARP 也可以用来完成 nginx 的大部分 ...

  4. 27.0 linux VM虚拟机IP问题

    我的虚拟机是每次换一个不同的网络,b不同的ip,使用桥接模式就无法连接,就需要重新还原默认设置才行: 第一步:点击虚拟机中的编辑-->虚拟网络编辑器 第二步:点击更改设置以管理员权限进入 第三步 ...

  5. 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)

    1. ZK的监控机制 1.1 监听数据的变化  (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...

  6. 零基础学习java------34---------登录案例,域,jsp(不太懂),查询商品列表案例(jstl标签)

    一. 简单登录案例 流程图: 项目结构图 前端代码: <!DOCTYPE html> <html> <head> <meta charset="UT ...

  7. 谈谈你对volatile的理解

    1.volatile是Java虚拟机提供的轻量级的同步机制 1.1保证可见性1.2不保证原子性1.3禁止指令重排 JMM(Java内存模型Java Memory Model,简称JMM)本身是一种抽象 ...

  8. 几种常用JavaScript设计模式es6

    设计模式分类(23种设计模式) 创建型 单例模式 原型模式 工厂模式 抽象工厂模式 建造者模式 结构型 适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式 行为型 观察者模式 迭 ...

  9. 快速挂起VIM以及调出被挂起的VIM的方法

    vim中开了多窗口后有时需要临时切出去执行shell指令,查看结果,在vim中用%很不方便查看结果,要切出去又要逐个小窗口:q,非常麻烦. 上网一查竟然有挂起的方法: 挂起:ctrl-z 调出:fg ...

  10. 时光网内地影视票房Top100爬取

    为了和艺恩网的数据作比较,让结果更精确,在昨天又写了一个时光网信息的爬取,这次的难度比艺恩网的大不少,话不多说,先放代码 # -*- coding:utf-8 -*-from __future__ i ...