链接:http://poj.org/problem?id=3264

题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问

思路:ST表模板,预处理区间最值,O(1)复杂度询问

AC代码:

 #include<iostream>
#include<vector>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn = ;
const int logn = ;
int Log[maxn];
int MIN[maxn][logn];
int MAX[maxn][logn];
int cow[maxn];
int n,q;
void pre(){
Log[] = ;
Log[] = ;
for(int i = ;i<=n;i++){
Log[i] = Log[i/] + ;
}
}
void st(){
for(int i = ;i<=n;i++){
MIN[i][] = MAX[i][] = cow[i];
}
for(int j = ;(<<j)<=n;j++){
for(int i = ;i+(<<j)-<=n;i++){
MAX[i][j] = max(MAX[i][j-],MAX[i+(<<(j-))][j-]);
MIN[i][j] = min(MIN[i][j-],MIN[i+(<<(j-))][j-]);
}
}
}
int main()
{
scanf("%d%d",&n,&q);
for(int i = ;i<=n;i++){
scanf("%d",&cow[i]);
}
pre();
st();
while(q--){
int A,B;
scanf("%d%d",&A,&B);
int s = Log[B-A+];
int range_max = max(MAX[A][s],MAX[B-(<<s)+][s]);
int range_min = min(MIN[A][s],MIN[B-(<<s)+][s]);
cout<<range_max-range_min<<endl;
}
return ;
}

POJ 3264 Balanced Lineup(ST模板)的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

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

  2. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  3. POJ 3264 Balanced Lineup(模板题)【RMQ】

    <题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...

  4. POJ 3264 Balanced Lineup | st表

    题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...

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

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

  6. 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分析:标 ...

  7. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  8. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  9. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  10. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. 通过 Chrome浏览器 查看http请求报文

    as we all know  HTTP 请求报文 包含请求行.请求头和请求体三部分 请求行:(请求方式 资源路径 协议/版本) 例如:POST /test/index.html HTTP/1.1 P ...

  2. Pikachu-Sql Inject(SQL注入)

    在owasp发布的top10排行榜里,注入漏洞一直是危害排名第一的漏洞,其中注入漏洞里面首当其冲的就是数据库注入漏洞.一个严重的SQL注入漏洞,可能会直接导致一家公司破产!SQL注入漏洞主要形成的原因 ...

  3. 轻量级RPC设计与实现第五版(最终版)

    在最近一段时间里,通过搜集有关资料加上自己的理解,设计了一款轻量级RPC,起了一个名字lightWeightRPC.它拥有一个RPC常见的基本功能.主要功能和特点如下: 利用Spring实现依赖注入与 ...

  4. Python三元表达式、列表推导式、生成器表达式

    1. 三元表达式 name=input('姓名>>: ') res='SB' if name == 'aaaa' else 'NB' print(res) 2. 列表推导式 #1.示例 e ...

  5. 修改testlink上传文件大小

    大家在使用testlink导入测试时,可能会遇到由于上传的文件太大,而不能上传的现象.当然建议大家在导入用例或者上传文件时,不要上传过大的文件,因为这样的速度确实非常慢.可是由于特殊的原因我们必须要导 ...

  6. Python面向对象三大特性(封装、继承、多态)

    封装 类中把某些属性和方法隐藏起来,或者定义为私有,只在类的内部使用,在类的外部无法访问,或者留下少量的接口(函数)供外部访问:从上一篇文章中的私有属性与私有方法中的代码体现了该特性. class m ...

  7. LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表

    文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...

  8. JAVA StringUtils工具类

    org.apache.commons.lang Class StringUtils java.lang.Object org.apache.commons.lang.StringUtils publi ...

  9. PWA - 整体(未完)

    渐进式 Web 应用(PWA) 运用现代的 Web API 以及传统的渐进式增强策略来创建跨平台 Web 应用程序. PWA 的优势 可被发现 易安装 manifest(https://develop ...

  10. 洛谷P3367 【模板】并查集 模板 找baba

    链接https://www.luogu.org/problem/P3367 #include<bits/stdc++.h> using namespace std; ; int fa[ra ...