G - Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
POJ 3264
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
题意:第一行给出N和Q,接下来N行,是输入的数据。接下来Q行,每行一个区间范围,对于每行Q输出区间范围内最大最小值的差。
裸的线段树,没什么说的
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=50005;//最大数据数量
const int INF=99999999;
struct node
{
int left;//当前结点对应区间左右边界
int right;
int max_num;//当前结点对应区间范围内最大数
int min_num;//当前结点对应区间范围内最小数
} Tree[maxn<<2];
int num[maxn];//接收数据
int N;//数据数量
int Q;//比较次数
int max_num;//区间最值
int min_num;
void Build(int root,int left,int right)
{
Tree[root].left=left;//记录root结点对应区间
Tree[root].right=right;
if(left==right)//树的底层记录数据
{
Tree[root].min_num=Tree[root].max_num=num[left];
return;
}
int mid=(left+right)>>1;
Build(root<<1,left,mid);
Build(root<<1|1,mid+1,right);
Tree[root].max_num=max(Tree[root<<1].max_num,Tree[root<<1|1].max_num);
Tree[root].min_num=min(Tree[root<<1].min_num,Tree[root<<1|1].min_num);
//上浮最值数据
}
void Query(int root,int left,int right,int find_left,int find_right)
{
if(max_num>=Tree[root].max_num&&min_num<=Tree[root].min_num)
return;
if(find_left==left&&find_right==right)
{
max_num=max(max_num,Tree[root].max_num);
min_num=min(min_num,Tree[root].min_num);
return;
}
int mid=(left+right)>>1;
if(find_right<=mid)
Query(root<<1,left,mid,find_left,find_right);
else if(find_left>mid)
Query(root<<1|1,mid+1,right,find_left,find_right);
else
{
Query(root<<1,left,mid,find_left,mid);
Query(root<<1|1,mid+1,right,mid+1,find_right);
}
}
int main()
{
scanf("%d%d",&N,&Q);
for(int i=1; i<=N; i++)
scanf("%d",&num[i]);
Build(1,1,N);
int left_num,right_num;
while(Q--)
{
scanf("%d%d",&left_num,&right_num);
max_num=-INF;
min_num=INF;//初始化最值
Query(1,1,N,left_num,right_num);
printf("%d\n",max_num-min_num);
}
return 0;
}
又做了一次
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=50005;//最大数据数量
const int INF=99999999;
struct node
{
int min_num;
int max_num;
}Tree[maxn<<2];
int N;//N个数据
int Q;//Q次查询
void Build(int root,int left,int right)
{
if(left==right)
{
scanf("%d",&Tree[root].max_num);
Tree[root].min_num=Tree[root].max_num;
return;
}
int mid=(left+right)>>1;
Build(root<<1,left,mid);
Build(root<<1|1,mid+1,right);
Tree[root].max_num=max(Tree[root<<1].max_num,Tree[root<<1|1].max_num);
Tree[root].min_num=min(Tree[root<<1].min_num,Tree[root<<1|1].min_num);
}
int Query_max(int root,int left,int right,int find_left,int find_right)
{
if(find_left<=left&&find_right>=right)
return Tree[root].max_num;
int mid=(left+right)>>1;
if(find_right<=mid)
return Query_max(root<<1,left,mid,find_left,find_right);
else if(find_left>mid)
return Query_max(root<<1|1,mid+1,right,find_left,find_right);
else
return max(Query_max(root<<1,left,mid,find_left,mid),Query_max(root<<1|1,mid+1,right,mid+1,find_right));
}
int Query_min(int root,int left,int right,int find_left,int find_right)
{
if(find_left<=left&&find_right>=right)
return Tree[root].min_num;
int mid=(left+right)>>1;
if(find_right<=mid)
return Query_min(root<<1,left,mid,find_left,find_right);
else if(find_left>mid)
return Query_min(root<<1|1,mid+1,right,find_left,find_right);
else
return min(Query_min(root<<1,left,mid,find_left,mid),Query_min(root<<1|1,mid+1,right,mid+1,find_right));
}
int main()
{
scanf("%d%d",&N,&Q);
Build(1,1,N);
int left,right;
while(Q--)
{
scanf("%d%d",&left,&right);
printf("%d\n",Query_max(1,1,N,left,right)-Query_min(1,1,N,left,right));
}
return 0;
}