点击打开题目
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
|---|---|---|
Total Submissions: 46323 | Accepted: 21730 | |
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 2Sample Output
6
3
0Source
USACO 2007 January Silver
用ST算法,预处理的复杂度为nlogn,1766ms跑完。要注意最后log的用法。
然后我又不信邪,用线段树写了一遍,C++1985ms跑完,也不算太慢哈。代码在最下面。
代码如下:(ST算法)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MAX 50000
int n,q;
int num[MAX+11];
int maxx[MAX+11][25];
int minn[MAX+11][25];
void RMQ()
{
for (int i = 1 ; i < 25 ; i++)
{
for (int j = 1 ; j <= n ; j++)
{
if (j + (1 << i) - 1 <= n)
{
maxx[j][i] = max(maxx[j][i-1] , maxx[j+(1<<(i-1))][i-1]);
minn[j][i] = min(minn[j][i-1] , minn[j+(1<<(i-1))][i-1]);
}
else
break;
}
}
}
int main()
{
while (~scanf ("%d %d",&n,&q))
{
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&num[i]);
maxx[i][0] = minn[i][0] = num[i];
}
RMQ();
while (q--)
{
int l,r;
scanf ("%d %d",&l,&r);
// int m = log2(r-l+1); //C++这么写会出问题
int m = log((double)r-l+1) / log(2.0); //这么写注意数据类型的转换
printf ("%d\n",max(maxx[l][m] , maxx[r-(1<<m)+1][m]) - min(minn[l][m] , minn[r-(1<<m)+1][m]));
}
}
return 0;
}代码如下:(线段树)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define L o<<1
#define R o<<1|1
#define MAX 50000
struct node
{
int l,r;
int maxx,minn;
}tree[MAX<<2];
void PushUp(int o)
{
tree[o].maxx = max (tree[L].maxx , tree[R].maxx);
tree[o].minn = min (tree[L].minn , tree[R].minn);
}
void Build(int o,int l,int r)
{
tree[o].l = l;
tree[o].r = r;
if (l == r)
{
scanf ("%d",&tree[o].maxx);
tree[o].minn = tree[o].maxx;
return;
}
int mid = (l + r) >> 1;
Build(L,l,mid);
Build(R,mid+1,r);
PushUp(o);
}
int Query_max(int o,int l,int r) //查询左右区间的最大值
{
if (tree[o].l == l && tree[o].r == r)
{
return tree[o].maxx;
}
int mid = (tree[o].l + tree[o].r) >> 1;
if (mid >= r)
return Query_max(L,l,r);
else if (mid < l)
return Query_max(R,l,r);
else
return max(Query_max(L,l,mid) , Query_max(R,mid+1,r));
}
int Query_min(int o,int l,int r) //查询左右区间的最大值
{
if (tree[o].l == l && tree[o].r == r)
return tree[o].minn;
int mid = (tree[o].l + tree[o].r) >> 1;
if (mid >= r)
return Query_min(L,l,r);
else if (mid < l)
return Query_min(R,l,r);
else
return min(Query_min(L,l,mid) , Query_min(R,mid+1,r));
}
int main()
{
int n,q;
while (~scanf ("%d %d",&n,&q))
{
Build(1,1,n); //创建线段树
while (q--)
{
int l,r;
scanf ("%d %d",&l,&r);
printf ("%d\n",Query_max(1,l,r) - Query_min(1,l,r));
}
}
return 0;
}