博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3264(RMQ或者线段树)
阅读量:5154 次
发布时间:2019-06-13

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

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 42929   Accepted: 20184
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 31734251 54 62 2

Sample Output

630

Source

题意:区间最大值与最小值之差RMQ版:()

#include 
#include
#include
#include
using namespace std;#define N 50010int a[N];int max_dp[N][20];int min_dp[N][20];int MAX(int i,int j){ if(i>=j) return i; return j;}int MIN(int i,int j){ if(i<=j) return i; return j;}void init_MAX_RMQ(int n){ for(int i=1;i<=n;i++) max_dp[i][0]=a[i]; for(int j=1;(1<
<=n;j++){ for(int i=1;i<=n-(1<

线段树:

#include 
#include
#include
#include
using namespace std;#define N 50010struct Tree{ int l,r; int Max,Min;}tree[4*N];int a[N];int MAX_VALUE;int MIN_VALUE;int MAX(int i,int j){ if(i>=j) return i; return j;}int MIN(int i,int j){ if(i<=j) return i; return j;}void PushUp(int idx){ tree[idx].Max = MAX(tree[idx<<1].Max,tree[idx<<1|1].Max); tree[idx].Min = MIN(tree[idx<<1].Min,tree[idx<<1|1].Min);}void build(int l,int r,int idx){ tree[idx].l = l; tree[idx].r = r; if(l==r) { tree[idx].Max = tree[idx].Min = a[l]; return ; } int mid=(l+r)>>1; build(l,mid,idx<<1); build(mid+1,r,idx<<1|1); PushUp(idx);}void query(int l,int r,int idx){ if(tree[idx].l==l&&tree[idx].r==r){ MAX_VALUE = MAX(MAX_VALUE,tree[idx].Max); MIN_VALUE = MIN(MIN_VALUE,tree[idx].Min); return; } int mid=(tree[idx].l+tree[idx].r)>>1; if(mid>=r) query(l,r,idx<<1); else if(mid

 

转载于:https://www.cnblogs.com/liyinggang/p/5346572.html

你可能感兴趣的文章
在vue项目npm run build后,index.html中引入css和js 报MIME type问题
查看>>
python的数据结构
查看>>
HTML5已成为主流的移动互联网云计算编程格式
查看>>
(转)从客户端中检测到有潜在危险的 Request.Form 值
查看>>
How to fix updating ubuntu apt-get problem
查看>>
以软件开发生命周期的过程来说明不同测试的使用情况
查看>>
Log Structured Merge Trees(LSM) 原理
查看>>
mysql中的事务
查看>>
Linux内核配置Kconfig语法
查看>>
NSQ:分布式的实时消息平台
查看>>
linux 开机启动nginx
查看>>
Java程序如何自动在线升级
查看>>
Exercise: A Routine Day
查看>>
判断点是否在多边形内
查看>>
ImageView
查看>>
consul ACL2
查看>>
C++深拷贝浅拷贝
查看>>
CSS 伪对象选择符: before、after
查看>>
深入剖析 Laravel 服务容器
查看>>
高并发IM系统架构优化实践
查看>>