poj3264Balanced Lineup(倍增ST表)

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 52328 Accepted: 24551
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

Line 1: Two space-separated integers, N and Q
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

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

//先敲个板子
#include<iostream>
#include<cstdio>
#include<cstring>#define maxn 1000000using namespace std;
int n,m,ans,x,y,a[maxn],p[maxn];
int f1[maxn][25],f2[maxn][25];inline int init()
{int x=0,f=1;char c=getchar();while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return x*f;
}int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}void ST()
{for(int i=1;i<=n;i++)f1[i][0]=f2[i][0]=a[i];for(int j=1;j<=20;j++)for(int i=1;i+(1<<j)-1<=n;i++){f1[i][j]=min(f1[i][j-1],f1[i+(1<<j-1)][j-1]);f2[i][j]=max(f2[i][j-1],f2[i+(1<<j-1)][j-1]);}for(int i=1;i<=n;i++)for(int j=0;j<=20;j++)if((1<<j)>i){p[i]=j-1;break;                }
}int query(int l,int r)
{int k=p[r-l+1];int ans1=min(f1[l][k],f1[r-(1<<k)+1][k]);int ans2=max(f2[l][k],f2[r-(1<<k)+1][k]);return ans2-ans1;
}int main()
{n=init();m=init();for(int i=1;i<=n;i++)a[i]=init();ST();for(int i=1;i<=m;i++){x=init();y=init();printf("%d\n",query(x,y));}return 0;
}

 

转载于:https://www.cnblogs.com/L-Memory/p/6815944.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254688.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LightOJ1283 Shelving Books(DP)

题目 Source http://www.lightoj.com/volume_showproblem.php?problem1283 Description You are a librarian. You keep the books in a well organized form such that it becomes simpler for you to find a book and even they look better in the shelves. One day you ge…

量子传输技术转移一个人需要4500万亿年

看过《星际迷航》的朋友一定不会忘记这句经典的台词&#xff1a;斯科蒂&#xff0c;将我传输过去&#xff01;其中涉及到量子隐形传输的技术&#xff0c;可以把物体从三维时空一处传输到另一处。但可惜的是&#xff0c;这种看着非常炫的技术或许根本无法实现。 到目前为止&…

使用OutputDebugString帮助调试

使用OutputDebugString帮助调试 前面我已经介绍了使用TRACE来帮助我们调试&#xff0c;但使用TRACE有一个限制&#xff0c;只能在将程序DEBUG编译状态下才能使用&#xff0c;下面我们介绍OutputDebugString函数&#xff0c;通过它&#xff0c;可以在在DEBUG或RELEASE情况也可以…

leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)

题目描述&#xff1a; You are given a string representing an attendance record for a student. The record only contains the following three characters: A : Absent.L : Late.P : Present.A student could be rewarded if his attendance record doesnt contain more t…

简单实现

1.创建接口和实现类 &#xff08;模拟实现查询天气&#xff09; 接口&#xff1a; package com.learning.weather;/*** * weather 接口 &#xff1a;实现模拟wsdl*/ public interface WeatherInterface {/*** 查询天气* param name* return*/public String queryWeather(Strin…

halcon联合C#测量十字Mark中心

halcon联合C#测量十字Mark中心 函数说明 public void FitRectangleMeasure(HWindow 窗口句柄, HImage 图像, out double 中心Y坐标, out double 中心X坐标)操作步骤&#xff0c;首先绘制两个矩形测量框&#xff1b;之后就可进行自动计算。 public void FitRectangleMeasure(…

x264 struct 学习

x264_t结构体维护着CODEC的诸多重要信息其中成员frames是一个指示和控制帧编码过程的结构。其中current是已经准备就绪可以编码的帧&#xff0c;其类型已经确定&#xff1b;next是尚未确定类型的帧&#xff1b;unused用于回收不使用的frame结构体以备今后再次使用。 structx26…

单例模式的新实现

单例模式的新实现 jdk1.5 之前 单例模式的两种方式&#xff0c;两种方法都是要把构造器保持私有的&#xff0c;并导出公有的静态成员&#xff0c;以便允许客户端能够访问该类的唯一实例。 第一种方法中&#xff0c;公有的静态成员是个final域: //Singleton with public final f…

有关莫比乌斯反演

