[POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34306 | Accepted: 16137 | |
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.
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问题。线段树的应用。
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<ctype.h> 5 #include<stdlib.h> 6 #include<stdbool.h> 7 8 #define rep(i,a,b) for(i=(a);i<=(b);i++) 9 #define clr(x,y) memset(x,y,sizeof(x)) 10 #define sqr(x) (x*x) 11 #define LL long long 12 13 const int INF=0xffffff0; 14 15 struct { 16 int L,R; 17 int minV,maxV; 18 } tree[800010]; 19 20 int i,j,n,q,minV,maxV; 21 22 int min(int a, int b) 23 { 24 if(a<b) return a; 25 return b; 26 } 27 28 int max(int a,int b) 29 { 30 if(a>b) return a; 31 return b; 32 } 33 34 void BuildTree(int root,int L,int R) 35 { 36 tree[root].L=L; 37 tree[root].R=R; 38 tree[root].maxV=-INF; 39 tree[root].minV=INF; 40 41 if(L!=R) { 42 BuildTree(2*root,L,(L+R)/2); 43 BuildTree(2*root+1,(L+R)/2+1,R); 44 } 45 46 } 47 48 void Insert(int root,int i,int v) 49 { 50 int mid; 51 52 if(tree[root].L==tree[root].R) { 53 tree[root].maxV=tree[root].minV=v; 54 return ; 55 } 56 57 tree[root].minV=min(tree[root].minV,v); 58 tree[root].maxV=max(tree[root].maxV,v); 59 60 mid=(tree[root].L+tree[root].R)/2; 61 if(i<=mid) 62 Insert(2*root,i,v); 63 else 64 Insert(2*root+1,i,v); 65 66 } 67 68 void Query(int root,int s,int e) 69 { 70 int mid; 71 72 if(tree[root].minV>=minV && tree[root].maxV<=maxV) return ; 73 if(tree[root].L==s && tree[root].R==e) { 74 minV=min(minV,tree[root].minV); 75 maxV=max(maxV,tree[root].maxV); 76 return ; 77 } 78 79 mid=(tree[root].L+tree[root].R)/2; 80 81 if(e<=mid) 82 Query(2*root,s,e); 83 else if(s>mid) 84 Query(2*root+1,s,e); 85 else { 86 Query(2*root,s,mid); 87 Query(2*root+1,mid+1,e); 88 } 89 90 } 91 92 93 int main() 94 { 95 int i,x,y; 96 97 scanf("%d%d",&n,&q); 98 BuildTree(1,1,n); 99 rep(i,1,n) { 100 scanf("%d",&x); 101 Insert(1,i,x); 102 } 103 104 while(q--) { 105 scanf("%d%d",&x,&y); 106 minV=INF; 107 maxV=-INF; 108 Query(1,x,y); 109 printf("%d\n",maxV-minV); 110 } 111 112 return 0; 113 } 114 115