OJ题号:
洛谷2880

思路1:

线段树维护区间最大最小值。

 #include<cstdio>
#include<cctype>
#include<utility>
#include<algorithm>
inline int getint() {
char ch;
bool sgn=false;
while(!isdigit(ch=getchar())) if(ch=='-') sgn=true;
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return sgn?-x:x;
}
const int inf=0x7fffffff;
const int N=;
class SegmentTree {
#define _left <<1
#define _right <<1|1
private:
int max[N<<],min[N<<];
void push_up(const int p) {
max[p]=std::max(max[p _left],max[p _right]);
min[p]=std::min(min[p _left],min[p _right]);
}
public:
void build(const int p,const int b,const int e) {
if(b==e) {
max[p]=min[p]=getint();
return;
}
int mid=(b+e)>>;
build(p _left,b,mid);
build(p _right,mid+,e);
push_up(p);
}
std::pair<int,int> query(const int p,const int b,const int e,const int l,const int r) {
if((b==l)&&(e==r)) {
return std::make_pair(max[p],min[p]);
}
int mid=(b+e)>>;
int max=,min=inf;
if(l<=mid) {
std::pair<int,int> tmp=query(p _left,b,mid,l,std::min(mid,r));
max=std::max(max,tmp.first);
min=std::min(min,tmp.second);
}
if(r>mid) {
std::pair<int,int> tmp=query(p _right,mid+,e,std::max(mid+,l),r);
max=std::max(max,tmp.first);
min=std::min(min,tmp.second);
}
return std::make_pair(max,min);
}
};
SegmentTree t;
int main() {
int n=getint(),m=getint();
t.build(,,n);
while(m--) {
int l=getint(),r=getint();
std::pair<int,int> tmp=t.query(,,n,l,r);
printf("%d\n",tmp.first-tmp.second);
}
return ;
}

思路2:
倍增法求RMQ。

 #include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,logN=log2(N)+;
int min[N][logN],max[N][logN];
int main() {
int n=getint(),m=getint();
for(int i=;i<=n;i++) {
min[i][]=max[i][]=getint();
}
for(int j=;j<=log2(n);j++) {
for(int i=;i<=n-(<<j)+;i++) {
min[i][j]=std::min(min[i][j-],min[i+(<<(j-))][j-]);
max[i][j]=std::max(max[i][j-],max[i+(<<(j-))][j-]);
}
}
while(m--) {
int l=getint(),r=getint();
int k=log2(r-l+);
printf("%d\n",std::max(max[l][k],max[r+-(<<k)][k])-std::min(min[l][k],min[r+-(<<k)][k]));
}
return ;
}

[USACO07JAN]Balanced Lineup的更多相关文章

  1. 洛谷 P2880 [USACO07JAN]Balanced Lineup G (ST表模板)

    题意:给你一组数,询问\(q\)次,问所给区间内的最大值和最小值的差. 题解:经典RMQ问题,用st表维护两个数组分别记录最大值和最小值然后直接查询输出就好了 代码: int n,q; int a[N ...

  2. 洛谷P2880 [USACO07JAN] Balanced Lineup G(树状数组/线段树)

    维护区间最值的模板题. 1.树状数组 1 #include<bits/stdc++.h> 2 //树状数组做法 3 using namespace std; 4 const int N=5 ...

  3. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

  4. P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般 ...

  5. P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream&g ...

  6. 【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)

    题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连 ...

  7. [USACO07JAN]平衡的阵容Balanced Lineup

    [USACO07JAN]平衡的阵容Balanced Lineup 题目描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) a ...

  8. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  9. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

随机推荐

  1. Solr记录-solr基础内容

    Solr架构(体系结构) 在本章中,我们将讨论Apache Solr的架构. 下图显示了Apache Solr的体系结构的框图. Solr架构 - 构件块以下是Apache Solr的主要构建块(组件 ...

  2. Codeforces 906 D. Power Tower

    http://codeforces.com/contest/906/problem/D 欧拉降幂 #include<cstdio> #include<iostream> usi ...

  3. bzoj千题计划237:bzoj1492: [NOI2007]货币兑换Cash

    http://www.lydsy.com/JudgeOnline/problem.php?id=1492 dp[i] 表示 第i天卖完的最大收益 朴素的dp: 枚举从哪一天买来的在第i天卖掉,或者是不 ...

  4. 【转】UICollectionView使用介绍

    CHENYILONG Blog UICollectionView 使用介绍 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/lu ...

  5. caoha

  6. Expression Tree Build

    The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...

  7. centos6.8安装mysql5.6【转】

    首先先要去看看本机有没有默认的mysql, 本地默认有的,我们应先卸载,在安装新的这个逻辑. rpm -qa | grep mysql 我本机默认安装的mysql5.1.73 下一步删除 rpm -e ...

  8. C#使用redis学习笔记

    1.官网:http://redis.io/(英)  http://www.redis.cn/(中) 2.下载:https://github.com/dmajkic/redis/downloads(Wi ...

  9. IPsec学习笔记

    IPsec是什么 IPsec(IP Security)是一系列为IP通信提供安全性的协议和服务的集合,工作在IP层,可以为上层协议和应用提供透明的安全服务.IPsec提供两种安全机制:认证和加密. 认 ...

  10. JPA 菜鸟教程 15 继承-一个表-SINGLE_TABLE

    原地址:http://blog.csdn.net/JE_GE/article/details/53678422 继承映射策略 一个类继承结构一个表的策略,最终只生成一个表,这是继承映射的默认策略. 举 ...