POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差。
解法一:这个是最初的解法,时间上可能会超时,下面还有改进算法.4969ms
#include<stdio.h>
#include<stdlib.h>
#define INF 1000010
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a)) typedef struct res
{
int mx,mi;
} pair; typedef struct node
{
struct node *lc,*rc;
int ld,rd;
pair res;
} node; node *buildTree(int a,int b)
{
node*p=(node*)malloc(sizeof(node));
p->ld=a;
p->rd=b;
p->res.mx=;
p->res.mi=INF;
p->lc=p->rc=NULL;
if(a==b)
return p;
p->lc=buildTree(a,(a+b)>>);
p->rc=buildTree(((a+b)>>)+,b);
return p;
} void insert(node*T,int pos,int key)
{
int mid=(T->rd+T->ld)>>;
if(T->rd==T->ld)
{
T->res.mx=T->res.mi=key;
return;
}
if(pos<=mid)
insert(T->lc,pos,key);
else insert(T->rc,pos,key);
T->res.mx=max(T->lc->res.mx,T->rc->res.mx);
T->res.mi=min(T->lc->res.mi,T->rc->res.mi);
return;
} pair search(node*T,int a,int b)
{
pair temp1,temp2,ans;
int mid=(T->rd+T->ld)>>; temp1.mx=temp2.mx=;
temp1.mi=temp2.mi=INF;
if(a<=T->ld&&T->rd<=b)
return T->res;
if(a<=mid)
temp1=search(T->lc,a,b);
if(b>mid)
temp2=search(T->rc,a,b);
ans.mx=max(temp1.mx,temp2.mx);
ans.mi=min(temp1.mi,temp2.mi);
return ans;
} int main(void)
{
int n,q,i,a,b;
pair ans;
scanf("%d%d",&n,&q);
node *head=buildTree(,n);
for(i=; i<=n; i++)
{
scanf("%d",&a);
insert(head,i,a);
}
for(i=; i<=q; i++)
{
scanf("%d%d",&a,&b);
ans=search(head,a,b);
printf("%d\n",ans.mx-ans.mi);
}
return ;
}
解法二:
利用数组的形式:3704ms
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000010
#define INF 1000010
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a)) int l[N],r[N],i,n;
typedef struct
{
int mx,mi;
} pair;
pair res[N]; void BuildTree(int x,int y,int p)
{
l[p]=x;
r[p]=y;
if(x==y)
return;
BuildTree(x,(x+y)>>,*p);
BuildTree(((x+y)>>)+,y,*p+);
} void Insert(int num,int p)
{
int mid=(l[p]+r[p])>>;
res[p].mx=max(res[p].mx,num);
res[p].mi=min(res[p].mi,num);
if(l[p]==r[p])
return;
if(i<=mid)
Insert(num,*p);
else Insert(num,*p+);
} pair search(int x,int y,int p)
{
pair a1,a2,ans;
a1.mx=a2.mx=;
a1.mi=a2.mi=INF;
int mid=(l[p]+r[p])>>;
if(x<=l[p]&&y>=r[p])
return res[p];
if(x<=mid)
a1=search(x,y,*p);
if(y>mid)
a2=search(x,y,*p+);
ans.mx=max(a1.mx,a2.mx);
ans.mi=min(a1.mi,a2.mi);
return ans;
} int main(void)
{
int q,j,x,y;
pair ans;
memset(res,,sizeof(res));
scanf("%d%d",&n,&q);
BuildTree(,n,);
for(i=; i<N; i++)
res[i].mi=INF;
for(i=; i<=n; i++)
{
scanf("%d",&j);
Insert(j,);
} for(i=; i<q; i++)
{
scanf("%d%d",&x,&y);
ans=search(x,y,);
printf("%d\n",ans.mx-ans.mi);
}
return ;
}
解法三:
这是利用别人的算法改进而来,所以有时候多借鉴别人的代码,并且可以和自己的想法结合起来,效果还不错.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 50010
#define INF 1000010
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a)) typedef struct
{
int l,r;
int mx,mi;
} pair;
pair temp[N*];
int Max,Min; void BuildTree(int x,int y,int p)
{
temp[p].l=x;
temp[p].r=y;
temp[p].mx=;
temp[p].mi=INF;
if(x==y)
return;
int mid=(x+y)/;
BuildTree(x,mid,p*);
BuildTree(mid+,y,*p+);
} void Insert(int v,int num,int p)
{
if(temp[v].l==temp[v].r)
{
temp[v].mx=temp[v].mi=num;
return ;
}
int mid=(temp[v].r+temp[v].l)/;
if(p<=mid)
{
Insert(*v,num,p);
temp[v].mx=max(temp[v].mx,temp[*v].mx);
temp[v].mi=min(temp[v].mi,temp[*v].mi);
}
else
{
Insert(*v+,num,p);
temp[v].mx=max(temp[v].mx,temp[*v+].mx);
temp[v].mi=min(temp[v].mi,temp[*v+].mi);
}
} void search(int x,int y,int p)
{
if(temp[p].l==x&&temp[p].r==y)
{
Max=max(Max,temp[p].mx);
Min=min(Min,temp[p].mi);
return;
}
int mid=(temp[p].l+temp[p].r)/;
if(x>mid)
{
search(x,y,*p+);
}
else if(y<=mid)
{
search(x,y,*p);
}
else
{
search(x,mid,*p);
search(mid+,y,*p+);
}
return;
} int main(void)
{
int n,q,i,x,y;
scanf("%d%d",&n,&q);
BuildTree(,n,);
for(i=;i<=n;i++)
{
scanf("%d",&x);
Insert(,x,i);
}
for(i=;i<q;i++)
{
scanf("%d%d",&x,&y);
Max=;
Min=INF;
search(x,y,);
printf("%d\n",Max-Min);
}
return ;
}
POJ - 3264 Balanced Lineup 线段树解RMQ的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup -- RMQ或线段树
一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
随机推荐
- elasticsearch学习一、安装和配置
原文链接:http://jingyan.baidu.com/article/48206aead42b53216bd6b372.html ElasticSearch是一个基于Lucene的搜索服务器.它 ...
- 数据库hang住 处理过程
当数据库出现严重的性能问题或者hang了的时候,我们非常需要通过systemstate dump来知道进程在做什么,在等待什么,谁是资源的持有者,谁阻塞了别人.在出现上述问题时,及时收集systems ...
- MAC itunes无法验证服务器s.mzstatic/itunes无法更新服务器解决方案
打开host文件: 一.用终端打开: sudo vi /etc/hosts 输入完这行命令后需要输入电脑密码,然后确认,进入host文件 然后按i键进入编辑模式,在最后一行添加:23.214.233. ...
- iOS 获取设备型号以及IP地址
首先导入四个头文件 #include <sys/types.h> #include <sys/sysctl.h> #include <ifaddrs.h> #inc ...
- ios专题 - 图片(UIImage)获取方法
说到图片获取的方法,就得看API文档. UIImage生成实例的方法有: 1)imageNamed 从指定文件返回对象. 这个方法有个比较特殊的地方:该方法首先从系统缓存中寻找该图片,如果有,则从缓存 ...
- javascript 布局 第20节
<html> <head> <title>页面布局</title> <style type="text/css"> bo ...
- JavaScript使用技巧
使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是否有一个有效值,如果存在就返回true值.为了做这样的验证,我们可以使用!!操作符来实现是非常的方便与简单.对于变量可以使用 ...
- [001] winnie the pooh - 读后记
winnie the pooh 我是在伍君仪透析英语视频培训班,获得这本书的,PDF格式的(排版不是很好,和当当上的相比有部分章节缺失) 这是我第一本采用透析法读完的英文书. 今天(2015年10月2 ...
- UVA 11995 I Can Guess the Data Structure!(ADT)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- UVA 12097 LA 3635 Pie(二分法)
Pie My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numbe ...