POJ3264(KB7-G RMQ)
Balanced Lineup
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
Source
//2017-05-17
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
int a[N], DPmin[N][], DPmax[N][]; void init(int n)
{
for(int j = ; j<=(int)log2(n); j++)
for(int i = ; i<=n; i++){
DPmax[i][j] = DPmax[i][j-];
DPmin[i][j] = DPmin[i][j-];
if(i+(<<j)- <= n){
DPmax[i][j] = max(DPmax[i][j-], DPmax[i+(<<(j-))][j-]);
DPmin[i][j] = min(DPmin[i][j-], DPmin[i+(<<(j-))][j-]);
}
}
} int query(int l, int r)
{
int k = (int)log2(r-l+);
return max(DPmax[l][k], DPmax[r-(<<k)+][k])-min(DPmin[l][k], DPmin[r-(<<k)+][k]);
} int main()
{
int n, q;
while(scanf("%d%d", &n, &q)!=EOF)
{
for(int i = ; i <= n; i++){
scanf("%d", &DPmax[i][]);
DPmin[i][] = DPmax[i][];
}
init(n);
int l, r;
while(q--)
{
scanf("%d%d", &l, &r);
printf("%d\n", query(l, r));
}
} return ;
}
POJ3264(KB7-G RMQ)的更多相关文章
- POJ3264 Balanced Lineup [RMQ模板]
题意:有n头牛,输入他们的身高,求某区间身高的极值的差(max-min), 用RMQ模板,同时构造求极大值和极小值的两个数组. //poj3264 #include <iostream> ...
- [POJ3264]Balanced Lineup(RMQ, ST算法)
题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- HDU 5869 Different GCD Subarray Query
离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- 【POJ3264】Balanced Lineup(RMQ)
题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛 ...
- RMQ、POJ3264
这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...
- (WAWAWAWAWAWAW) G. Periodic RMQ Problem
没有联通门 : Codeforces G. Periodic RMQ Problem /* Codeforces G. Periodic RMQ Problem MMP 什么动态开点线段树啊 ... ...
- POJ3264/RMQ
题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...
随机推荐
- [学习笔记]树形dp
最近几天学了一下树形\(dp\) 其实早就学过了 来提高一下打开树形\(dp\)的姿势. 1.没有上司的晚会 我的人生第一道树形\(dp\),其实就是两种情况: \(dp[i][1]\)表示第i个人来 ...
- Redis中的批量操作Pipeline
大多数情况下,我们都会通过请求-相应机制去操作redis.只用这种模式的一般的步骤是,先获得jedis实例,然后通过jedis的get/put方法与redis交互.由于redis是单线程的,下一次请求 ...
- webpack快速入门——实战技巧:watch的正确使用方法,webpack自动打包
随着项目大了,后端与前端联调,我们不需要每一次都去打包,这样特别麻烦,我们希望的场景是,每次按保存键,webpack自动为我们打包,这个工具就是watch! 因为watch是webpack自带的插件, ...
- vue 3D小球 loading
<template> <div class="load"> <div class="loadEffect"> <spa ...
- 收藏一篇关于Asp.net Response.Filter的文章
Capturing and Transforming ASP.NET Output with Response.Filter https://weblog.west-wind.com/posts/20 ...
- 集合框架_DAY17
1:五种数据结构: 数组:长度固定,有序,查找方便 链表:添加删除方便 栈:先进后出 队列:先进先出 树结构:完成排序动作 2:泛型(了解) (1)是一种把明确数据类型的工作推迟到创 ...
- spring mvc 数据转换
项目目录结构 User.java package org.mythsky.springmvcdemo.model; import org.springframework.format.annotati ...
- 微信小程序网络封装-简单高效
废话引言 小程序虽然出世很久了,但一直没怎么接触到小程序开发.吉他兴趣班老师想弄一个小程序发布课程信息和打卡功能,作为IT一员就自愿加入了这个小程序开发小组中.虽然小程序面向的是前端工程师,但作为移动 ...
- Editplus php
一.配置PHP帮助手册 1.打开Editplus,[工具]-->[配置用户工具],在[添加工具]子菜单下选择[HTML帮助(*.chm)(T)],文件路径选择php的chm帮助文件路径. 这样在 ...
- 快速选择算法/Select 寻找第k大的数
参考算法导论9.3节的内容和这位大神的博客:http://blog.csdn.net/v_JULY_v上对这一节内容代码的实现进行了学习 尝试实现了以查找中位数为前提的select算法. 算法功能:可 ...