POJ——3264Balanced Lineup(RMQ模版水题)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 44112 | Accepted: 20713 | |
Case Time Limit: 2000MS |
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
- 6 3
- 1
- 7
- 3
- 4
- 2
- 5
- 1 5
- 4 6
- 2 2
Sample Output
- 6
- 3
- 0
代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdlib>
- #include<sstream>
- #include<cstring>
- #include<cstdio>
- #include<string>
- #include<deque>
- #include<stack>
- #include<cmath>
- #include<queue>
- #include<set>
- #include<map>
- #define INF 0x3f3f3f3f
- #define MM(x) memset(x,0,sizeof(x))
- using namespace std;
- typedef long long LL;
- const int N=50010;
- int n,q;
- int maxm[N][18],minm[N][18];
- void rmq()
- {
- int i,j;
- for (j=1; (1<<j)<=n; j++)
- {
- for (i=1; i+(1<<j)-1<=n; i++)
- {
- minm[i][j]=min(minm[i][j-1],minm[i+(1<<(j-1))][j-1]);
- maxm[i][j]=max(maxm[i][j-1],maxm[i+(1<<(j-1))][j-1]);
- }
- }
- }
- int main(void)
- {
- int i,j,l,r;
- while (~scanf("%d%d",&n,&q))
- {
- MM(maxm);MM(minm);
- for (i=1; i<=n; i++)
- {
- scanf("%d",&maxm[i][0]);
- minm[i][0]=maxm[i][0];
- }
- rmq();
- for (i=0; i<q; i++)
- {
- scanf("%d%d",&l,&r);
- int k=(int)log2(r-l+1);
- int ans=max(maxm[l][k],maxm[r-(1<<k)+1][k])-min(minm[l][k],minm[r-(1<<k)+1][k]);
- printf("%d\n",ans);
- }
- }
- return 0;
- }
POJ——3264Balanced Lineup(RMQ模版水题)的更多相关文章
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- poj 1004:Financial Management(水题,求平均数)
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
- POJ 3176 Cow Bowling (水题DP)
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
- poj 1658 Eva's Problem(水题)
一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...
- poj 3264 Balanced Lineup (RMQ算法 模板题)
RMQ支持操作: Query(L, R): 计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...
- ACM: POJ 1401 Factorial-数论专题-水题
POJ 1401 Factorial Time Limit:1500MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 1986 DIstance Query LCA水题
给出一棵树,对于每一个询问,给出2个节点,输出2个节点的距离. 输入中有字母,那个是没有用的,不用管. 思路: 0.选择编号为1的节点作为树的root (注意:有些题的边是单向的,这时候我们要根据节点 ...
随机推荐
- Distinct Values(贪心)
问题 D: Distinct Values 时间限制: 1 Sec 内存限制: 128 MB提交: 13 解决: 5[提交] [状态] [讨论版] [命题人:admin] 题目描述 Chiaki ...
- python_102_属性方法
# 属性方法:把一个方法变成一个静态属性 #1 class Dog(object): def __init__(self,name): self.name=name @property#属性 def ...
- C++ Primer读书笔记(一)第一篇:C++概述,第一章:开始
1. 主要内容 介绍程序语言的核心思想和C++的基本概念. 印象比较深刻的就是分而治之(divide and conque)的分解思想. 2. 知识广场 1) C++ 文件后缀 cc, cpp,,cx ...
- Windows10 关闭自动更新
win+R调出运行窗口: 输入services.msc,查找 跳出服务窗口,点击windows update设置禁用即可 Windows Update Medic Service没办法禁用,需要采用其 ...
- 如何用纯 CSS 创作一副国际象棋
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...
- verilog $fopen 函数的小缺陷
system task $fopen 的argument 为1.文件名字(可以包含具体的文件路径但是注意用)2.打开方式比如"r"."w"."a&qu ...
- GoF23种设计模式之结构型模式之代理模式
一.概述 为其他对象提供一种代理以控制对这个对象的访问. 二.适用性 1.远程代理(RemoteProxy):为一个对象在不同的地址空间土工局部代表. 2.虚代理(VirtualProxy):根据需要 ...
- cf 1016C
C. Vasya And The Mushrooms time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- list_for_each_entry()函数分析
list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...
- Flask-web开发
使用虚拟环境 虚拟环境使用第三方实用工具virtualenv创建.输入一下命令可以检查系统是否安装了virtualenv virtualenv --version 如果显示错误,你就需要安装这个工具. ...