poj 3264 Balanced Lineup
2012-08-11 14:35
260 查看
这道题用线段树做,记录不同区间的的最大最小值,在搜索时从大区间开始搜索,具体看代码的注释!
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
Sample Output
代码:
#include<iostream>
using namespace std;
struct node
{
int l,r;
int max,min;
node *le,*re;
}tree[1000000];
int count;
int miin,maax;
void build(node *root,int l,int r) //建树
{
root->l=l;
root->r=r;
root->min=10000000;
root->max=-10000000;
if(l!=r)
{
count++;
root->le=tree+count;
count++;
root->re=tree+count;
build(root->le,l,(l+r)/2);
build(root->re,(l+r)/2+1,r);
}
}
void insert(node *root,int i,int a) //把数据插入
{
if(root->r==i && root->l==i)
{
root->max=root->min=a;
return ;
}
root->max=root->max>a?root->max:a;
root->min=root->min<a?root->min:a;
if(i<=((root->l+root->r)/2))
insert(root->le,i,a);
else
insert(root->re,i,a);
}
void query(node * root,int c,int d) //查找
{
if(root->min>=miin && root->max<=maax) //如果此前搜索的根的最大(小)值比现在记录的最大值小(大),就不用再往下搜索这个根了
return ;
if(c==root->l && d==root->r)
{
maax=root->max>maax?root->max:maax;
miin=root->min<miin?root->min:miin;
return ;
}
if(d<=((root->l+root->r)/2))
query(root->le,c,d);
else if(c>=((root->l+root->r)/2+1))
query(root->re,c,d);
else
{
query(root->le,c,(root->l+root->r)/2);
query(root->re,(root->l+root->r)/2+1,d);
}
}
int main()
{
int m,n,i,a,d,c;
scanf("%d%d",&m,&n); //如果用cin的话会超时
count=0;
build(tree,1,m);
for(i=1;i<=m;i++)
{
scanf("%d",&a);
insert(tree,i,a); //把数据插入到建好的树上,并且记录最大最小值;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&d);
miin=10000000;
maax=-10000000;
query(tree,c,d); //查找在cd段内的最大最小值;
printf("%d\n",maax-miin);
}
return 0;
}
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>
using namespace std;
struct node
{
int l,r;
int max,min;
node *le,*re;
}tree[1000000];
int count;
int miin,maax;
void build(node *root,int l,int r) //建树
{
root->l=l;
root->r=r;
root->min=10000000;
root->max=-10000000;
if(l!=r)
{
count++;
root->le=tree+count;
count++;
root->re=tree+count;
build(root->le,l,(l+r)/2);
build(root->re,(l+r)/2+1,r);
}
}
void insert(node *root,int i,int a) //把数据插入
{
if(root->r==i && root->l==i)
{
root->max=root->min=a;
return ;
}
root->max=root->max>a?root->max:a;
root->min=root->min<a?root->min:a;
if(i<=((root->l+root->r)/2))
insert(root->le,i,a);
else
insert(root->re,i,a);
}
void query(node * root,int c,int d) //查找
{
if(root->min>=miin && root->max<=maax) //如果此前搜索的根的最大(小)值比现在记录的最大值小(大),就不用再往下搜索这个根了
return ;
if(c==root->l && d==root->r)
{
maax=root->max>maax?root->max:maax;
miin=root->min<miin?root->min:miin;
return ;
}
if(d<=((root->l+root->r)/2))
query(root->le,c,d);
else if(c>=((root->l+root->r)/2+1))
query(root->re,c,d);
else
{
query(root->le,c,(root->l+root->r)/2);
query(root->re,(root->l+root->r)/2+1,d);
}
}
int main()
{
int m,n,i,a,d,c;
scanf("%d%d",&m,&n); //如果用cin的话会超时
count=0;
build(tree,1,m);
for(i=1;i<=m;i++)
{
scanf("%d",&a);
insert(tree,i,a); //把数据插入到建好的树上,并且记录最大最小值;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&d);
miin=10000000;
maax=-10000000;
query(tree,c,d); //查找在cd段内的最大最小值;
printf("%d\n",maax-miin);
}
return 0;
}
相关文章推荐
- POJ3264 Balanced Lineup 线段树|ST表
- poj 3264 Balanced Lineup ST
- poj 3264 Balanced Lineup(基础线段树)
- POJ 3264 Balanced Lineup(RMQ详解)
- POJ - 3264 - Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup(RMQ)
- [POJ 3264]Balanced Lineup(ST算法求RMQ)
- Balanced Lineup-POJ - 3264-RMQ线段树/st表
- POJ - 3264 Balanced Lineup解题报告(RMQ问题 ST算法 魔板题)
- 【线段树】poj 3264 Balanced Lineup(外:hdu 1754 I Hate It)
- POJ 3264 Balanced Lineup 区间查询(两棵树求最大最小值)
- poj 3264 Balanced Lineup[线段树,,水]
- poj 3264 Balanced Lineup ( ST算法(dp))
- poj 3264 Balanced Lineup(RMQ && 线段树)
- POJ3264——Balanced Lineup
- POJ 3264 Balanced Lineup (RMQ分析)
- 【POJ】3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup