poj 3264 RMQ 水题
题意:找到一段数字里最大值和最小值的差
水题
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
int n,m,t;
const int MAXN = ;
int dpMAX[MAXN][],dpMIN[MAXN][];
int mm[MAXN];
int cow[MAXN];
//初始化RMQ, b数组下标从1开始,从0开始简单修改
void makemaxRMQ(int n,int b[])
{
for(int i=;i<=n;i++)
dpMAX[i][]=b[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dpMAX[i][j]=max(dpMAX[i][j-],dpMAX[i+(<<(j-))][j-]);
}
int rmqMAX(int s,int v)
{
int k=(int)(log(v-s+1.0)/log(2.0));
return max(dpMAX[s][k],dpMAX[v-(<<k)+][k]);
}
void makeminRMQ(int n,int b[])
{
for(int i=;i<=n;i++)
dpMIN[i][]=b[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dpMIN[i][j]=min(dpMIN[i][j-],dpMIN[i+(<<(j-))][j-]);
}
int rmqMIN(int s,int v)
{
int k=(int)(log(v-s+1.0)/log(2.0));
return min(dpMIN[s][k],dpMIN[v-(<<k)+][k]);
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int q;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%d",&cow[i]);
}
makemaxRMQ(n,cow);
makeminRMQ(n,cow);
for(i=;i<=q;i++)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",rmqMAX(a,b)-rmqMIN(a,b));
}
}
return ;
}
poj 3264 RMQ 水题的更多相关文章
- POJ 3264 RMQ水题
题目大意就是有很多牛.告诉你每只牛的高度.然后有很多个询问.输出该区间内的最大身高差.也就是用RMQ求最大值最小值.貌似还可以用线段树.然而,我还不会线段树.....T_T 可能是太多组数据了.cin ...
- POJ 3264 RMQ裸题
POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- Poj 1552 Doubles(水题)
一.Description As part of an arithmetic competency program, your students will be given randomly gene ...
- poj 3264 RMQ
直接写个RMQ就能过. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- 最小费用最大流模板 poj 2159 模板水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15944 Accepted: 8167 Descr ...
- POJ 1837 Balance 水题, DP 难度:0
题目 http://poj.org/problem?id=1837 题意 单组数据,有一根杠杆,有R个钩子,其位置hi为整数且属于[-15,15],有C个重物,其质量wi为整数且属于[1,25],重物 ...
- poj 3264(RMQ或者线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42929 Accepted: 20184 ...
- POJ - 3090 gcd水题
大概题意就是求\(1 \le i,j \le n\)的\(gcd(i,j) = 1\)的个数+2(对于0的特判) 正解应该是欧拉函数或者高逼格的莫比乌斯反演 但数据实在太水直接打表算了 /*H E A ...
随机推荐
- js正则表达式中的问号几种用法小结
这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...
- SYN攻击TIME_WAIT防御。
#! /bin/bash /bin/netstat -anput | grep TIME_WAIT >time_wait.txt /bin/cat time_wait.txt |awk '{pr ...
- Linux 学习笔记(一)
Linux体系结构 下面是Linux体系结构的示意图: 在所有Linux版本中,都会涉及到以下几个重要概念: 内核:内核是操作系统的核心.内核直接与硬件交互,并处理大部分较低层的任务,如内存管理.进程 ...
- struts+spring action应配置为scope="prototype"
truts+spring action应配置为scope="prototype" <bean id="personAction" scope=" ...
- svn切换用户
问题背景:你用一个用户更新了代码,此时想用另一个用户提交,这就涉及到一个svn切换用户的问题 1. 查看svn 的用户名,密码:找到用户名,密码文件,都是明文的,你可以看到例:linuxhjj@hjj ...
- Entity Framework CodeFirst尝试
前言 Code First模式我们称之为“代码优先”模式,是从EF4.1开始新建加入的功能.使用Code First模式进行EF开发时开发人员只需要编写对应的数据类(其实就是领域模型的实现过程),然后 ...
- HDU 4857 Couple doubi(找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4861 解题报告:桌子上有k个球 ,第i个球的价值wi = 1^i+2^i+...+(p-1)^i (m ...
- 在Linux上使用的10种云备份方案
导读 不久前,为用户提供一种备份远程机器上数据的简易方法还很稀奇.现在,我们已觉得这理所当然.Dropbox及其他公司简化了这项任务.苹果.谷歌和微软都提供各自的数据备份方法. 在Linux上,情况有 ...
- xcode Git
http://blog.csdn.net/w13770269691/article/details/38704941 在已有的git库中搭建新库,并且将本地的git仓库,上传到远程服务器的git库中, ...
- The Flash
flash.now[:error] = "" render :new flash[:error] = "" redirect videos_path http: ...