Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 32777 Accepted: 15424
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
USACO 2007 January Silver
题意:求给定Q(1<=Q<=200000)个数a1,a2...aq,多次求任意区间内ai-aj中最大数和最小数的差;
思路:线段树查询;
AC代码:
#include<stdio.h> #include<string.h> #define MAX_INF 9999999 #define MIN_INF -9999999 struct Node { int l,r; int max,min; }a[200005]; int max(int n,int m) { return n>m?n:m; } int min(int n,int m) { return n<m?n:m; } void Build(int n,int l,int r) { a[n].l=l; a[n].r=r; a[n].max=MIN_INF; a[n].min=MAX_INF; if(l==r) return; Build(2*n,l,(l+r)/2); Build(2*n+1,(l+r)/2+1,r); } void Insert(int n,int v,int num) { if(a[n].max<num) a[n].max=num; if(a[n].min>num) a[n].min=num; if(a[n].l==a[n].r) return; if(v<=(a[n].l+a[n].r)/2) Insert(n*2,v,num); else Insert(n*2+1,v,num); } int QMax(int n,int l,int r) { if(a[n].l==l&&a[n].r==r) return a[n].max; if(r<=(a[n].l+a[n].r)/2) return QMax(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) return QMax(n*2+1,l,r); else { return max(QMax(n*2,l,(a[n].l+a[n].r)/2),QMax(n*2+1,(a[n].l+a[n].r)/2+1,r)); } } int QMin(int n,int l,int r) { if(a[n].l==l&&a[n].r==r) return a[n].min; if(r<=(a[n].l+a[n].r)/2) return QMin(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) return QMin(n*2+1,l,r); else { return min(QMin(n*2,l,(a[n].l+a[n].r)/2),QMin(n*2+1,(a[n].l+a[n].r)/2+1,r)); } } void QMinus(int n,int l,int r) { if(a[n].l==l&&a[n].r==r) { printf("%d\n",a[n].max-a[n].min); } else { if(r<=(a[n].l+a[n].r)/2) QMinus(n*2,l,r); else if(l>=(a[n].l+a[n].r)/2+1) QMinus(n*2+1,l,r); else { int m1=QMax(n*2,l,(a[n].l+a[n].r)/2); int m2=QMax(n*2+1,(a[n].l+a[n].r)/2+1,r); int m3=QMin(n*2,l,(a[n].l+a[n].r)/2); int m4=QMin(n*2+1,(a[n].l+a[n].r)/2+1,r); printf("%d\n",max(m1,m2)-min(m3,m4)); } } } int main() { int i,j,n,m,x,l,r; while(scanf("%d %d",&n,&m)!=EOF) { Build(1,1,n); for(i=1;i<=n;i++) { scanf("%d",&x); Insert(1,i,x); } for(i=0;i<m;i++) { scanf("%d %d",&l,&r); QMinus(1,l,r); } } return 0; }