您的位置:首页 > 其它

POJ 3264 Balanced Lineup 线段树 RMQ

2016-08-13 19:08 393 查看
Balanced Lineup

Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Submit

Status

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<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
const int MAX=150005;
struct node
{
int Max,Min;
};
int A[MAX];
node F[MAX*4];
int N,Q,a,b;
void build(int x,int le,int ri)
{
if (le==ri)
{
F[x].Max=A[le],F[x].Min=A[le];
return;
}
build(x*2,le,(le+ri)/2);
build(x*2+1,(le+ri)/2+1,ri);
F[x].Max=max(F[x*2].Max,F[x*2+1].Max);
F[x].Min=min(F[x*2].Min,F[x*2+1].Min);
}
int query1(int x,int le,int ri,int L,int R)
{
if (L>ri||R<le)
return -99999999;
if (le==ri)
return F[x].Max;
if (le>=L&&ri<=R)
return F[x].Max;
int mid=(le+ri)/2;
if (L>=le&&R<=mid)
return query1(x*2,le,mid,L,R);
else if (R<=ri&&L>=mid+1)
return query1(x*2+1,mid+1,ri,L,R);
else
return max(query1(x*2,le,mid,L,R),query1(x*2+1,mid+1,ri,L,R));
}
int query2(int x,int le,int ri,int L,int R)
{
if (L>ri||R<le)
return 99999999;
if (le==ri)
return F[x].Min;
if (le>=L&&ri<=R)
return F[x].Min;
int mid=(le+ri)/2;
if (L>=le&&R<=mid)
return query2(x*2,le,mid,L,R);
else if (R<=ri&&L>=mid+1)
return query2(x*2+1,mid+1,ri,L,R);
else
return min(query2(x*2,le,mid,L,R),query2(x*2+1,mid+1,ri,L,R));
}
int main()
{
while (scanf("%d%d",&N,&Q)!=EOF)
{
for (int i=1; i<=N; i++)
scanf("%d",&A[i]);
build(1,1,N);
for (int i=1; i<=Q; i++)
{
scanf("%d%d",&a,&b);
printf("%d\n",query1(1,1,N,a,b)-query2(1,1,N,a,b));
}
}
return 0;
}

//RMQ

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
using namespace std;
const int MAX = 200001;
int Fmax[MAX][20],Fmin[MAX][20];
int n, q;
void init()
{
int i, j, lg = floor(log10(double(n))/log10(double(2)));
for (j = 1; j <= lg; ++j)
{
for (i = 1; i <= n+1-(1<<j); ++i)
{
Fmax[i][j] = max(Fmax[i][j-1], Fmax[i+(1<<(j-1))][j-1]);
Fmin[i][j] = min(Fmin[i][j-1], Fmin[i+(1<<(j-1))][j-1]);
}
}
}

int main()
{
int h, i, a, b, lg;
scanf("%d%d", &n, &q);
for (i = 1; i <= n; ++i)
{
scanf("%d", &h);
Fmax[i][0] = Fmin[i][0] = h;
}
init();
for (i = 1; i <= q; ++i)
{
scanf("%d%d", &a, &b);
if (a > b) swap(a, b);
lg = floor(log10(double(b-a+1))/log10(double(2)));
printf("%d\n", max(Fmax[a][lg], Fmax[b-(1<<lg)+1][lg]) -
min(Fmin[a][lg], Fmin[b-(1<<lg)+1][lg]));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: