1.题目描述:
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 51907 Accepted: 24341Case Time Limit: 2000MSDescription
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 2.题意概述:给出一串的数字,然后给出一个区间a b,输出从a到b的最大的数和最小的数的差
3.解题思路:
也是线段树的一个基本应用,又叫做RMQ问题。
4.AC代码:
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <functional> #include <cmath> #include <vector> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <ctime> #define INF 0x3f3f3f3f #define maxn 50010 #define lson root << 1 #define rson root << 1 | 1 #define N 1111 #define eps 1e-6 #define pi acos(-1.0) #define e exp(1.0) using namespace std; const int mod = 1e9 + 7; typedef long long ll; typedef unsigned long long ull; struct tree { int l, r, maxw, minw; } t[maxn * 4]; int h[maxn]; void pushup(int root) { t[root].maxw = max(t[lson].maxw, t[rson].maxw); t[root].minw = min(t[lson].minw, t[rson].minw); } void build(int l, int r, int root) { t[root].l = l; t[root].r = r; if (l == r) { t[root].maxw = t[root].minw = h[l]; return; } int mid = l + r >> 1; build(l, mid, lson); build(mid + 1, r, rson); pushup(root); } void update(int pos, int l, int r, int root) { if (l == r && l == pos) { t[root].maxw = t[root].minw = h[l]; return; } int mid = l + r >> 1; if (pos <= mid) update(pos, l, mid, lson); else update(pos, mid + 1, r, rson); pushup(root); } void query(int & a, int & b, int x, int y, int l, int r, int root) { if (x <= l && r <= y) { a = min(a, t[root].minw); b = max(b, t[root].maxw); return; } int mid = l + r >> 1; if (x <= mid) query(a, b, x, y, l, mid, lson); if (y > mid) query(a, b, x, y, mid + 1, r, rson); } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long _begin_time = clock(); #endif int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) scanf("%d", &h[i]); build(1, n, 1); while (m--) { int x, y; scanf("%d%d", &x, &y); int a = INF, b = 0; query(a, b, x, y, 1, n, 1); printf("%d\n", b - a); } } #ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms.", _end_time - _begin_time); #endif return 0; }