TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1698
时间限制(普通/Java):5000MS/50000MS 内存限制:65536KByte
描述
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.
输入
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.
输出
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.
样例输入
6 3
1
7
3
4
2
5
1 5
4 6
2 2
样例输出
6
3
0
思路:题目大意就是,给n个数m个查询,下面行输入n个数。m行输入m个查询,查询最大值和最小值的差。
rmq-st模板题。拿来练手的。作为丢人的初学线段树选手,也附上手打的线段树代码。
RMQ-ST代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
int a[],dm[][],dx[][];
void rmq(int num){
for(int i = ; i < num ; i++){
dm[i][] = dx[i][] = a[i];
}
for(int j = ; (<<j) <= num ; j++){
for(int i = ;i+(<<j)- < num ;i ++){
dx[i][j] = max(dx[i][j-],dx[i+(<<(j-))][j-]);
dm[i][j] = min(dm[i][j-],dm[i+(<<(j-))][j-]);
}
}
}
int qmax(int st,int ed){
int k = ;
while((<<(k+))<= ed - st + )k++;
return max(dx[st][k],dx[ed - (<<k) + ][k]);
}
int qmin(int st,int ed){
int k = ;
while((<<(k+))<= ed - st + )k++;
return min(dm[st][k],dm[ed - (<<k) + ][k]);
}
int main(){
int n,k;
while(~scanf("%d %d",&n,&k)){
memset(dx,,sizeof(dx));
memset(dm,,sizeof(dm));
for(int i = ; i < n ; i++)scanf("%d",&a[i]);
rmq(n);
while(k--){
int x,y;
scanf("%d %d",&x,&y);
printf("%d\n",qmax(x-,y-)-qmin(x-,y-));
}
}
}
线段树代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = ;
struct note{
int l,r;
int nMin,nMax;
}segTree[maxn<<];
int Max,Min;
int a[maxn];
void build(int i,int l,int r){
segTree[i].l = l;
segTree[i].r = r;
if(l==r){
segTree[i].nMin = segTree[i].nMax = a[l];
return;
}
int mid = (l+r)>>;
build(i<<,l,mid);
build(i<<|,mid+,r);
segTree[i].nMax = max(segTree[i<<].nMax,segTree[i<<|].nMax);
segTree[i].nMin = min(segTree[i<<].nMin,segTree[i<<|].nMin);
}
void query(int i,int l,int r){
if(segTree[i].nMax <= Max && segTree[i].nMin >= Min){
return;
}
if(segTree[i].l == l && segTree[i].r == r){
Max = max(segTree[i].nMax,Max);
Min = min(segTree[i].nMin,Min);
return;
}
int mid = (segTree[i].l + segTree[i].r) >> ;
if(r <= mid)
query(i<<,l,r);
else if(l > mid)
query(i<<|,l,r);
else{
query(i<<,l,mid);
query(i<<|,mid+,r);
}
}
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
for(int i = ; i <= n ;i++)scanf("%d",&a[i]);
build(,,n);
while(m--){
int x,y;
Max = -;Min = ;
scanf("%d %d",&x,&y);
query(,x,y);
printf("%d\n",Max-Min);
}
}
}
TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- POJ3264Balanced Lineup 线段树练手
题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差 #include <iostream> #include ...
- POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- 线段树+RMQ问题第二弹
线段树+RMQ问题第二弹 上篇文章讲到了基于Sparse Table 解决 RMQ 问题,不知道大家还有没有印象,今天我们会从线段树的方法对 RMQ 问题再一次讨论. 正式介绍今天解决 RMQ 问题的 ...
- POJ - 3264 Balanced Lineup(线段树或RMQ)
题意:求区间最大值-最小值. 分析: 1.线段树 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
随机推荐
- Linux下部署URL重写
在将项目部署在Linux服务器后可以通过URL重写隐藏应用的入口文件index.php cd /etc/apache2 vi apache2.conf 修改以下内容 将none改成Alll 重启服务 ...
- php 分页实现 和 php 大文件上传失败的处理方式
1. php分页: 要想做php的分页,首先要弄清楚要什么样的分页,关系到哪些参数,参数之间怎么关联: 目标分页效果如下: 分析: 1.主要涉及两个参数:一个是当前输出页面的页码,用变量$p 表示,另 ...
- Servlet基本_クッキー、URLリライティング
1.クッキーの基礎クッキーは.クライアント側に保存されるテキストデータです. セキュリティ上の制約.・自分で発行したクッキーにしかアクセスできない.クッキーには発行元のホストの情報が記録されている.・ ...
- Delphi中QuotedStr介绍及使用
delphi 函数给字符串两边加单引号并返回.声明:function QuotedStr(const S: string): string;用函数 QuotedStr把字符串S转换成为用引号括起来的字 ...
- Ftp命令使用
FTP> ? 显示 ftp 命令说明.? 与 help 相同. 格式:? [command] 说明:[command] 指定需要帮助的命令名称.如果没有指定 command,ftp将显示全部命 ...
- mycat 多个逻辑库加读写分离
上3篇笔记主要记录了,读写分离,单库分表,分库分表 这次改动了下,实现了分库分表以及读写分离,基于多个逻辑库,先看配置文件 server.xml <user name="root&qu ...
- mssqlservers数据嗅探
SQL Server - 最佳实践 - 参数嗅探问题 转. 文章来自:https://yq.aliyun.com/articles/61767 先说我的问题,最近某个存储过程,暂定名字:sp_a ...
- 【367】通过 python 实现 SVM 硬边界 算法
参考: 支持向量机整理 SVM 硬边界的结果如下: $$min \quad \frac{1}{2} \sum_{i=1}^m\sum_{j=1}^m \alpha_i\alpha_jy_iy_j \v ...
- django 认证系统--1
django的认证系统提供认证和授权两种功能 认证系统包括如下部分: 1.Users 2.Permissions 主要是以 YES/NO 的形式反映一个用户是否能够做某事 3.Groups:就是对多个 ...
- Swift类型转换 和 类型别名的定义(typealias)
(一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...