您的位置:首页 > 其它

POJ 3264 Balanced Lineup( 线段树&&RMQ )

2014-08-14 19:38 405 查看
Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 34331 Accepted: 16145
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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output
6
3
0

Source

RMQ一遍过,刚开始用数组写线段树超时一次,后来改成结构体就过了,所以对于同一个区间同时求最大和最小值,还是用结构体写线段树比较好

RMQ 2032ms   线段树1625ms

线段树代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define PI acos(-1,0)
using namespace std;
const int maxn = 110;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
const int N = 50010;
struct node
{
int left,right;
int num,num1; //
}T[4*N];
int ma=0,ni = 999999;
void pushup(int id)
{
T[id].num = max(T[id<<1].num,T[id<<1|1].num);
T[id].num1 = min(T[id<<1].num1,T[id<<1|1].num1);
}
void Creat(int left,int right,int id)
{
T[id].left =left;
T[id].right =right;
if(T[id].left ==T[id].right)
{
scanf("%d",&T[id].num);
T[id].num1 = T[id].num;
return ;
}
int m = (left+right)>>1;
Creat(lson);
Creat(rson);
pushup(id);
}

void query(int id,int l,int r)
{
int mid=(T[id].left +T[id].right)/2;
if(T[id].left ==l&&T[id].right ==r)
{
if(T[id].num >ma)
{
ma = T[id].num;
}
if(T[id].num1<ni)
ni = T[id].num1;
return;
}
if(r<=mid)
query(2*id,l,r);
else if(l>mid)
query(2*id+1,l,r);
else
{
query(2*id,l,mid);
query(2*id+1,mid+1,r);
}
}
int main()
{
int n,m,l,r;
while(scanf("%d%d",&n,&m)!=EOF)
{
Creat(1,n,1);
for(int i = 1;i<=m;i++)
{
scanf("%d%d",&l,&r);
ma = 0,ni = 9999999;
query(1,l,r);
printf("%d\n",ma-ni);
}
}
return 0;
}

RMQ (可做模板)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define PI acos(-1,0)
using namespace std;
const int maxn = 110;
const int maxm = 100001;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define MAX INT_MAX
#define MIN INT_MIN
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
const int N = 50010;
int maxl
[50], minl
[50];
int a
;

void S_T(int n)
{
int l = int(log((double)n)/log(2.0));
for (int j=1;j<=l;j++)
{
for (int i=1; i + (1 << (j-1) ) - 1 <=n;++i)
{
maxl[i][j] = max(maxl[i][j-1], maxl[i + (1 << (j-1) )][j-1]);
minl[i][j] = min(minl[i][j-1], minl[i + (1 << (j-1) )][j-1]);
}
}
}

void RMQ(int l, int r)
{
int k = (int)(log((double)(r-l+1))/log(2.0));
int a1 = max(maxl[l][k], maxl[r - (1<<k) + 1][k]);
int a2 = min(minl[l][k], minl[r - (1<<k) + 1][k]);
printf("%d\n",a1-a2);
}

int main()
{
int n,m;
while (~scanf("%d%d",&n,&m))
{
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxl[i][0] = minl[i][0] = a[i];
}
S_T(n);
int a, b;
while (m--)
{
scanf("%d%d",&a,&b);
RMQ(a,b);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: