首页 > 代码库 > POJ 3264 Balanced Lineup

POJ 3264 Balanced Lineup

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 47419 Accepted: 22270
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 ≤ ABN), 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 31734251 54 62 2

Sample Output

630

Source

USACO 2007 January Silver
 
 
  线段树基础题。
 1 #include <iostream> 2 #include <algorithm> 3 #include <map> 4 #include <vector> 5 #include <functional> 6 #include <string> 7 #include <cstring> 8 #include <queue> 9 #include <set>10 #include <cmath>11 #include <cstdio>12 using namespace std;13 #define IOS ios_base::sync_with_stdio(false)14 #define TIE std::cin.tie(0)15 #define MIN2(a,b) (a<b?a:b)16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))17 #define MAX2(a,b) (a>b?a:b)18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))19 typedef long long LL;20 typedef unsigned long long ULL;21 const int INF = 0x3f3f3f3f;22 const double PI = 4.0*atan(1.0);23 24 const int MAX_N = 50005;25 int tot, n, qmax, qmin;26 struct Node{27     int L, R, a, b, maxr, minr;28 }T[4*MAX_N];29 inline void Up(int x)30 {31     T[x].maxr = MAX2(T[T[x].L].maxr, T[T[x].R].maxr);32     T[x].minr = MIN2(T[T[x].L].minr, T[T[x].R].minr);33 }34 void Build(int L, int R)35 {36     int x = ++tot;37     T[x].a = L; T[x].b = R;38     T[x].L = T[x].R = 0;39     if (L == R) {40         int temp;41         scanf("%d", &temp); 42         T[x].maxr = T[x].minr = temp;43         return; 44     }45     int mid = (L + R) >> 1;46     T[x].L = tot + 1; Build(L, mid);47     T[x].R = tot + 1; Build(mid + 1, R);48     Up(x);49 }50 51 void Query(int x, int a, int b)52 {53     if (T[x].a >= a&&T[x].b <= b){ 54         qmax =MAX2(qmax, T[x].maxr); 55         qmin = MIN2(qmin, T[x].minr);56         return; 57     }58     int mid = (T[x].a + T[x].b) >> 1;59     int maxl = -INF, maxr = -INF, minl = INF, minr = INF;60     if (mid >= a&&T[x].L)61         Query(T[x].L, a, b);62     if (mid < b&&T[x].R)63         Query(T[x].R, a, b);64 }65 66 int main()67 {68     int qn,a,b,ma=0,mi=0;69     while (~scanf("%d%d", &n, &qn)){70         tot = 0;71         Build(1, n);72         for (int i = 0; i < qn; i++){73             scanf("%d%d", &a, &b);74             qmax = -INF; qmin = INF;75             Query(1, a, b);76             printf("%d\n", qmax - qmin);77         }78     }79 }

 

POJ 3264 Balanced Lineup