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

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

这个线段树建立是以数组的,根节点为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
 #include <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0xffffff0;
int maxa=-inf;
int mina=inf;
struct tree
{
int l,r;
int maxt,mint;
int mid()
{
return (l+r)/;
} };
tree Tree[];
void builttree(int root,int x,int y){
Tree[root].l=x;
Tree[root].r=y;
Tree[root].maxt=-inf;
Tree[root].mint=inf;
if(x!=y){
builttree(root*+,x,(x+y)/);
builttree(root*+,(x+y)/+,y);
}
}
void inserttree(int root,int i,int v){
if(Tree[root].l==i&Tree[root].r==i)
{
Tree[root].maxt=Tree[root].mint=v;
return;
}
Tree[root].maxt=max(Tree[root].maxt,v);
Tree[root].mint=min(Tree[root].mint,v);
if(i<=Tree[root].mid())
inserttree(root*+,i,v);
else
inserttree(root*+,i,v); }
void checktree(int root,int x,int y){
if(Tree[root].maxt<=maxa&&Tree[root].mint>=mina)
return;
if(Tree[root].l==x&&Tree[root].r==y)
{
maxa=max(maxa,Tree[root].maxt);
mina=min(mina,Tree[root].mint);
return ;
}
if(y<=Tree[root].mid())
checktree(root*+,x,y);
else if(x>Tree[root].mid())
checktree(root*+,x,y);
else {
checktree(root*+,x,Tree[root].mid());
checktree(root*+,Tree[root].mid()+,y);
} }
int main()
{
int n,m;
scanf("%d%d",&n,&m);
builttree(,,n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
inserttree(,i,x);
}
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mina=inf,maxa=-inf;
checktree(,x,y);
printf("%d\n",maxa-mina);
} return ; }
												

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

  1. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  2. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

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

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  4. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  5. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  6. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  7. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

  8. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  9. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

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

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

随机推荐

  1. HTML学习笔记(上)

    1. HTML介绍 1.1 什么是HTML HyperText Markup Language,超文本标记语言.简单来说,HTML文件本质上就是一个文本文件,但是这个文本文件是带有标签的. 不同的标签 ...

  2. linux du与ls查看文件大小时的区别

    du和ls查看文件大小的区别 du == disk usage (磁盘使用量,占用的磁盘空间)du 的基本使用du -s     #s参数是可以统计占硬盘空间大小的如 du -skh web-k或-- ...

  3. level-4

    [1.网页乱码的问题是如何产生的.怎么样解决?] 造成html网页乱码原因主要是html源代码内中文字内容与html编码不同造成.主要情况有以下三种: 1.比如网页源代码是gbk的编码,而内容中的中文 ...

  4. JavaWeb Request和Response

    1. Request与Response 1.1. Web应用运行机制 到目前为止,我们已经掌握了Web应用程序的运行机制,现在学习的就是Web应用程序运行机制中很重要的内容 —— Request与Re ...

  5. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  6. java.lang.ClassFormatError Duplicate field name&signature in class file XXXXXX【转】

    本文转载自:https://blog.csdn.net/ylchou/article/details/7739742 2012-7-5 15:06:25org.apache.catalina.core ...

  7. H3C 交换机设置telnet WEB用户

    huwei : local-user admin password cipher @#$@#$ service-type telnet ssh service-type telnet ssh leve ...

  8. 转-centos7下安装apache服务器httpd的yum方式安装

    转自Clement-Xu的csdn博客 http://blog.csdn.net/clementad/article/details/41620631   Apache在Linux系统中,其实叫“ht ...

  9. What's the difference between using “let” and “var” to declare a variable in JavaScript?

    https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare ...

  10. Tensorflow 从零开始

    1.安装pip pip是一个用于管理和安装Python包的工具,类似于LINUX 的yum命令一样! 命令(Ubuntu系统):sudo apt-get install python-pip 验证安装 ...