POJ 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264

题目描述:跟N个数和Q个询问,求询问区间[a, b]中最大值和最小值的差。

我的第一个线段树……

  1 /*

  2 Interval Tree

  3 */

  4 #include <cstdio>

  5 #include <cstring>

  6 #include <cstdlib>

  7 

  8 const int MAXN = 50000 + 2;

  9 const int INF = 2147483645;

 10 

 11 struct Node

 12 {

 13     int l, r;

 14     int maxi, mini;

 15     int l_child, r_child;

 16 };

 17 

 18 int cnt;

 19 Node Tree[ MAXN << 2 ];

 20 int findMax, findMin;

 21 

 22 void Build( int l, int r, Node &Td )

 23 {

 24     Td.l = l;

 25     Td.r = r;

 26 

 27     Td.maxi = -INF;

 28     Td.mini = INF;

 29 

 30     if ( l != r )

 31     {

 32         ++cnt;

 33         Td.l_child = cnt;

 34         Build( l, (l + r) / 2, Tree[ Td.l_child ] );

 35 

 36         ++cnt;

 37         Td.r_child = cnt;

 38         Build( (l + r) / 2 + 1, r, Tree[ Td.r_child ] );

 39     }

 40 

 41     return;

 42 }

 43 

 44 void Insert( int ii, int &e, Node &Td )

 45 {

 46     if ( ii == Td.l && ii == Td.r )

 47     {

 48         Td.maxi = Td.mini = e;

 49         return;

 50     }

 51 

 52     Td.maxi = Td.maxi > e ? Td.maxi : e;

 53     Td.mini = Td.mini < e ? Td.mini : e;

 54 

 55     if ( ii <= (Td.l + Td.r) / 2 )

 56         Insert( ii, e, Tree[ Td.l_child ] );

 57     else

 58         Insert( ii, e, Tree[ Td.r_child ] );

 59 

 60     return;

 61 }

 62 

 63 /*

 64 void show( int n )

 65 {

 66     for ( int i = 0; i <= n * 2; ++i )

 67       printf("[%d, %d]: %d %d\n", Tree[i].l, Tree[i].r, Tree[i].maxi, Tree[i].mini );

 68     return;

 69 }

 70 */

 71 

 72 void Query( int st, int ed, Node &Td )

 73 {

 74     if ( Td.maxi <= findMax && Td.mini >= findMin ) return;

 75 

 76     if ( st == Td.l && ed == Td.r )

 77     {

 78         findMax = findMax > Td.maxi ? findMax : Td.maxi;

 79         findMin = findMin < Td.mini ? findMin : Td.mini;

 80         return;

 81     }

 82 

 83     int mid = (Td.l + Td.r) / 2;

 84 

 85     if ( ed <= mid )

 86     {

 87         Query( st, ed, Tree[ Td.l_child ] );

 88     }

 89     else if ( st > mid  )

 90     {

 91         Query( st, ed, Tree[ Td.r_child ] );

 92     }

 93     else

 94     {

 95         Query( st, mid, Tree[ Td.l_child ] );

 96         Query( mid + 1, ed, Tree[ Td.r_child ] );

 97     }

 98 

 99 }

100 

101 int main()

102 {

103     int n, Q;

104     while ( scanf( "%d%d", &n, &Q ) != EOF )

105     {

106         cnt = 0;

107         Build( 1, n, Tree[0] );

108 

109         for ( int i = 0; i < n; ++i )

110         {

111             int a;

112             scanf( "%d", &a );

113             Insert( i + 1, a, Tree[0] );

114         }

115 

116       //  show(n);

117 

118         for ( int i = 0; i < Q; ++i )

119         {

120             int a, b;

121             scanf( "%d%d", &a, &b );

122             findMax = -INF;

123             findMin = INF;

124             Query( a, b, Tree[0] );

125             printf( "%d\n", findMax - findMin );

126         }

127     }

128     return 0;

129 }

你可能感兴趣的:(poj)