1. 程式人生 > >Balanced Lineup(線段樹的簡單了解)

Balanced Lineup(線段樹的簡單了解)

個人 ica tree ngs mat can scanf rate class

個人心得:線段樹就是將一段序列拆分為一個個單獨的節點,不過每倆個節點又可以聯系在一起,所以就能很好的結合,比如這一題,

每次插入的時候都將這一段區間的最大最小值更新,就能大大減少時間。

這個線段樹建立是以數組的,根節點為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 ≤ 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
 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(線段樹的簡單了解)