嘟嘟嘟

一道RMQ板子题,分别维护最大值和最小值,不解释。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps =1e-;
const int maxn = 5e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, a[maxn]; int dp[maxn][][], b[maxn];
void RMQ()
{
for(int i = ; i <= n; ++i) dp[i][][] = dp[i][][] = a[i];
for(int j = ; ( << j) <= n; ++j)
for(int i = ; i + ( << j) - <= n; ++i)
{
dp[i][j][] = max(dp[i][j - ][], dp[i + ( << (j - ))][j - ][]);
dp[i][j][] = min(dp[i][j - ][], dp[i + ( << (j - ))][j - ][]);
}
int x = ;
for(int i = ; i <= n; ++i)
{
b[i] = x;
if(( << (x + )) <= (i + )) x++;
}
}
int query(int L, int R, bool flag)
{
int k = b[R - L + ];
if(flag) return min(dp[L][k][flag], dp[R - ( << k) + ][k][flag]); //应该是R - (1 << k) + 1,不是R - k + 1……
else return max(dp[L][k][flag], dp[R - ( << k) + ][k][flag]);
} int main()
{
n = read(); m = read();
for(int i = ; i <= n; ++i) a[i] = read();
RMQ();
for(int i = ; i <= m; ++i)
{
int L = read(), R = read();
write(query(L, R, ) - query(L, R, )); enter;
}
return ;
}

[Usaco2007 Jan]Balanced Lineup的更多相关文章

  1. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  2. BZOJ1636: [Usaco2007 Jan]Balanced Lineup

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[ ...

  3. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  4. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  5. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  6. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  7. [Usaco2007 Jan]Balanced Lineup排队

    [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...

  8. bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*

    bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...

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

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

  10. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

随机推荐

  1. CRC16位校验

    之前有跟第三方通讯合作,应为CRC表码问题导致校验出结果不一致,纠结了很久,最后直接采用CRC计算方式校验才解决. 两种方式贴,自行对比. CRC校验计算方法 private ushort CRC_1 ...

  2. WebForm 【复合控件】

    一 复合控件(取值,赋值用法相近)  RadioButtonList      --单选按钮 (一组列表)  <asp:RadioButtonList ID="RadioButtonL ...

  3. 9.C#知识点:线程初识及Thread初识(一)

    知识点目录==========>传送门 线程和进程的简单概括. 1.进程就是"活动中"的程序,一个.程序是一个没有生命的实体,只有处理器赋予程序生命时,它才能成为一个活动的实 ...

  4. WebServer搭建过程

    第一步,新建Web网站项目 第二步: 右击项目解决方案,选择添加新建项 选择Web服务项 第三步,在asmx方法中将Hello World注释掉,然后编写自己的方法,这里根据项目的不同可以做不同的处理 ...

  5. 用01随机函数构造[a,b]整数范围随机数

    #include <stdio.h> #include <stdlib.h> #define RAND_0_1 (rand()&0x1) int random(int ...

  6. mysql小试题

    1. 用户登录日志表 xes_user_login_logs 如下: (1) 检索登录超过两次的用户ID(sql语句) select user_id from vvt_ceshi group by u ...

  7. 小tip:CSS vw让overflow:auto页面滚动条出现时不跳动——张鑫旭

    小tip:CSS vw让overflow:auto页面滚动条出现时不跳动 这篇文章发布于 2015年01月25日,星期日,23:08,归类于 css相关. 阅读 46274 次, 今日 91 次 by ...

  8. a 链接点击下载

    1. 将链接设置为.zip 结尾2.在a元素中添加download 属性,(目前只有chrome.firefox和opera支持) function download(src) { var $a = ...

  9. JS中String与Array的一些常用方法

    真是恨透了这些类似于substring substr slice 要么长得像,要么就功能相近的方法... 1⃣️string 1.substring(start开始位置的索引,end结束位置索引) 截 ...

  10. loadrunner 脚本开发- web_url函数详解

    脚本开发- web_url函数详解 by:授客 QQ:1033553122   加载指定url的web页面(GET请求) C语言函数 int web_url( const char *StepName ...