博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ3264 (RMQのST解法)
阅读量:4306 次
发布时间:2019-06-06

本文共 2139 字,大约阅读时间需要 7 分钟。

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 31734251 54 62 2

Sample Output

630 题意,给出一串数字,有m个询问,询问l-r之间最大值和最小值的差. 好吧,这明显可以用线段树做....我知道....但可以离线实在让我忍不住了....不管了....st做法奉上.... 于是,收到了素质RE四连...

是是是,我以为网卡了,然后手贱直接交了4发,仔细一查原来j没算对,开大了.... 然后改一发A掉了

 

至于长度emmmm这只是暴力压行的结果,无视就行了.....

代码如下:

#include
#include
#include
#include
using namespace std;int dp1[200000][20],dp2[200000][20],a[200000];int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); dp1[i][0]=a[i]; dp2[i][0]=a[i]; } for(int j=1;j<=17;j++) { for(int i=1;i<=n;i++) { if(i+(1<
<=n) dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]); dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]); } } for(int i=1;i<=m;i++) { int l,r,ans=0; scanf("%d%d",&l,&r); int x=(int)(log((double)(r-l+1))/log(2.0)); int max1=max(dp1[l][x],dp1[r-(1<

 

果然ST看着比线段树短多了....

 

 

每天刷题,身体棒棒!

 

转载于:https://www.cnblogs.com/stxy-ferryman/p/7635817.html

你可能感兴趣的文章
Android音频系统之AudioPolicyService
查看>>
【计算机算法设计与分析】——5.4最优二分检索树
查看>>
不浮躁的社会是什么样的?
查看>>
KVM安装
查看>>
haproxy
查看>>
oracle中 rownum与rowid的理
查看>>
Linux之RPM 软件管理程序
查看>>
.NET 面试题(2)
查看>>
(转)java内部类详解
查看>>
mysql+mybatis递归调用
查看>>
mongoDB的安装(一)
查看>>
Spring Boot:快速入门教程
查看>>
「BZOJ2879」[Noi2012]美食节
查看>>
谈项目需求
查看>>
C#判断字符串是否为数字字符串
查看>>
I/O模型
查看>>
Spring Data JPA初使用 *****重要********
查看>>
xenu工具介绍
查看>>
hdu 5730 Shell Necklace——多项式求逆+拆系数FFT
查看>>
python try 异常处理 史上最全
查看>>