[Usaco2007 Jan]Balanced Lineup
一道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的更多相关文章
- BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 933 Solved: 56 ...
- BZOJ1636: [Usaco2007 Jan]Balanced Lineup
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 476 Solved: 345[ ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- [Usaco2007 Jan]Balanced Lineup排队
[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...
- bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*
bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...
随机推荐
- VC++6.0调试:Watch窗口的使用
#include <stdio.h> #include <windows.h> class AutoExpand { public: AutoExpand(int val, c ...
- Windows程序
Windows程序是事件驱动的,对于一个窗口,它的大部分例行维护是由系统维护的.每个窗口都有一个消息处理函数.在消息处理函数中,对传入的消息进行处理.系统内还有它自己的缺省消息处理函数. 客户 ...
- sqlserver 删除表中数据 id 从1开始
TRUNCATE TABLE TbName --TbName是表名 但如果TbName中某些字段与其它表有主外键关系,会报错: 无法截断表 'Plants',因为该表正由 FOREIGN KE ...
- JdbcTemplate完全学习
概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTe ...
- Netty中消除开始的日志消息修改日志级别
我是使用logback作为日志打印,之前使用slf4j,日志打印不出,就干脆换掉了. 1.首先引入依赖 <dependency> <groupId>ch.qos.logback ...
- HDU4280(KB11-G 最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- a 链接点击下载
1. 将链接设置为.zip 结尾2.在a元素中添加download 属性,(目前只有chrome.firefox和opera支持) function download(src) { var $a = ...
- MySql 利用crontab实现MySql定时任务
MySql 利用crontab实现MySql定时任务 by:授客 QQ:1033553122 适用平台 任意myslq版本数据库 操作方法 登陆到数据库系统所在的linxu系统 第一步:新建名为cro ...
- View的draw机制
View:1.draw//绘制一个View以及他的子View.最好不要覆写该方法,应该覆写onDraw方法来绘制自己.public void draw(Canvas canvas); public v ...
- Django ModelForm类生成表单
1. 定义ModelForm类 #froms.py from django import forms from app01.modles import User class UserModelForm ...