对于两个定义域为整数的函数F(x)和f(x); 若有: 然后F(x)可以快速求出&#xff1b; 如何用F求解f呢&#xff1f; 莫比乌斯反演&#xff1a; 对于两个定义域为整数的函数F(x)和f(x); 若有: 则有&#xff1a; 其中μ(x)为莫比乌斯函数&#xff0c;其定义为&#xff1a; 对于&#…

(原创)JS点击事件——Uncaught TypeError: Cannot set property 'onclick' of null

html部分代码&#xff1a; JS部分代码&#xff1a; 需要实现的效果&#xff1a;点击图片&#xff0c;来回相互切换。 我开始的错误做法&#xff1a;代码如上图所示&#xff08;逻辑上看起来是没有错误的&#xff09; 尝试过程&#xff1a;把JS代码放在</body>闭合标签之前…

事务传播机制/数据库异常解析——2016-8-13分享总结

一. 事务的传播机制&#xff0f;required 跟 required new 的使用与区别 基础回顾 1.1 事务的隔离级别&#xff1a; ISOLATION_READ_UNCOMMITTED&#xff08;读未提交&#xff09; ISOLATION_READ_COMMITTED&#xff08;读已提交&#xff09; ISOLATION_REPEATABLE_READ&#x…

console类详细解释

console类详细解释 微软链接https://docs.microsoft.com/zh-cn/dotnet/api/system.console?viewnetframework-4.8 C#中没有标准输入输出关键字&#xff0c;要调用console类下的方法。 练习与解释代码 using System; using System.Collections.Generic; using System.Linq; …

VC下调用x264进行视频编码,

4.X264.c中,h x264_encoder_open( param ) )是用来复制参数并验证参数的有效性,在CCS下应该是不需要验证参数的(参数都是在程序中设置好的),因此此处只作复制参数param和初始化X264_T h的操作.(VC下程序修改记录080106下午)修改COMMON.C中的void x264_param_default( x264_…

UploadRTOS.exe

UploadRTOS.exe类似于一个启动并为VxWin运行做准备的工具程序。VxWin安装之后&#xff0c;可以使用 上传工具程序 启动实时操作系统。 利用命令行参数,您可以使它执行不同的功能。该 上传工具程序 包含两个文件: UploadRTOS.exe (命令行程序) UploadR…

20155307 2016-2017 《Java程序设计》第三次实验报告

&#xff08;一&#xff09;敏捷开发与XP 敏捷开发是一种以人为核心、迭代、循序渐进的开发方法。“敏捷流程”是一系列价值观和方法论的集合。从2001年开始&#xff0c;一些软件界的专家开始倡导“敏捷”的价值观和流程&#xff0c;他们肯定了流行做法的价值&#xff0c;但是强…

ElasticSearch创建、修改、获取、删除、索引Indice mapping和Index Template案例

为什么80%的码农都做不了架构师&#xff1f;>>> The best elasticsearch highlevel java rest api-----bboss ElasticSearch客户端框架bboss的ClientInterface 接口提供了创建/修改、获取、删除索引Indice和IndexTemplate的方法&#xff0c;本文举例说明其使用方法…

ASCII码与string的相互转换

ASCII码与string的相互转换 思路&#xff1a; 1&#xff09;ASCII码转string&#xff1a;把字符&#xff08;串&#xff09;直接转换为int类型&#xff0c;即可得到ASCII码&#xff1b; 2&#xff09;string转ASCII码&#xff1a;将数字转换为字符串转出&#xff1b; {//将字…

X264代码中一些参数的意义

Main&#xff08;int argc&#xff0c;char *argv[]&#xff09;; 为了方便起见&#xff0c;不妨改写为&#xff1a; Main(void){ ...... intargc5; char*argv[]{ "main","-o","test.264","foreman.yuv","352x288" }; …

spring mvc注解@RequestMapping

1、url路径映射 基本功能 2、窄化请求映射 根路径子路径 注意setViewName的路径。 3、限制http请求方法 get和 post 如果是get 转载于:https://www.cnblogs.com/jway1101/p/5773923.html

Start application automatically during controller boot-up

&#xfeff;&#xfeff; Tip English •German Start application automatically during controller boot-up Description It is possible to start any program automatically during the boot-up procedure of the KR C4 controller. Precondition •User group “Exper…