POJ 3264 Balanced Lineup (线段树||RMQ)

A - Balanced Lineup
Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

题目大意:

  这道题是说,给你n个数字(n<=50000),然后Q( Q<=2*100000)个询问,在每个询问的区间中,最大值和最小值的差值是多少?

解题思路:

  直接用线段树就可以过,每个节点对应一个区间,直接logn的时间查询最大值和最小值,然后通过两者之间的差值就可以得到最后的结果。

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 
 4 using namespace std;
 5 
 6 # define MAX 50004
 7 # define lid id<<1
 8 # define rid id<<1|1
 9 
10 struct Segtree
11 {
12     int l,r;
13     int mx,mn;
14 }tree[MAX*4];
15 int a[MAX];
16 
17 void push_up( int id )
18 {
19     tree[id].mx = max(tree[lid].mx,tree[rid].mx);
20     tree[id].mn = min(tree[lid].mn,tree[rid].mn);
21 }
22 
23 void build( int id,int l,int r )
24 {
25     tree[id].l = l; tree[id].r = r;
26     if ( l==r )
27     {
28         tree[id].mx = tree[id].mn = a[l];
29         return;
30     }
31     int mid = ( tree[id].l+tree[id].r )/2;
32     build(lid,l,mid);
33     build(rid,mid+1,r);
34     push_up(id);
35 }
36 
37 int query1( int id,int l,int r )
38 {
39     if ( tree[id].l==l&&tree[id].r==r )
40     {
41         return tree[id].mx;
42     }
43     int mid = ( tree[id].l+tree[id].r )/2;
44     if ( r <= mid )
45         return query1(lid,l,r);
46     else if ( l > mid )
47         return query1(rid,l,r);
48     else
49     {
50         return max(query1(lid,l,mid),query1(rid,mid+1,r));
51     }
52 }
53 
54 int query2( int id,int l,int r )
55 {
56     if ( tree[id].l==l&&tree[id].r==r )
57     {
58         return tree[id].mn;
59     }
60     int mid = ( tree[id].l+tree[id].r )/2;
61     if ( r <= mid )
62         return query2(lid,l,r);
63     else if ( l > mid )
64         return query2(rid,l,r);
65     else
66     {
67         return min(query2(lid,l,mid),query2(rid,mid+1,r));
68     }
69 }
70 
71 
72 int main(void)
73 {
74     int n; scanf("%d",&n);
75     int q; scanf("%d",&q);
76     for ( int i = 1;i <= n;i++ )
77     {
78         scanf("%d",&a[i]);
79     }
80     build(1,1,n);
81     while ( q-- )
82     {
83         int t1,t2; scanf("%d%d",&t1,&t2);
84         int ans = query1(1,t1,t2)-query2(1,t1,t2);
85         printf("%d\n",ans);
86     }
87 
88 
89     return 0;
90 }

 

posted @ 2015-09-15 19:31  BYYB_0506  阅读(199)  评论(0编辑  收藏  举报