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 }
				
时间: 2024-10-07 18:42:34

Balanced Lineup(线段树的简单了解)的相关文章

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 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 Joh

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 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 Joh

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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 Joh

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

POJ 3264 Balanced Lineup (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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

POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i

POJ3264_Balanced Lineup(线段树/单点更新)

解题报告 题意: 求区间内最大值和最小值的差值. 思路: 裸线段树,我的线段树第一发.区间最值. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 #define LL long long using namespace std; LL minn[201000],maxx[201000]; void update(LL root,LL l,LL r,LL p,LL

poj 3264 Balanced Lineup(线段数求区间最大最小值)

链接:http://poj.org/problem?id=3264 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32772   Accepted: 15421 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.