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
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
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(线段树的简单了解)的更多相关文章
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
随机推荐
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
- 最小化CentOS6.7(64bit)---安装mysql5.5、jdk、tomcat
********mysql******** ------------------------------------------------------------------------------ ...
- Openstak(M版)控制节点安装
#############修改hosts文件 # controller10.0.0.11 controller# compute110.0.0.31 compute1# block110.0.0.41 ...
- str字符串、bool类型常用方法总结
字符串拼接 必须是字符串与字符串拼接 print('马化腾'+'马云') print('马化腾' * 10) 将打印10个马化腾 字符串翻转 [ : :-1] 字符串可以加和乘,不能减和乘 input ...
- iOS UIScrollView 滚动到当前展示的视图居中展示
需求展示: 测试效果1 first uiscrollView 宽度 为屏幕宽度 滚动步长 为 scroll 宽度的1/3 分析: 这个是最普通版 无法使每一次滚动的结果子视图居中展示, WA ...
- Scalability, Availability & Stability Patterns
https://blog.csdn.net/ajian005/article/details/6191814 一 自我有要求的读者应该提出问题:(研习:掌握层次:)能力级别:不会(了解)——领会( ...
- Python学习进程(7)字符串
本节介绍字符串的创建与操作方法. (1)创建字符串: 创建字符串既可以用单引号也可以用双引号: root@SJM:/home/sunjimeng/桌面# cat text.py ...
- 每天一个Linux命令(38)top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. (1)用法: 用法: top [参数] top是 ...
- 021_在Eclipse Indigo中安装插件hadoop-eclipse-plugin-1.2.1.jar,直接运行wordcount程序
1.工具介绍 Eclipse Idigo.JDK1.7-32bit.hadoop1.2.1.hadoop-eclipse-plugin-1.2.1.jar(自己网上下载) 2.插件安装步骤 1)将ha ...
- 【Topcoder】SRM157 DIV2总结
250分题:简单的二分,就是平常玩的猜数字游戏 代码:GitHub 500分题:给出一个员工一天的打卡时间段,要求求出员工这一天的工资.其中正常上班时间是6:00:00到18:00:00,薪水是wag ...