Balanced Lineup(线段树的简单了解)

Posted 余生漫漫浪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Balanced Lineup(线段树的简单了解)相关的知识,希望对你有一定的参考价值。

个人心得:线段树就是将一段序列拆分为一个个单独的节点,不过每俩个节点又可以联系在一起,所以就能很好的结合,比如这一题,

每次插入的时候都将这一段区间的最大最小值更新,就能大大减少时间。

这个线段树建立是以数组的,根节点为0,后面每次都是父节点*2+1/2。

这题简单的教会了我如何创建线段树,以及一些简单的线段树操作,还要继续加深。

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
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<iostream>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 const int inf=0xffffff0;
 8 int maxa=-inf;
 9 int mina=inf;
10 struct tree
11 {
12     int l,r;
13     int maxt,mint;
14     int mid()
15     {
16         return (l+r)/2;
17     }
18 
19 };
20 tree Tree[800000];
21 void builttree(int root,int x,int y){
22       Tree[root].l=x;
23       Tree[root].r=y;
24       Tree[root].maxt=-inf;
25       Tree[root].mint=inf;
26       if(x!=y){
27         builttree(root*2+1,x,(x+y)/2);
28         builttree(root*2+2,(x+y)/2+1,y);
29       }
30 }
31 void inserttree(int root,int i,int v){
32       if(Tree[root].l==i&Tree[root].r==i)
33       {
34           Tree[root].maxt=Tree[root].mint=v;
35           return;
36       }
37       Tree[root].maxt=max(Tree[root].maxt,v);
38       Tree[root].mint=min(Tree[root].mint,v);
39       if(i<=Tree[root].mid())
40            inserttree(root*2+1,i,v);
41       else
42         inserttree(root*2+2,i,v);
43 
44 }
45 void checktree(int root,int x,int y){
46     if(Tree[root].maxt<=maxa&&Tree[root].mint>=mina)
47         return;
48     if(Tree[root].l==x&&Tree[root].r==y)
49     {
50         maxa=max(maxa,Tree[root].maxt);
51         mina=min(mina,Tree[root].mint);
52         return ;
53     }
54     if(y<=Tree[root].mid())
55           checktree(root*2+1,x,y);
56     else if(x>Tree[root].mid())
57         checktree(root*2+2,x,y);
58     else {
59         checktree(root*2+1,x,Tree[root].mid());
60         checktree(root*2+2,Tree[root].mid()+1,y);
61     }
62 
63 
64 }
65 int main()
66 {
67     int n,m;
68     scanf("%d%d",&n,&m);
69     builttree(0,1,n);
70     for(int i=1;i<=n;i++)
71     {
72         int x;
73         scanf("%d",&x);
74         inserttree(0,i,x);
75     }
76     for(int i=1;i<=m;i++)
77     {
78         int x,y;
79         scanf("%d%d",&x,&y);
80         mina=inf,maxa=-inf;
81         checktree(0,x,y);
82         printf("%d\n",maxa-mina);
83     }
84 
85     return 0;
86 
87 }

 



以上是关于Balanced Lineup(线段树的简单了解)的主要内容,如果未能解决你的问题,请参考以下文章

G - Balanced Lineup

POJ3264-Balanced Lineup-线段树

POJ - 3264——Balanced Lineup(入门线段树)

POJ3264 Balanced Lineup 线段树+单点更新

POJ 3264 Balanced Lineup 线段树

POJ 3264 Balanced Lineup (线段树)